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

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.

Leave a Comment