0

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
protas
  • 617
  • 1
  • 5
  • 10
  • Usually depends upon the type of image but you may like to look at these : http://www.kerrywong.com/2009/05/07/canny-edge-detection-auto-thresholding/ or http://stackoverflow.com/questions/4292249/automatic-calculation-of-low-and-high-thresholds-for-the-canny-operation-in-open – ZdaR Oct 22 '15 at 05:35
  • I tried the first one already, and it detected lots of edges where the matlab code detected none. – protas Oct 22 '15 at 06:33
  • for clarification: are you asking how to put that 0.1 in opencv? – Ander Biguri Oct 22 '15 at 08:01
  • post input and output images in lossless format (.png) please. – Micka Oct 22 '15 at 09:09
  • I want to know what matlab is doing with that 0.1. – protas Oct 22 '15 at 20:00

1 Answers1

0

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
Settembrini
  • 1,366
  • 3
  • 20
  • 32
  • So, it's the same as cv::Canny(m_gray, edges, 255*0.4, 255*1)? It's giving me the same black output at least. I didn't write the matlab code, it's just there. All I want to make sure my code gives the same output, even if it's a black image. – protas Oct 23 '15 at 20:00
  • I think the equivalent openCv code is: GaussianBlur(m_gray, m_gray, Size(9,9), 0.1, 0.1); Canny(m_gray, edges, 255*0.4, 255*1); Is that right? – protas Oct 23 '15 at 20:20
  • I checked the Matlab docu and found the following syntax: BW = edge(I,method,threshold). Tried it with a grayvalue picture, it gives a normal canny edge, white edges on black ground. So 0.1 is the high treshold (max is 1; i.e. with 0.99 you just get a few edges, with 0.01 you get a million edges. – Settembrini Oct 23 '15 at 20:28
  • 0.1 seems to be the sigma, which is used for smoothing. It's not the high threshold, if I set the high threshold to 0.1 and the low to 0.04 I will get a lot of edges. – protas Oct 23 '15 at 20:56
  • No, in the above Matlab syntax 0.1 is the high threshold. That's what I say, 0.1 gives a normal Canny picture - I tried it in Matlab. – Settembrini Oct 23 '15 at 21:02
  • we speak about the third syntax here: mathworks.com/help/images/ref/edge.html?refresh=true – Settembrini Oct 23 '15 at 21:08
  • But edge(gray,'canny',0.1) gives me a black image. If 0.1 was the high threshold it would give me lots of edges. – protas Oct 23 '15 at 22:01
  • I don't know open cv. In Matlab however thresholds are scaled to the range [0,1]. It might be that cv uses [0, 255]. – Settembrini Oct 23 '15 at 22:55