Getting Canny to provide good results
Hello everyone, I am working on edge detection. I have been developing under ImageJ for a while and converted most of my code to OpenCV in the past few weeks. Most of this process happened painlessly, however I cannot get the Canny filter to work in OpenCV as it did in ImageJ.
Here is an example of image I work with (left), results with ImageJ (middle) and results with OpenCV (right). Hysteresis thresholds were set about the same.
I have been searching for why such differences in results occur. I found that for ImageJ, the Canny algorithm uses a convolution mask which combines a gaussian function and a sobel kernel, thus computing gradients on a smoothed image.
OpenCV's Canny only uses a Sobel Kernel I believe, but since the Gaussian and Sobel are linear, I should get the same results by gaussing the original image beforehand and then applying Canny. But it is not the case.
One main difference is that ImageJ's kernel size for its convolution mask is 16 (and not modifiable), while OpenCv's one is 3,5 or 7 (results are shown for kernel 7), and this parameter seems to have a big impact on results.
Do you think it is the reason why I have so different results ? If so, how can I get around it ?
Thanks.
PS : Things I tried : playing with hysteresis thresholds, morphological operations post Canny, but I would like as good results as ImageJ
In master branch you can split Canny into gradient calculation and edge detection (link).
Perhabs you can make a custom filter kernel for larger Sobel masks. You can find a tutorial here.
I think that with image given (left) we can have only rigth part of canny results. Using canny I have got this results
Thank you both for your input. LBerger, glad to know that I'm not completely misusing Canny. matman, I was not aware of the existence of this overload, I want to give it a try. However, I can't figure out how to call it from my code, as Canny() calls the "normal" Canny function (this is probably a newbie problem, sorry...)
you have got an example here
Thanks for the fast answer ! But in your example, what part of the code specifies to the program to use the "second" Canny ? This may be my inexperience in C++ talking... I'm using Python, and by the way your Python equivalent example uses the "normal" Canny (maybe it is not possible in Python ?)
About python I don't know. x and y gradient image are computed https://github.com/opencv/opencv/blob... and canny https://github.com/opencv/opencv/blob...