🐶
Machine Vision

CLAHE for RGB Images: A Step-by-Step Guide

By Jan on 03/05/2025

Learn how to enhance the contrast of RGB color images effectively using Contrast Limited Adaptive Histogram Equalization (CLAHE) with this comprehensive guide.

CLAHE for RGB Images: A Step-by-Step Guide

Table of Contents

Introduction

This article provides a step-by-step guide on how to enhance the contrast of an image using Contrast Limited Adaptive Histogram Equalization (CLAHE) technique in Python. We will use the OpenCV library for image processing.

Step-by-Step Guide

  1. Import necessary libraries:
import cv2
  1. Load the image:
image = cv2.imread('image.jpg')
  1. Convert the image from RGB to LAB color space:
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
  1. Split the LAB image into its channels:
l_channel, a_channel, b_channel = cv2.split(lab_image)
  1. Create a CLAHE object:
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
  1. Apply CLAHE to the L channel:
enhanced_l_channel = clahe.apply(l_channel)
  1. Merge the enhanced L channel with the A and B channels:
enhanced_lab_image = cv2.merge((enhanced_l_channel, a_channel, b_channel))
  1. Convert the enhanced LAB image back to RGB color space:
enhanced_image = cv2.cvtColor(enhanced_lab_image, cv2.COLOR_LAB2BGR)
  1. Display or save the enhanced image:
cv2.imshow('Enhanced Image', enhanced_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

or

cv2.imwrite('enhanced_image.jpg', enhanced_image)

Explanation:

  • CLAHE is applied only to the L channel of the LAB image because it represents the luminance or lightness of the image, while the A and B channels represent the color information.
  • clipLimit parameter limits the amplification of contrast.
  • tileGridSize parameter defines the size of the tiles used for histogram equalization.
  • By merging the enhanced L channel with the original A and B channels, we preserve the color information of the image while enhancing its contrast.

Code Example

This Python code uses the OpenCV library to enhance the contrast of an image. It reads an image, converts it to LAB color space, applies Contrast Limited Adaptive Histogram Equalization (CLAHE) to the lightness channel, merges the channels back, converts the image back to BGR color space, and finally displays or saves the enhanced image.

import cv2

# Load the image
image = cv2.imread('image.jpg')

# Convert the image from RGB to LAB color space
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)

# Split the LAB image into its channels
l_channel, a_channel, b_channel = cv2.split(lab_image)

# Create a CLAHE object
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))

# Apply CLAHE to the L channel
enhanced_l_channel = clahe.apply(l_channel)

# Merge the enhanced L channel with the A and B channels
enhanced_lab_image = cv2.merge((enhanced_l_channel, a_channel, b_channel))

# Convert the enhanced LAB image back to RGB color space
enhanced_image = cv2.cvtColor(enhanced_lab_image, cv2.COLOR_LAB2BGR)

# Display the enhanced image
cv2.imshow('Enhanced Image', enhanced_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Alternatively, save the enhanced image
# cv2.imwrite('enhanced_image.jpg', enhanced_image)

Explanation:

  1. Import cv2: This line imports the OpenCV library, which provides image processing functions.
  2. Load the image: This line loads the image named 'image.jpg' from the current directory.
  3. Convert to LAB color space: This line converts the image from the default BGR color space to LAB color space. LAB color space separates lightness (L) from color (A and B channels), making it suitable for contrast enhancement.
  4. Split the channels: This line separates the LAB image into its three channels: L, A, and B.
  5. Create CLAHE object: This line creates a CLAHE object with a clip limit of 2.0 and a tile grid size of 8x8 pixels. These parameters control the contrast enhancement.
  6. Apply CLAHE to L channel: This line applies CLAHE to the L channel, which represents the lightness of the image.
  7. Merge the channels: This line merges the enhanced L channel with the original A and B channels to create the enhanced LAB image.
  8. Convert back to BGR: This line converts the enhanced LAB image back to the BGR color space for display or saving.
  9. Display or save the image: These lines either display the enhanced image in a window or save it to a file named 'enhanced_image.jpg'.

This code effectively enhances the contrast of an image while preserving its colors using the CLAHE algorithm.

Additional Notes

General:

  • Purpose: CLAHE is particularly useful for enhancing images with poor contrast or uneven lighting conditions, such as medical images or outdoor photographs.
  • Advantages over standard histogram equalization: CLAHE avoids over-amplifying noise in relatively homogeneous regions of the image by performing equalization locally.
  • Alternative color spaces: While LAB is commonly used, other color spaces with a separate luminance channel (e.g., YUV, HSV) can also be used with CLAHE.

Parameters:

  • clipLimit:
    • Controls the amount of contrast limiting.
    • Higher values increase contrast but may introduce artifacts.
    • A value of 0 effectively disables CLAHE, resulting in standard histogram equalization.
  • tileGridSize:
    • Determines the size of the local regions for histogram equalization.
    • Smaller tiles result in more localized enhancement but may create a blocky appearance.
    • Larger tiles provide smoother transitions but may be less effective in enhancing fine details.

Implementation Details:

  • OpenCV's createCLAHE() function: This function initializes the CLAHE algorithm with the specified parameters.
  • Applying to other channels: While applying CLAHE to all channels might seem tempting, it can lead to color distortions. It's generally recommended to apply it only to the luminance channel.

Applications:

  • Medical imaging: Enhancing contrast in X-rays, CT scans, and MRI images to improve visibility of anatomical structures.
  • Microscopy: Enhancing images of cells and tissues to highlight details.
  • Security cameras: Improving visibility in low-light conditions.
  • Object detection: Preprocessing images to improve the performance of object detection algorithms.

Further Exploration:

  • Experiment with different clipLimit and tileGridSize values to observe their effects on the enhanced image.
  • Compare the results of CLAHE with other contrast enhancement techniques, such as standard histogram equalization and adaptive histogram equalization (AHE).
  • Explore the use of CLAHE in different applications and image domains.

Summary

This code snippet demonstrates how to enhance the contrast of an image using Contrast Limited Adaptive Histogram Equalization (CLAHE) technique with OpenCV in Python.

Here's a breakdown of the process:

  1. Load Image: Read the image file using cv2.imread().
  2. Convert to LAB Color Space: Transform the image from RGB to LAB color space using cv2.cvtColor(). This is done because CLAHE is applied only on the luminance channel (L channel) to avoid amplifying color noise.
  3. Split Channels: Separate the LAB image into its individual L, A, and B channels using cv2.split().
  4. Create CLAHE Object: Initialize a CLAHE object with desired parameters like clipLimit (controls contrast amplification) and tileGridSize (defines tile size for histogram equalization).
  5. Apply CLAHE: Apply the CLAHE algorithm to the L channel using clahe.apply().
  6. Merge Channels: Combine the enhanced L channel with the original A and B channels using cv2.merge() to reconstruct the image.
  7. Convert Back to RGB: Convert the enhanced LAB image back to RGB color space using cv2.cvtColor().
  8. Display/Save Image: Display the enhanced image using cv2.imshow() or save it to a file using cv2.imwrite().

Key Points:

  • CLAHE is applied only to the L channel to enhance contrast without affecting color information.
  • clipLimit and tileGridSize parameters allow fine-tuning the contrast enhancement.
  • This technique is particularly useful for images with poor lighting or low contrast.

Conclusion

In conclusion, this article elucidated the process of enhancing image contrast using the CLAHE algorithm in Python with the OpenCV library. By focusing on the luminance channel in the LAB color space, CLAHE effectively amplifies contrast while preserving color integrity. The article provided a step-by-step code breakdown, explained key parameters, and highlighted the advantages of CLAHE over traditional histogram equalization techniques. The insights provided equip readers with the knowledge to implement and fine-tune CLAHE for various applications, including medical imaging, microscopy, and object detection, ultimately improving image clarity and detail visibility.

References

  • Color Image Histogram CLAHE with OpenCV - FreedomVC Color Image Histogram CLAHE with OpenCV - FreedomVC | Contrast Limited Adaptive Histogram Equalization (CLAHE) - a color image histogram improvement method that improves over global equalization
  • Simple illumination correction in images OpenCV C++ - Stack ... Simple illumination correction in images OpenCV C++ - Stack ... | Jun 21, 2014 ... Convert the RGB image to Lab color-space (e.g., any color-space with a luminance channel will work fine), then apply adaptive histogramĀ ...
  • CLAHE for color image OpenCV 3.0 - OpenCV Q&A Forum CLAHE for color image OpenCV 3.0 - OpenCV Q&A Forum | Feb 1, 2016 ... use of clahe but it only works for grayscale image (even though I ... rgb Here but "split" doesn't work and I do not know what is theĀ ...
  • adapthisteq adapthisteq | Examples Ā· Apply Contrast-Limited Adaptive Histogram Equalization (CLAHE) Ā· Apply CLAHE to Indexed Color Image.
  • The output of CLAHE applied on RGB color model. | Download ... The output of CLAHE applied on RGB color model. | Download ... | Download scientific diagram | The output of CLAHE applied on RGB color model. from publication: Performing Contrast Limited Adaptive Histogram Equalization Technique on Combined Color Models for Underwater Image Enhancement | This paper describes method to improve the visibility of underwater images. Using Contrast Limited Adaptive Histogram Equalization (CLAHE) technique, our method derives the enhanced image from combination of outputs performed on RGB color model and HSV color model that is... | Equalization, Histogram and Image Enhancement | ResearchGate, the professional network for scientists.
  • Adaptive Touch to Histograms— CLAHE | by Muhammed Nur Talha ... Adaptive Touch to Histograms— CLAHE | by Muhammed Nur Talha ... | A Comprehensive Look at Histogram and Its Implementations…
  • CLAHE for Quapth - Image Analysis - Image.sc Forum CLAHE for Quapth - Image Analysis - Image.sc Forum | Hi Everyone, I came across this paper : Protocol for high-plex, whole-slide imaging of human formalin-fixed paraffin-embedded tissue using PhenoCycler-Fusion. Redirecting In the paragraph below they mentioned using CALHE I am familar with the CALHE method using openCv but the output is a png image which loses all the information when I open it in Quapth. Does anyone have any idea how it’s been done for Quapth?
  • Enhance Local Contrast (CLAHE) Enhance Local Contrast (CLAHE) | The ImageJ wiki is a community-edited knowledge base on topics relating to ImageJ, a public domain program for processing and analyzing scientific images, and its ecosystem of derivatives and variants, including ImageJ2, Fiji, and others.
  • Transforms (augmentations.transforms) - Albumentations ... Transforms (augmentations.transforms) - Albumentations ... | Albumentations provides a comprehensive, high-performance framework for augmenting images to improve machine learning models. Ideal for computer vision applications, supporting a wide range of augmentations.

Were You Able to Follow the Instructions?

šŸ˜Love it!
😊Yes
😐Meh-gical
šŸ˜žNo
🤮Clickbait