22
votes

CLAHE is Contrast Limited Adaptive Histogram Equalization and a source in C can be found at http://tog.acm.org/resources/GraphicsGems/gemsiv/clahe.c

So far I have only seen some examples/tutorials about applying CLAHE on grayscale images so is it possible to apply CLAHE on color images (such as RGB 3 channles images)? If yes, how?

2
not possible directly. maybe convert to LAB or HSV, apply clahe on L and convert back. and you can use it from opencv, too.berak
@berak thanks and your comment could be an answerMickey Shine
^^ yes, B. that's where i got the idea.berak

2 Answers

45
votes

Conversion of RGB to LAB(L for lightness and a and b for the color opponents green–red and blue–yellow) will do the work. Apply CLAHE to the converted image in LAB format to only Lightness component and convert back the image to RGB. Here is the snippet.

bgr = cv2.imread(image_path)

lab = cv2.cvtColor(bgr, cv2.COLOR_BGR2LAB)

lab_planes = cv2.split(lab)

clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(gridsize,gridsize))

lab_planes[0] = clahe.apply(lab_planes[0])

lab = cv2.merge(lab_planes)

bgr = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)

bgr is the final RGB image obtained after applying CLAHE.

1
votes

this for c#, apply clahe for rgb images.

    private static Image<Bgr, byte> appy_CLAHE( Image<Bgr, byte> imgIn , int clipLimit=2, int tileSize=25)
    {
        var imglabcol = new Image<Lab, byte>(imgIn.Size);
        var imgoutL = new Image<Gray, byte>(imgIn.Size);  

        var imgoutBGR = new Image<Bgr, byte>(imgIn.Size);  

        //clahe filter must be applied on luminance channel or grayscale image
        CvInvoke.CvtColor(imgIn, imglabcol, ColorConversion.Bgr2Lab, 0); 

        CvInvoke.CLAHE(imglabcol[0], clipLimit, new Size(tileSize, tileSize), imgoutL);
        imglabcol[0] = imgoutL; // write clahe results on Lum channel into image

        CvInvoke.CvtColor(imglabcol, imgoutBGR, ColorConversion.Lab2Bgr, 0);

        return imgoutBGR;
    }