I am working on a project with OCR using a webcam. I need to know the number of black pixels present in an image, so I tried to use the cv2.calcHist
function but it causes python to stop when I execute the code. If I comment lines 13 and 14, the code works fine. I have implemented the function as shown in the documentation, so I do not know what is wrong.
I am using python 2.7 in Windows 7. Here is the code:
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
while(1):
ret, pic = cap.read()
cv2.imshow('Webcam', pic)
img = cv2.cvtColor(pic, cv2.COLOR_BGR2GRAY)
pic2 = img.copy()
blur = cv2.GaussianBlur(pic2, (5, 5), 0)
ret3, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
hist = np.bincount(th3.ravel(), minlength=256)
print "# black pixels: ", hist[0]
cv2.imshow('th3', th3)
if cv2.waitKey(1) & 0xFF == ord('x'):
break
cap.release()
cv2.destroyAllWindows()
Thanks in advance!