cv2.calcHist causes error "pythonw.exe has stopped working"

0

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!

    
asked by Alex Ortega 05.02.2017 в 17:47
source

2 answers

0

I solved the problem using the function that provides numpy as recommended by the documentation although it takes 4 times more than the function that provides opencv (question edited with the new code).

The implemented function is this:

hist = np.bincount(th3.ravel(), minlength=256)
    
answered by 06.02.2017 / 07:29
source
0

In this tutorial you can see how to calculate and graph histograms of images: link

    
answered by 05.02.2017 в 22:01