Home > Software design >  OpenCV AdaptiveTreshold Error: blockSize % 2 == 1 && blockSize > 1 in function 'adaptiveThre
OpenCV AdaptiveTreshold Error: blockSize % 2 == 1 && blockSize > 1 in function 'adaptiveThre

Time:07-23

I am trying to use adaptiveTreshold, but I get error regarding blockSize. I know it should be odd and > 0, it is set 11 but it still shows this error. What could be wrong?

treshold = 115
maxValue = 255 
adaptiveMethod = cv.ADAPTIVE_THRESH_GAUSSIAN_C
thresholdType = cv.THRESH_BINARY
blockSize = 11
C = 2
            
im = cv.adaptiveThreshold(img, treshold, maxValue, adaptiveMethod, thresholdType, blockSize, C)
            
plt.imsave(save_path, im, cmap = 'gray')

error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/thresh.cpp:1675: error: (-215:Assertion failed) blockSize % 2 == 1 && blockSize > 1 in function 'adaptiveThreshold

CodePudding user response:

What's wrong?

The function expects an odd numbered kernel. But you have passed the variable thresholdType = cv.THRESH_BINARY in place of blockSize. Since the corresponding integer value of cv.THRESH_BINARY is 0, this error is raised.

What does the error mean?

It means both the following conditions must be satisfied:

  1. blockSize must be an odd number
  2. blockSize must be greater than 1

In other words, blockSize must be 3 or any odd number above 3.

How to correct it?

Remove the unwanted treshold:

cv.adaptiveThreshold(img, maxValue, adaptiveMethod, thresholdType, blockSize, C)

CodePudding user response:

adaptiveThreshold does not take a threshold argument. Your call should be

im = cv.adaptiveThreshold(img, maxValue, adaptiveMethod, thresholdType, blockSize, C)
 
  • Related