Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV error with AdaptiveThreshold

Tags:

python

opencv

I am trying to capture the video through openCV and then applying Adaptive thresholding on the video (to convert to black and white). The problem is this code throws up error very frequently. I am playing around with the last two numbers in the adaptive threshold function.

cv2.error: /tmp/opencv-UA2sOU/opencv-2.4.9/modules/imgproc/src/thresh.cpp:797: error: (-215) blockSize % 2 == 1 && blockSize > 1 in function adaptiveThreshold

I can't seem to understand what causes this issue.

import numpy as np
import cv2
import pylab as pl

cap = cv2.VideoCapture(0)


# take first frame of the video
ret,frame = cap.read()

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read() 
    thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,40,15)

    cv2.imshow('frame',thresh)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        #cv2.imwrite("snap.jpg", thresh)
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
like image 307
pbu Avatar asked Aug 10 '15 13:08

pbu


1 Answers

cv2.adaptiveThreshold requires an odd blockSize. So you'd need to use 39 or 41 instead of 40.

blockSize – Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.

This is indicated by the error message: It says "blockSize % 2 == 1", which means blockSize is not divisible by 2.

like image 188
Falko Avatar answered Sep 22 '22 20:09

Falko