If CLAHE is not giving you satisfactory results, and Homomorphic Filtering has limitations for your use case, you may need to explore other techniques that are designed for high dynamic range (HDR) scenes or that can deal with lighting variations more effectively.
One approach that is often used for images with a wide range of intensities is multi-scale Retinex, especially its variant known as Multi-Scale Retinex with Color Restoration (MSRCR). Retinex theory is based on the assumption that the perceived color of an object is invariant to the illumination source and that the human visual system is able to separate the illumination from the reflectance in a scene.
Below is an example implementation of MSRCR in Python. This requires the OpenCV library, which you can install via pip if you haven't already done so:
pip install opencv-python
And here is the code:
import cv2
import numpy as np
def multiscale_retinex_with_color_restoration(image, scales):
"""
Perform Multi-Scale Retinex with Color Restoration.
Parameters:
- image: Input image
- scales: List of sigma values for Gaussian blur
Returns:
- Processed image after applying MSRCR algorithm
"""
img_float = np.float32(image) + 1.0 # To avoid log(0)
# Weight for the sum of log images
retinex = np.zeros_like(img_float)
for scale in scales:
blurred = cv2.GaussianBlur(img_float, (0, 0), scale)
retinex += np.log(img_float) - np.log(blurred)
retinex = retinex / len(scales) # Averaging
# Color Restoration step
h, w, _ = image.shape
intensity = np.sum(retinex, axis=2) / 3
intensity = cv2.GaussianBlur(intensity, (0, 0), scales[0])
intensity = (intensity - np.min(intensity)) / (np.max(intensity) - np.min(intensity))
intensity = (intensity * 255).astype(np.uint8)
intensity = np.expand_dims(intensity, axis=2) / 255.0 # Normalize and convert to 3D array for broadcasting
retinex = retinex / intensity # Color Restoration
# Simplest Color Balance step (percentile-based)
total = h * w * 3.0
low_val = np.percentile(retinex, 1)
high_val = np.percentile(retinex, 99)
retinex = np.clip(retinex, low_val, high_val)
retinex = ((retinex - low_val) / (high_val - low_val)) * 255.0
return retinex.astype(np.uint8)
# Define the scales for Retinex
scales = [15, 80, 250]
# Read your image (make sure it's in BGR format if using OpenCV)
img = cv2.imread('your_image.jpg')
msrcr_result = multiscale_retinex_with_color_restoration(img, scales)
# Show the result
cv2.imshow('MSRCR Result', msrcr_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Optionally, save the result
cv2.imwrite('msrcr_result.jpg', msrcr_result)
The multiscale_retinex_with_color_restoration function takes an image and a list of sigma values to define the scales of Gaussian blur for the Retinex computation. The range of sigma values (scales) controls the level of detail that is enhanced, and you might need to experiment with this to get the best results for your images. The function also includes a color restoration step and a simple percentile-based color balance step.
This approach should work well for images with varying lighting conditions, including those with very bright and very dark regions. However, you might still need to tweak parameters such as sigma values and the percentiles used in the color balance step to better suit your specific images.
Remember that finding the perfect solution for such diverse lighting conditions can be challenging, and it will likely require some iteration and tuning specific to your dataset. If you continue to have difficulties, it might be worthwhile to consider machine learning approaches where a model is trained specifically for your image characteristics.