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;
JavaScript
x
19
19
1
import numpy as np
2
import cv2
3
4
def histogram(img):
5
height = img.shape[0]
6
width = img.shape[1]
7
8
hist = np.zeros((256))
9
10
for i in np.arange(height):
11
for j in np.arange(width):
12
a = img.item(i,j)
13
hist[a] += 1
14
15
print(hist)
16
17
img = cv2.imread('rose.jpg', cv2.IMREAD_GRAYSCALE)
18
histogram(img)
19
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:
JavaScript
1
2
1
hist = np.zeros(256, dtype=np.uint32)
2
Check the type of your current array and find it is float64
with:
JavaScript
1
2
1
print(hist.dtype)
2
Hint: See also here.