calcHist not returning expected values?

asked 2014-12-06 03:42:28 -0600

mcsmurf gravatar image

Hi, I'm trying to calculate a histogram with OpenCV 2.4.10 with Python on Windows. I use this code to show the image, calculate and print the histogram:

diff2_img = cv2.imread("result.png", cv2.CV_LOAD_IMAGE_GRAYSCALE)
cv2.imshow("cropped", diff2_img)
hist = cv2.calcHist(diff2_img, [0], None, [256], [0, 256])
for s in hist:
  print s

I used the image at to test this. But somehow only one entry in the array is !=0, it has a value of "[ 800.]" (is this the numbers of pixels...?). I would have expected two entries !=0, the one for the white area and the one for the gray area. When I use

from matplotlib import pyplot as plt
plt.hist(diff2_img.ravel(), 256, [0, 256])

for comparision, it looks fine (two bars: A small one for the gray area, a big one for the white area). Is my code above wrong?

edit retag flag offensive close merge delete