Why is the Canny edge detection in MATLAB different to OpenCV?

28 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2019 年 4 月 22 日
回答済み: MathWorks Support Team 2019 年 4 月 24 日
When I am comparing the outputs of using the "edge" function in MATLAB to the "Canny" function in OpenCV, I notice that the OpenCV implementation filters out more edges compared to the MATLAB's implementation.
In MATLAB, I use:
>> edge(I, 'canny', [], 2.0)
In OpenCV (C++), I use:
>> cv::Canny(gray_image_smooth, edge, lowThresh, highThresh);
where I have tried to set the low and high thresholds using a manner similar to what is given in the "edge" function.

採用された回答

MathWorks Support Team
MathWorks Support Team 2019 年 4 月 24 日
The 'canny' option in the MATLAB edge function attempts to be as precisely faithful as possible to the algorithm described in Canny's Masters Thesis and PAMI paper:
There are several notable differences between the algorithm in "edge" and what appears in OpenCV:
1) Smoothing of input image prior to computation of gradients.
We use a 16x16 gaussian filter with sigma sqrt(2) prior to computing gradients. OpenCV does not do this step.
2) In the gradient computation step, we use a Derivative of Gaussian filter. OpenCV uses the Sobel operator.
3) Our non-maximum suppression step is different than that of OpenCV and is faithful to the algorithm described in the Canny paper.
4) Determining hysteresis threshold limits
We do this using a histogram approach, where the gradient magnitude is binned into 64 levels, and the threshold is based on a percentile of the gradient values (the 70% level is the high threshold). 28% (0.4 of the 70%) is the level for the lower threshold. So there are two levels: Any pixel in the top 30% of the gradient (which passes the non-maximum suppression step) is definitely an edge pixel. The pixels between high and low threshold are included in another list for the next step OpenCV does not have a mechanism for determining the hysteresis threshold limits automatically
5) Hysteresis thresholding
If there are any pixels connected to the ones above the high threshold, and come inside the category of between high and low threshold, they are all considered edge pixels. Canny in his PAMI paper mentions: "... instead the thresholding is done with hysteresis. If any part of a contour is above a high threshold, those points are immediately output, as is the entire connected segment of contour which contains the points and which lies above a low threshold. The probability of streaking is greatly reduced because for a contour to be broken it must now fluctuate above the high threshold and below the low threshold. ... The ratio of the high to low threshold in the implementation is in the range of two or three to one." This exact algorithm can be implemented using morphological reconstruction, which is what is done in the edge function. OpenCV uses a different approach to Hysteresis thresholding that is not equivalent.
There are a variety of differences between the implementations in MATLAB and OpenCV which can amount to very different outputs in the final detections. If you prefer the style of output in OpenCV, we provide that algorithm as well as the 'approxcanny' option in "edge" function. Our experience is that 'approxcanny' is significantly faster in terms of its runtime performance but often leads to decreased accuracy in the detected edges.

その他の回答 (0 件)

製品


リリース

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by