# RGB image normalization

Hi guys,
I am currently trying to normalize an RGB image coming from the onboard camera to delete the brightness influence, but all I get is a black image.

Here is the code of my function:

``````def normalization(img):

for i in xrange(3):
img[:,:,i]=np.absolute((img[:,:,i]-np.min(img[:,:,i]))/(np.max(img[:,:,i])-np.min(img[:,:,i])))*255

return img
``````

If someone could tell me why it’s wrong i would appreciate.
Thank you

Vincent

Hello, thanks for reaching out!

I wasn’t able to reproduce this result executing the code that you provided. Perhaps there is some discrepancy in how I tested

(a) What is the data type of the image that you’re providing to the normalization function?
(b) How do you determine that the image is ‘black’? Ie, are you displaying the image using some other function, or printing values, etc.

Also, depending on your application it might be worth considering using an existing method for balancing the image, like histogram equalization. OpenCV provides a function that accomplishes this

https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/histogram_equalization/histogram_equalization.html

Thanks!

Hi jaybdub,
what do you get when you’re using my function ?

(a): I just use cv2.VideoCapture and then cap.read() to get a frame that I put in the function call. If I’m right it’s a numpy array.

(b): I show the image out of the function using imshow().

Okay I’ll have a look on the method and try it.

Actually, for uint8 datatypes the function you provided will quantize the values to either 0 or 255.

OpenCV also has a function that I would recommend using so you don’t have to explicitly typecast your image.

``````image = cv2.normalize(image, None, 0, 255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC3)
``````

For more information on this function you can call the following in a Python interpreter.

``````help(cv2.normalize)
``````

Be also sure not (and avoid) to provide a frame where min = max, dividing by zero may lead to problems…

I already tried this function, but it seems to me it only normalize intensity…(value part of HSV) and what I need is to normalize through the 3 colors RGB…

Yeah sure, thanks

Hi,
I’m still struggling normalizing my image, it feels like the cv2.normalize isn’t what I’m looking for.

I found this procedure but I can’t make it work is there something wrong in it ?
http://www.aishack.in/tutorials/normalized-rgb/

Thanks

You may try to look at min and max of your image with cv2.minMaxLoc.
You might find that min is 0 and max 255, so it doesn’t rescale.
Think about dead and hot pixels. So you may use some filtering before.

You may also use cv2.split for checking each BGR component, and apply cv2.normalize on each and then merge these again into your output BGR frame.

I checked and yes I have my S image (sum of B,G and R) that has some 0 and 255 values…

Firstly I don’t get why I get some 0 when the min of B,G and R is non-zero and the only thing I do is S=B+G+R

Secondly I tried to use cv2.inRange to create a mask to delete the 0 value and applied it to the S image but I still get 0 values…

I’m really lost there…