cvtColor hls to rgb lightening image

asked 2019-09-09 09:15:51 -0500

enxdtw gravatar image

updated 2019-09-10 02:19:43 -0500


I am trying to lighten an image by converting to HLS and then adjusting the lightness channel

cv::Mat hls;
cv::Mat planes[3];

cv::cvtColor(frame, hls, cv::ColorConversionCodes::COLOR_BGR2HLS);

cv::split(hls, planes);

   cv::Mat intensity = planes[1];

   blur(intensity, intensity, cv::Size(100, 100));

   // make sure lightness is within 0->1 bounds
intensity.forEach<float>([&](float& pixel, const int po[]) -> void {
    pixel = fmin(1.0, fmax(0.0, pixel));

   planes[1] = intensity;

cv::merge(planes, 3, hls);

// make BGR image to display
cv::cvtColor(hls, frame, cv::ColorConversionCodes::COLOR_HLS2BGR);

The problem is though that, after it has been lightened by the planes[1] += 50; call, the image has had it's colours changed and the darker pixels in the image now appear green, the other pixels do seem to be lightened though (the original image was a dark monotone image).

Any idea why this is happening and how I can get round this problem, I think it must be something to do with the Chroma component of the HLS-RGB conversion scheme?




edit retag flag offensive close merge delete


Can you share the original image? I just tried your code with an image and it worked as expected.

Witek gravatar imageWitek ( 2019-09-09 09:36:44 -0500 )edit

Can you see the image link? Maybe the brightness effect is not so visible with +50, with +150 offset the effect is more obvious

enxdtw gravatar imageenxdtw ( 2019-09-09 10:01:30 -0500 )edit

Well, the dark parts have green hue which is hardly visible in the original image. After the brightness increase it becomes more apparent. I don't really see any "new" colors in the lighter image.

Witek gravatar imageWitek ( 2019-09-09 10:09:25 -0500 )edit

Now I'm doing a blur to the Lightness image, I have modified the code accordingly. I also used my own HLS2RGB functions and get the same result. There must be something in the HLS-RGB conversion algorithm that makes that colours change (or enhanced) if the lightness value changes.

enxdtw gravatar imageenxdtw ( 2019-09-10 02:18:45 -0500 )edit

I think I know what's going on here. The smoothing operation (or the lightening operation) takes the darker pixels and lightens them. When they are dark the green hue of them is not noticeable, but when lightened the green hue becomes dominant. So it isn't a problem with the methods it is more a problem with my ignorance of the hue component of darker pixels.

enxdtw gravatar imageenxdtw ( 2019-09-10 02:30:07 -0500 )edit

This is exactly what I meant in my comment above.

Witek gravatar imageWitek ( 2019-09-10 03:09:57 -0500 )edit