I'm trying to do an adaptive threshold:
cv::Mat mat = cv::imread(inputFile);
cv::cvtColor(mat, mat, CV_BGR2GRAY);
cv::adaptiveThreshold(mat, mat, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 20, 0);
cv::imwrite(outputFile, mat);
But it fails with this message:
OpenCV Error: Assertion failed (blockSize % 2 == 1 && blockSize > 1) in cv::adaptiveThreshold, file ..\..\..\..\opencv\modules\imgproc\src\thresh.cpp, line 797
What is the problem?
The problem was that I was putting an even value for blockSize, while it required only odd values, so changing it from 20 to 21 fixed the assert failure:
cv::adaptiveThreshold(mat, mat, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 21, 0);
The docs kind of mention it, but they are not explicit it will fail if blockSize is not odd:
blockSize – Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
As you can see, nowhere does it say "it will fail if blockSize is not odd".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With