问题描述
在openCV中,canny运算符的低和高阈值是强制性的:
In openCV, the low and high thresholds for the canny operator are mandatory:
cvCanny(input,output,thresh1,thresh2)
在Matlab中,有一个自动计算的选项:
In Matlab, there's an option to calculate those automatically:
edge(input,'canny')
我查看了Matlab的代码代码,这实际上并不是直接自动计算的。
I've looked into Matlab's code for edge, and this is really not straight forward to calculate those automatically.
你知道canny运算符的任何实现吗?以及opencv的自动阈值计算?
Are you aware of any implementation of the canny operator along with automatic threshold calculation for opencv?
谢谢
推荐答案
我当我在寻找一种自动计算Canny阈值的方法时偶然发现了这个答案。
I stumbled upon this answer while I was searching for a way to automatically compute Canny's threshold values.
希望这有助于任何寻找确定自动阈值的好方法的人Canny的算法...
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:
-
使用以下方法计算Otsu的阈值:
Calculate Otsu's threshold using:
double otsu_thresh_val = cv::threshold(
orig_img, _img, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU
);
我们不需要 _img
。我们只对 otsu_thresh_val
感兴趣,但不幸的是,目前OpenCV中没有允许您只计算阈值的方法。
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.
使用Otsu的阈值作为更高的阈值,使用Canny算法的下限阈值的一半。
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 );
有关此内容的更多信息,请参见。可以在找到对Otsu实施的解释。
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.
这篇关于在opencv中自动计算Canny操作的低和高阈值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!