paidiver / paidiverpy

Create pipelines for preprocessing image data for biodiversity analysis.
Apache License 2.0
3 stars 0 forks source link

Illumination correction #19

Open soutobias opened 1 month ago

soutobias commented 1 month ago

What:

Illumination correction addresses uneven lighting in images to ensure uniform light distribution. This is crucial in underwater imaging, where uneven lighting is common due to lens vignetting and light source positioning. Lens vignetting reduces brightness towards the edges of an image, caused by lens design flaws, focal length, or aperture settings. Uneven light source positioning in underwater vehicles, like AUVs or ROVs, creates high-brightness spots near the light source and darker areas elsewhere, affecting color accuracy and overall image quality.

Why:

Proper illumination correction is essential for accurate image analysis and interpretation. Inconsistent lighting can distort colors, obscure details, and hinder the evaluation of underwater scenes. Correcting illumination helps standardize images, making them more suitable for tasks such as biodiversity assessment, species identification, and visual documentation.

How:

Illumination correction can be achieved through various techniques:

  1. Background Subtraction:

    • Median Filtering: Removes noise and enhances uniformity by replacing each pixel with the median value of neighboring pixels.
    • Gaussian Blur: Smoothens the image and reduces lighting variations by applying a Gaussian filter to estimate and subtract background illumination.
  2. Histogram Matching:

    • This technique normalizes lighting by adjusting the image’s histogram to match a reference histogram. It ensures consistent light distribution across the image.
  3. Retinex Algorithms:

    • Single-Scale Retinex (SSR): Enhances image details by improving local contrast and brightness. It simulates human visual perception to correct lighting non-uniformities.
    • Multi-Scale Retinex (MSR): Extends SSR by analyzing multiple scales, providing a more comprehensive correction for varying lighting conditions.

Python Code Examples:

  1. Median Filtering for Background Subtraction:

    import cv2
    import numpy as np
    
    def apply_median_filter(image_path, ksize=5):
        # Load image
        img = cv2.imread(image_path, cv2.IMREAD_COLOR)
    
        # Apply median filter
        filtered_img = cv2.medianBlur(img, ksize)
    
        # Save and display result
        cv2.imwrite('median_filtered.jpg', filtered_img)
        cv2.imshow('Median Filtered Image', filtered_img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    # Example usage
    apply_median_filter('image.jpg')
  2. Histogram Matching:

    import cv2
    import numpy as np
    
    def match_histograms(source_path, reference_path):
        # Load images
        source = cv2.imread(source_path, cv2.IMREAD_COLOR)
        reference = cv2.imread(reference_path, cv2.IMREAD_COLOR)
    
        # Convert to YUV color space
        source_yuv = cv2.cvtColor(source, cv2.COLOR_BGR2YUV)
        reference_yuv = cv2.cvtColor(reference, cv2.COLOR_BGR2YUV)
    
        # Equalize histograms of Y channel
        source_yuv[:,:,0] = cv2.equalizeHist(source_yuv[:,:,0])
        reference_yuv[:,:,0] = cv2.equalizeHist(reference_yuv[:,:,0])
    
        # Convert back to BGR color space
        result = cv2.cvtColor(source_yuv, cv2.COLOR_YUV2BGR)
    
        # Save and display result
        cv2.imwrite('histogram_matched.jpg', result)
        cv2.imshow('Histogram Matched Image', result)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    # Example usage
    match_histograms('source_image.jpg', 'reference_image.jpg')
  3. Retinex Algorithm (Simple Implementation):

    import cv2
    import numpy as np
    
    def retinex_enhancement(image_path, sigma=30):
        # Load image
        img = cv2.imread(image_path, cv2.IMREAD_COLOR)
    
        # Convert to float32 for better precision
        img_float = np.float32(img)
    
        # Apply Gaussian blur
        blur = cv2.GaussianBlur(img_float, (0, 0), sigma)
    
        # Retinex enhancement
        img_retinex = np.log10(img_float + 1) - np.log10(blur + 1)
        img_retinex = np.exp(img_retinex) - 1
        img_retinex = np.uint8(np.clip(img_retinex, 0, 255))
    
        # Save and display result
        cv2.imwrite('retinex_enhanced.jpg', img_retinex)
        cv2.imshow('Retinex Enhanced Image', img_retinex)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    # Example usage
    retinex_enhancement('image.jpg')

What to expect:

The result will be an image with improved and more uniform illumination. Corrected images will show a more consistent light distribution with reduced brightness gradients and better visual clarity, allowing for clearer and more accurate analysis of image details.

What makes it difficult:

Success Metrics:

LoicVA commented 1 month ago

For histogram matching, what would be the reference image?

Mojtabamsd commented 1 month ago

for high resolution images, rolling ball is slow, need to optimize the code or resize image before processing

soutobias commented 1 month ago

@Mojtabamsd , I apply some changes on your code on the develop branch. Please take a look and see if everything is fine