Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

CLAHE for color image OpenCV 3.0

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

CLAHE for color image OpenCV 3.0

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:

// 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();
clahe->setClipLimit(4);
cv::Mat dst;
clahe->apply(lab_planes[0], dst);

// Merge the the color planes back into an Lab image
dst.copyTo(lab_planes[0]);
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);

waitKey(0);
return 0;

CLAHE for color image OpenCV 3.0

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: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();
clahe->setClipLimit(4);
cv::Mat dst;
clahe->apply(lab_planes[0], dst);

// Merge the the color planes back into an Lab image
dst.copyTo(lab_planes[0]);
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);

waitKey(0);
return 0;