Yongkai liu asked . 2022-01-06

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

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.

edge , canny , opencv , threshold

Expert Answer

Neeta Dsouza answered . 2024-12-22 06:44:55

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.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.