简体   繁体   中英

Fast Adaptive Threshold for Canny Edge Detector in Android

According to my research, Canny Edge Detector is very useful for detecting the edge of an image. After I put many effort on it, I found that OpenCV function can do that, which is

    Imgproc.Canny(Mat image, Mat edges, double threshold1, double threshold2)

But for the low threshold and high threshold, I know that different image has different threshold, so can I know if there are any fast adaptive threshold method can automatically assign the low and high threshold according to different image?

This is relatively easy to do. Check out this older SO post on the subject.

A quick way is to compute the mean and standard deviation of the current image and apply +/- one standard deviation to the image.

The example in C++ would be something like:

Mat img = ...;
Scalar mu, sigma;
meanStdDev(img, mu, sigma);

Mat edges;
Canny(img, edges, mu.val[0] - sigma.val[0], mu.val[0] + sigma.val[0]);

Another method is to compute the median of the image and target a ratio above and below the median (eg, 0.66*medianValue and 1.33*medianValue ).

Hope that helps!

Opencv has an adaptive threshold function. With OpenCV4Android it is like this:

Imgproc.adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C);

An example:

Imgproc.adaptiveThreshold(mInput, mInput, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 4);

As for how to choose the parameters, you have to read the docs for more details. Choosing the right threshold for each image is a whole different question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM