Ask Your Question

CLAHE for color image OpenCV 3.0

asked 2016-02-01 19:47:55 -0500

Paul2208 gravatar image

updated 2016-02-01 21:38:31 -0500

Hello all.

I am trying to use clahe to "adjust" (enhance the local contrast of) an colored image for further segmentation purposes. I found this code use of clahe but it only works for grayscale image (even though I change the imread function parameter to CV_LOAD_IMAGE_UNCHANGED). Can anyone suggest a code can be used for color image?

I have seen some codes splinting the color image to Lab space then merging it back to rgb Here but "split" doesn't work and I do not know what is the problem with split (since it apperently works in the last link I provided above). Any suggestion?

Here is the error I am getting when using split : Unhandled exception at 0x018EFBFE (opencv_world300.dll) in Architecture_building.exe: 0xC0000005: Access violation writing location 0x2AB9BC70

Here is what I tried (image attachedC:\fakepath\Mypic.jpg)

// read and open an image
Mat img = imread("Mypic.jpg");

if (img.empty())
    cout << "Error : Image cannot be loaded..." << endl;
    return -1;  

namedWindow("Original Image", CV_WINDOW_AUTOSIZE); //create a window with the name "Original Image"
imshow("Original Image",img); //display the image

// convert the RGB color image to Lab
Mat lab_image;
cvtColor(img, lab_image, CV_BGR2Lab);

namedWindow("Lab Image", WINDOW_AUTOSIZE);
imshow("Lab Image",lab_image);

// Extract the L channel
vector<Mat> lab_planes(3);
split(lab_image, lab_planes);  // now we have the L image in lab_planes[0] 
cv::extractChannel(lab_image, lab_planes, 0);

namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
imshow("L channel Image",lab_planes[0]);

// apply the CLAHE algorithm to the L channel
cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE();
cv::Mat dst;
clahe->apply(lab_planes[0], dst);

// Merge the the color planes back into an Lab image
cv::merge(lab_planes, lab_image);

// convert back to RGB
 cv::Mat image_clahe;
 cv::cvtColor(lab_image, image_clahe, CV_Lab2BGR);

 // display the results  
 cv::imshow("image CLAHE", image_clahe);

return 0;
edit retag flag offensive close merge delete



yes, for colour images you will have to convert to something like lab or hsv, then apply clahe only on the luminance channel.

can you show us, what you tried ?

berak gravatar imageberak ( 2016-02-01 21:05:09 -0500 )edit

Hi Berak. I just updated the post. Please check what I tried

Paul2208 gravatar imagePaul2208 ( 2016-02-01 21:36:51 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-02-01 21:52:26 -0500

berak gravatar image

you code works fine, if you comment the

cv::extractChannel(lab_image, lab_planes, 0);


(you don't even need it, and the result of extractChannel should be a single channel Mat, not a vector<Mat>)

also, you can apply clahe "in-place", and save a copy:

clahe->apply(lab_planes[0], lab_planes[0]);
edit flag offensive delete link more


Ok, thanks berak.

Paul2208 gravatar imagePaul2208 ( 2016-02-02 14:31:01 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-02-01 17:31:14 -0500

Seen: 1,983 times

Last updated: Feb 01 '16