user5275692
user5275692

Reputation: 603

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

Upvotes: 1

Views: 340

Answers (2)

Aurelius
Aurelius

Reputation: 11359

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)

Upvotes: 4

Georges
Georges

Reputation: 245

If you do call the help() for that function in python, the call needs to respect the following form:

Help on built-in function adaptiveThreshold in module cv2:

adaptiveThreshold(...)
    adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) 

The value 3 between the adaptiveMethod and thresholdType in your call should be omitted.

And C is subtracted from the mean or weighted mean calculated maybe insert a value e.g. 2.

example call: mycall= cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY, 11, 2)

Upvotes: 1

Related Questions