I am trying to write code for canny edge detection but it is not giving exact output like inbuilt function.can any one help me

5 views (last 30 days)
i attached code,pls check it and tell me where i did wrong in that

Accepted Answer

David Young
David Young on 17 Nov 2014
Edited: David Young on 17 Nov 2014
The differences are quite small, and I don't think there is very much wrong. My own implementation of the Canny edge detector also produces slightly different results from edge().
Possible causes of differences include:
  • You use a rounded approximation to the Gaussian kernel for smoothing. It is likely that edge() uses a different approximation.
  • You use a symmetrical operator (the Sobel operator) for differencing. I believe edge() uses non-centred nearest-neighbour differences, which introduce a half-pixel shift in the gradient estimate positions. Personally I prefer your approach.
  • Your choice of non-maximum suppression is to quantise the gradient direction to the nearest 45 degrees and to compare gradient magnitudes at the pixels in the relevant direction. There are a number of other possible approaches - for example you can interpolate the gradient magnitude and then compare along the gradient direction. Canny did not specify what to do here, and everyone makes their own choice. I am not sure what edge() does, but it may well do something different from your method.
  • Your implementation of hysteresis thresholding is not correct. It is not sufficient to test whether any of the nearest neighbours exceeds the high threshold. You need to test whether the current pixel can be connected to a pixel where the gradient exceeds the high threshold.
I suspect the second point above is the most important effect in making a difference between your output and that of edge(). The last point above is the only one which I would identify as a definite error on your part.
Your code is not in a good MATLAB style, by the way - all your loops could be vectorised - but that's another story.
  3 Comments
Mahesh Kumar
Mahesh Kumar on 19 Nov 2014
Edited: Mahesh Kumar on 20 Nov 2014
if i change thresholds it is showing big difference compare to inbuilt,can you suggest me why it is showing like that
David Young
David Young on 20 Nov 2014
There are a number of ways of implementing hysteresis thresholding. To see the way I chose, you could look at this entry on the File Exchange.
Changing the thresholds will always have a big effect. Note that the thresholds used by edge() are relative to the maximum gradient. But I'm not sure what exactly you'd like me to explain.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!