A Python implementation of the color constancy algorithm for image color correction, particularly useful for medical imaging and computer vision applications.
Color constancy is the ability to perceive colors of objects invariant to the color of the light source. This package implements a color constancy algorithm that transforms image colors acquired under an unknown light source to appear as if they were taken under a canonical (reference) light source.
The algorithm performs two main steps:
- Gamma correction: Linearizes the image to remove display gamma
- Color correction: Applies the Grey-Edge algorithm with Minkowski p-norm (p=3) to estimate and correct for the illuminant
- Simple and efficient color constancy correction
- Support for various input formats (file paths, numpy arrays)
- Customizable gamma correction
- Type hints for better IDE support
- Comprehensive error handling
- Well-tested with pytest
git clone https://github.com/ytworks/ColorConstancy.git
cd ColorConstancy
pip install -e .git clone https://github.com/ytworks/ColorConstancy.git
cd ColorConstancy
pip install -e ".[dev]"from color_constancy import color_constancy
import cv2
# Process an image file
corrected = color_constancy('path/to/image.jpg')
cv2.imwrite('corrected_image.jpg', corrected)
# Process a numpy array
image = cv2.imread('path/to/image.jpg')
corrected = color_constancy(image, gamma=2.2)from pathlib import Path
from color_constancy import color_constancy
import cv2
# Using file path (str or Path object)
corrected = color_constancy('image.jpg')
corrected = color_constancy(Path('image.jpg'))
# Using numpy array
image = cv2.imread('image.jpg')
corrected = color_constancy(image)
# Custom gamma value
corrected = color_constancy('image.jpg', gamma=2.4)from color_constancy import color_constancy
try:
corrected = color_constancy('image.jpg')
except ValueError as e:
print(f"Error processing image: {e}")
except TypeError as e:
print(f"Invalid input type: {e}")cd examples
python basic_usage.pyThis will process the sample images in examples/images/ and create corrected versions.
The implementation uses the Grey-Edge algorithm with the following mathematical foundation:
-
Gamma Correction:
- Linearizes the image using inverse gamma transformation
- Default gamma value: 2.2 (standard for sRGB displays)
-
Illuminant Estimation:
- Computes the Minkowski p-norm (p=3) of the image
- Estimates the RGB illuminant vector
- Normalizes the vector using L2 norm
-
Color Correction:
- Applies the inverse of the estimated illuminant
- Clips values to valid range [0, 255]
Apply color constancy algorithm to an image.
Parameters:
image(Union[str, Path, np.ndarray]): Input image as file path or numpy arraygamma(float): Gamma correction value (default: 2.2)
Returns:
np.ndarray: Corrected image as uint8 numpy array
Raises:
ValueError: If image cannot be loaded or processedTypeError: If input type is invalid
Run the test suite:
# Basic test run
pytest
# With coverage report
pytest --cov=color_constancy --cov-report=html
# Verbose output
pytest -vWe welcome contributions! Please see CONTRIBUTING.md for guidelines.
- Fork the repository
- Clone your fork
- Install in development mode:
pip install -e ".[dev]" pre-commit install - Make your changes
- Run tests:
pytest - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Sample images from the International Skin Imaging Collaboration (ISIC) dataset
- Algorithm based on research in computational color constancy
- β Core algorithm implementation
- β Type hints and documentation
- β Unit tests
- β Example code
- β CI/CD setup
- π PyPI package (planned)
- Author: Yuzo Takagi
- GitHub: @ytworks
- Issues: GitHub Issues