Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion failed (blockSize % 2 == 1 && blockSize > 1) in cv::adaptiveThreshold

Tags:

c++

opencv

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?

like image 509
sashoalm Avatar asked Dec 25 '22 01:12

sashoalm


1 Answers

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".

like image 67
sashoalm Avatar answered Dec 28 '22 20:12

sashoalm