Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

calcHist not returning expected values?

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)
cv2.waitKey(0)
hist = cv2.calcHist(diff2_img, [0], None, [256], [0, 256])
for s in hist:
  print s

I used the image at http://abload.de/img/result2vkw7.png 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])
plt.show()

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?

calcHist not returning expected values?

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)
cv2.waitKey(0)
hist = cv2.calcHist(diff2_img, [0], None, [256], [0, 256])
for s in hist:
  print s

I used the image at http://abload.de/img/result2vkw7.png 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])
plt.show()

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?wrong?