I am trying to find the histogram values of an image by using my own function but when i run my code it prints the histogram values like [1.000e+00 4.000e+00 1.000e+00 8.000e+00 8.000e+00 2.500e+01 2.100e+01 4.500e+01 5.500e+01 8.800e+01 1.110e+02 1.220e+02 1.280e+02 1.370e+02 Is it normal or is there any other method that i can display histogram values in an understandable way? Here is my function;
import numpy as np import cv2 def histogram(img): height = img.shape[0] width = img.shape[1] hist = np.zeros((256)) for i in np.arange(height): for j in np.arange(width): a = img.item(i,j) hist[a] += 1 print(hist) img = cv2.imread('rose.jpg', cv2.IMREAD_GRAYSCALE) histogram(img)
Advertisement
Answer
Where you initialize your histogram, set its type to np.uint32
or similar since you can only ever have a whole, non-negative number of pixels of a given colour:
hist = np.zeros(256, dtype=np.uint32)
Check the type of your current array and find it is float64
with:
print(hist.dtype)
Hint: See also here.