Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Python: Normalize image

Tags:

python

opencv

I'm new to OpenCV. I want to do some preprocessing related to normalization. I want to normalize my image to a certain size. The result of the following code gives me a black image. Can someone point me to what exactly am I doing wrong? The image I am inputting is a black/white image

import cv2 as cv
import numpy as np

img = cv.imread(path)
normalizedImg = np.zeros((800, 800))
cv.normalize(img,  normalizedImg, 0, 255, cv.NORM_MINMAX)
cv.imshow('dst_rt', self.normalizedImg)
cv.waitKey(0)
cv.destroyAllWindows()
like image 260
kot09 Avatar asked Nov 17 '16 02:11

kot09


People also ask

How do I normalize an image in OpenCV Python?

We can use the normalize() function of OpenCV to normalize an image. The normalize() function's first argument is the source image that we want to normalize. The second argument is the destination image, creating an output image with our desired dimensions or size.

What is normalize in OpenCV?

Working of normalize() function in OpenCVThe process in which we modify the intensity values of pixels in a given image to make the image more appealing to the senses is called normalization of the image.

What does cv2 normalize do?

It takes an array in as an input and normalizes its values between 0 and 1. It then returns an output array with the same dimensions as the input.


3 Answers

as one can see at: http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#cv2.normalize, there is a → dst that say that the result of the normalize function is returned as output parameter. The function doesn't change the input parameter dst in-place. (The self. in cv.imshow('dst_rt', self.normalizedImg) line is a typo)

import cv2 as cv
import numpy as np
path = r"C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg"
img = cv.imread(path)
normalizedImg = np.zeros((800, 800))
normalizedImg = cv.normalize(img,  normalizedImg, 0, 255, cv.NORM_MINMAX)
cv.imshow('dst_rt', normalizedImg)
cv.waitKey(0)
cv.destroyAllWindows()
like image 136
Ophir Carmi Avatar answered Oct 28 '22 15:10

Ophir Carmi


It's giving you a black image because you are probably using different sizes in img and normalizedImg.

import cv2 as cv

img = cv.imread(path)
img = cv.resize(img, (800, 800))
cv.normalize(img, img, 0, 255, cv.NORM_MINMAX)

cv.imshow('dst_rt', img)
cv.waitKey(0)
cv.destroyAllWindows()

Update: In NumPy there are more intuitive ways to do this ref:

a = np.random.rand(3,2)

# Normalised [0,1]
b = (a - np.min(a))/np.ptp(a)

# Normalised [0,255] as integer: don't forget the parenthesis before astype(int)
c = (255*(a - np.min(a))/np.ptp(a)).astype(int)        

# Normalised [-1,1]
d = 2.*(a - np.min(a))/np.ptp(a)-1
like image 8
João Cartucho Avatar answered Oct 28 '22 15:10

João Cartucho


When you call cv.imshow() you use self.normalizedImg, instead of normalizedImg.

The self. is used to identify class members and its use in the code you've written is not appropriate. It shouldn't even run as written. However I assume this code has been extracted from a class definition, but you must be consistent in naming variables and self.normalizedImg is different from normalizedImg.

like image 2
rocklegend Avatar answered Oct 28 '22 15:10

rocklegend