Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalizing images in OpenCV

I wrote the following code to normalize an image using NORM_L1 in OpenCV. But the output image was just black. How to solve this?

import cv2 import numpy as np import Image  img = cv2.imread('img7.jpg') gray_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) a = np.asarray(gray_image)   dst = np.zeros(shape=(5,2))  b=cv2.normalize(a,dst,0,255,cv2.NORM_L1)   im = Image.fromarray(b)  im.save("img50.jpg")  cv2.waitKey(0) cv2.destroyAllWindows() 
like image 536
N.Chandimali Avatar asked Jun 25 '16 06:06

N.Chandimali


People also ask

How do I normalize an image in Python OpenCV?

If you want to change the range to [0, 1], make sure the output data type is float . Python required me to specify a dst input parameter. In this case, you may want to initialize norm_image to a copy of image and pass that in as dst.

What is normalization in images?

In image processing, normalization is a process that changes the range of pixel intensity values. Applications include photographs with poor contrast due to glare, for example. Normalization is sometimes called contrast stretching or histogram stretching.

How do you normalize an image in Python?

Use the normalize() Function of OpenCV to Normalize an Image in Python. Normalization in image processing is used to change the intensity level of pixels. It is used to get better contrast in images with poor contrast due to glare. We can use the normalize() function of OpenCV to normalize an image.

What does cv2 normalize do?

In the end, we can conclude that cv2 normalize() helps us by changing the pixel intensity and increasing the overall contrast.


2 Answers

If you want to change the range to [0, 1], make sure the output data type is float.

image = cv2.imread("lenacolor512.tiff", cv2.IMREAD_COLOR)  # uint8 image norm_image = cv2.normalize(image, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) 
like image 188
Sounak Avatar answered Oct 16 '22 10:10

Sounak


When you normalize a matrix using NORM_L1, you are dividing every pixel value by the sum of absolute values of all the pixels in the image. As a result, all pixel values become much less than 1 and you get a black image. Try NORM_MINMAX instead of NORM_L1.

like image 28
Andrey Smorodov Avatar answered Oct 16 '22 08:10

Andrey Smorodov