Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic calculation of low and high thresholds for the Canny operation in opencv

In OpenCV, the low and high thresholds for the canny operator are mandatory:

cvCanny(input,output,thresh1,thresh2) 

In Matlab, there's an option to calculate those automatically:

edge(input,'canny') 

I've looked into Matlab's code for edge, and this is really not straight forward to calculate those automatically.

Are you aware of any implementation of the canny operator along with automatic threshold calculation for OpenCV?

like image 448
hoffer Avatar asked Nov 27 '10 15:11

hoffer


People also ask

What is threshold in cv2 Canny?

Canny does use two thresholds (upper and lower): If a pixel gradient is higher than the upper threshold, the pixel is accepted as an edge. If a pixel gradient value is below the lower threshold, then it is rejected.

What are threshold values in Canny edge?

The 'Canny' method uses two thresholds. For example, if the threshold is [0.1 0.15] then the edge pixels above the upper limit(0.15) are considered and edge pixels below the threshold(0.1) are discarded. Now, you may have a question "what about the pixels in between upper and lower threshold"?

How do we choose optimal Canny edge detection parameters?

The Canny edge detection algorithm can be broken down into 5 steps: Step 1: Smooth the image using a Gaussian filter to remove high frequency noise. Step 2: Compute the gradient intensity representations of the image. Step 3: Apply non-maximum suppression to remove “false” responses to to edge detection.

How does OpenCV Canny work?

Canny() Function in OpenCV is used to detect the edges in an image. Where: Image: Input image to which Canny filter will be applied. T_lower: Lower threshold value in Hysteresis Thresholding.


1 Answers

I stumbled upon this answer while I was searching for a way to automatically compute Canny's threshold values.

Hope this helps anyone who comes looking for a good method for determining automatic threshold values for Canny's algorithm...


If your image consists of distinct foreground and background, then the edge of foreground object can use extracted by following:

  1. Calculate Otsu's threshold using:

    double otsu_thresh_val = cv::threshold(     orig_img, _img, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU ); 

    We don't need the _img. We are interested in only the otsu_thresh_val but unfortunately, currently there is no method in OpenCV which allows you to compute only the threshold value.

  2. Use the Otsu's threshold value as higher threshold and half of the same as the lower threshold for Canny's algorithm.

    double high_thresh_val  = otsu_thresh_val,        lower_thresh_val = otsu_thresh_val * 0.5; cv::Canny( orig_img, cannyOP, lower_thresh_val, high_thresh_val ); 

More information related to this can be found in this paper: The Study on An Application of Otsu Method in Canny Operator. An explaination of Otsu's implementation can be found here.

like image 180
VP. Avatar answered Sep 28 '22 06:09

VP.