20

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()
kot09
  • 987
  • 3
  • 10
  • 18
  • Is your image using a bitdepth other than 8bit? if so you'll need to convert it to 8bit to display it. – zeFrenchy Nov 17 '16 at 09:46
  • img.dtype gives me uint8 @zeFrenchy – kot09 Nov 22 '16 at 16:12
  • I hope you mean the image is grayscale, not actually black and white, ortherwise normalizing will do nothing. Does it work with NORM_L2? – zeFrenchy Nov 22 '16 at 16:19
  • Sorry if my terms are not correct. What I meant to say is that my image is binarized. the pixel values are either (0, 0, 0) or (255, 255, 255). I am not sure if that is part of grayscale; in class, the prof told us there was a difference between grayscale and binarized – kot09 Nov 22 '16 at 16:24

3 Answers3

19

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()
Ophir Carmi
  • 2,701
  • 1
  • 23
  • 42
  • 1
    How do you normalize all the image set? Should you regard on each image by itself or one normalization over all images? – Jürgen K. Sep 24 '19 at 11:34
  • It depends if "alpha" and/or "beta" are variables or fixed values. if they are variables (like the over all min and max of the image set values) - over all images (but you need to calculate alpha and beta in advance). if they are fixed - each image by itself. – Ophir Carmi Sep 25 '19 at 12:21
  • 1
    if dst is returned by cv2.normalize as a output parameter, why does dst is also taken as input parameter? @OphirCarmi – Karthick S Jul 20 '20 at 06:45
  • this is a good question, maybe because of the c++ api of this function – Ophir Carmi Apr 06 '21 at 10:50
9

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
João Cartucho
  • 3,688
  • 32
  • 39
2

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.

rocklegend
  • 81
  • 11