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
块大小或平铺大小是要用于传递给过程的内核的像素数^ 2。它需要是一个奇数。我不确定这是一个错误还是别的错误?
答案 0 :(得分:4)
您正在使用额外参数调用adaptiveThreshold
。您在3
和adaptiveMethod
参数之间错误地放置了thresholdType
。在您的调用中,cv2.THRESH_BINARY
标志位于blockSize
值的预期位置。顺便提一下,cv2.THRESH_BINARY
的计算结果为零,因此您看到错误。调用函数的正确方法是:
filtered_image = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 3, 0)
答案 1 :(得分:1)
如果您在python中为该函数调用help()
,则调用需要遵循以下形式:
Help on built-in function adaptiveThreshold in module cv2:
adaptiveThreshold(...)
adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst])
您的通话中3
和adaptiveMethod
之间的值thresholdType
应省略。
从平均值或加权平均值中减去C
,可以插入值e.g. 2
。
示例电话:
mycall= cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY, 11, 2)