user5275692 user5275692 - 5 months ago 32
Python Question

Opencv python incorrect blocksize

def adaptive_gaussian_thresholding(self, filename):
im = cv2.imread(filename, 0)
filtered_image = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 3, cv2.THRESH_BINARY, 3, 0)
cv2.imwrite(filename, filtered_image)



filtered_image = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 3, cv2.THRESH_BINARY, 3, 0)
error: /root/opencv/modules/imgproc/src/thresh.cpp:1287: error: (-215) blockSize % 2 == 1 && blockSize > 1 in function adaptiveThreshold



The blocksize or tile size is the number of pixels^2 to be used for the kernel that is passed to the procedure. It needs to be an odd number. I am unsure if this is a bug or something else is wrong?

Official documentation

Answer

You are calling adaptiveThreshold with an extra argument. You have incorrectly placed a 3 in between the adaptiveMethod and thresholdType arguments. In your invocation, the cv2.THRESH_BINARY flag is in the position expected for the blockSize value. Incidentally, cv2.THRESH_BINARY evaluates to zero, hence the error you see. The correct way to call your function is:

filtered_image = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 3, 0)
Comments