Canny dynamic threshold

asked 2016-04-07 05:40:25 -0600

annmelody gravatar image

updated 2016-04-19 00:26:15 -0600


Thanks for one of the comments that remind me of the possibility that it doesn't find anything useful at all... I modified the threshold I used in creating edge map, and it started to work correctly! However, I am wondering if there is a more robust way to generate dynamic threshold for canny edge detection?

below is the img I used

image description image description

I have found a similar post here:

but I'm using version 3.0, anyone has the same problem?

Here is my code...

edge_map1 = cv2.Canny(img1, 100, 200)  

edge_map2 = cv2.Canny(img2, 100,200)

contours1, hierarchy1 = cv2.findContours(edge_map1, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) 

contours2, hierarchy2 = cv2.findContours(edge_map2, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

for c1 in contours1:
    for c2 in contours2:

         ret = cv2.matchShapes(c1, c2, 1, 0.0)

However, the returned value is always zero. Thanks in advance!

edit retag flag offensive close merge delete


  • can you add your images ?

  • +1 for trying to format your code, but next time, please use the 10101 button, not the " one ;)

berak gravatar imageberak ( 2016-04-07 05:50:32 -0600 )edit

One of the many reasons that matchShape cannot return a match, is because one of the findContours doens't find anything usefull, therefore like @berak said, we need at least your data to test this out :)

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-08 03:48:07 -0600 )edit

@berak@StevenPuttemans Really appreciate your kind help. I have found out the reason and also added the image :)

annmelody gravatar imageannmelody ( 2016-04-19 00:28:04 -0600 )edit
Balaji R gravatar imageBalaji R ( 2016-04-19 01:33:00 -0600 )edit

@Balaji R I tried the above solution but findContours didn't work out with the edge map generated :(.

ret, th = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

edge_map = cv2.Canny(img, ret*0.5, ret)
annmelody gravatar imageannmelody ( 2016-04-19 01:51:54 -0600 )edit