Blue tint in image, how to adjust blue in OpenCV

Hi there,

I have an image in a cv::Mat object, after debayering/demosaicking and applying color reproduction, then increasing contrast and brightness, I end up with a good image although it’s very blue.

Searching for this usually gives answers like the white balance needs adjusting, convert to HSV then adjust one color within ranges, but none of the example code I have tried works out. Converting to HSV gives me a very red image. Trying some of the white balancing and color balancing code gives a result on half the image or just changes its color again.

I am using OpenCV 4.5.1 with C++. I am not sure of the color space or any other technicalities of the image (I am sure this is not helping me).

What I am looking for is white balancing that works with CV_32F or CV_16U Mat types (if that’s what I need) or some way of adjusting just the blue to take some of it out so I can inspect the result and check whether it looks correct.

Thanks again

Can you post an example?
You might have one of two problems:

  • either you chose an incorrect demosaic algorithm, which can switch red and blue channels, resulting in strange colors. Try to photograph a page with basic colors and see if the colors are correct. Otherwise try other demosaicing algorithms (BayerRG2RGB, BayerBG2RGB…)

  • or, as you said, the white balance is bad.
    This might already be corrected in the camera (refer to the camera API or use CAP_PROP_WB_TEMPERATURE or CAP_PROP_AUTO_WB if using the cv2.VideoCapture class).
    Otherwise multiply each channel with a constant. e.g. take a photo of a white paper sheet and measure the RGB values of the neutral color. Let’s say it’s R=150 G=180 B=200. To correct the white balance (python):

    im[:,:,0] = im[:,:,0]/200*150 #blue channel
    im[:,:,1] = im[:,:,1]/180*150 #green channel
    

    Now the white level should be correct.

Note: The HSV is a different color space, it’s only for image processing, not for diplay!