Ask Your Question

calcHist not returning expected values?

asked 2014-12-06 03:44:58 -0500

mcsmurf gravatar image

updated 2014-12-25 16:56:36 -0500

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

1 answer

Sort by ยป oldest newest most voted

answered 2014-12-27 10:41:21 -0500

Guanta gravatar image

Just a note: in python you can use numpy's histogram function which is much easier to use and has also more functionality.

edit flag offensive delete link more

Question Tools


Asked: 2014-12-06 03:44:58 -0500

Seen: 326 times

Last updated: Dec 25 '14