How to apply CLAHE on RGB color images
Asked Answered
K

3

35

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?

Kulturkampf answered 29/7, 2014 at 4:54 Comment(5)
not possible directly. maybe convert to LAB or HSV, apply clahe on L and convert back. and you can use it from opencv, too.Yttria
@Yttria thanks and your comment could be an answerKulturkampf
possible duplicate of simple illumination correction in images openCV c++Wun
^^ yes, B. that's where i got the idea.Yttria
This link will help you #24341614Markel
Q
60

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.

Quasi answered 18/11, 2017 at 20:18 Comment(3)
cv2.split isn't required as OpenCV in Python uses NumPy arrays. Once you create the CLAHE object, just do lab[...,0] = clahe.apply(lab[...,0]). You can also remove cv2.mergeMelodrama
This method sometimes gives bad results. See point 4 of #56906092. Would you have any idea @NikhilVM?Passementerie
@Passementerie One has to try varying the parameters clipLimit and tileGridSize. In the question linked above, the OP has used grid size of 100.Gotten
M
5
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_RGB2Lab)

#configure CLAHE
clahe = cv2.createCLAHE(clipLimit=10,tileGridSize=(8,8))

#0 to 'L' channel, 1 to 'a' channel, and 2 to 'b' channel
img[:,:,0] = clahe.apply(img[:,:,0])

img = cv2.cvtColor(img, cv2.COLOR_Lab2RGB)

cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Macfadyn answered 6/7, 2022 at 15:48 Comment(0)
D
2

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;
    }
Decoration answered 1/6, 2021 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.