0
votes

I'm trying to port some Matlab code to C++. I've come across this line:

edges = edge(gray,'canny',0.1);

The output for the sample image is a completely black image. I want to reproduce the same behaviour using cv::Canny. What values should I use for low threshold and high threshold?

Sample:
Sample Image

Output:
Output

1
Usually depends upon the type of image but you may like to look at these : kerrywong.com/2009/05/07/canny-edge-detection-auto-thresholding or stackoverflow.com/questions/4292249/…ZdaR
I tried the first one already, and it detected lots of edges where the matlab code detected none.protas
for clarification: are you asking how to put that 0.1 in opencv?Ander Biguri
post input and output images in lossless format (.png) please.Micka
I want to know what matlab is doing with that 0.1.protas

1 Answers

0
votes

In the line above you have not defined a threshold, probably it takes zero then, thus delivering a black picture. Also, you use a sigma of 0.1 which means virtually no Gauss Blur in the first Canny step. Within Matlab you can get an optimized threhold by:

 [~, th] = edge(gray,'canny');

and then apply the optimized threshold th multiplied by some factor f (from my experience f should be between 1-3), you have to try out:

 edges=edge(gray,'canny',f*th,'both', sigma);

sigma is sqrt(2) by default (you used 0.1 above). Following remarks:

  • Matlab calculated the optimized threshold as a percentile of the distribution of intensity gradients (you can see the construction of edge() if you enter "edit edge", if I remember correctly)
  • the above parameter th is a vector consisting of the low and high threshold. Matlab always uses low_threshold = 0.4* high_threshold