Fast Adaptive Threshold for Canny Edge Detector in Android

10,091

Solution 1

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 (e.g., 0.66*medianValue and 1.33*medianValue).

Hope that helps!

Solution 2

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.

Share:
10,091
Hua Er Lim
Author by

Hua Er Lim

Updated on June 15, 2022

Comments

  • Hua Er Lim
    Hua Er Lim almost 2 years

    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?