Ask Your Question

Triangle detection - Canny failing on corner [closed]

asked 2016-01-08 17:29:09 -0500

epatton gravatar image

updated 2020-05-26 07:12:48 -0500

supra56 gravatar image


I'm trying to detect a triangle however approxPolyDP fails because:

length(approx) != 3
approx = cv2.approxPolyDP(.....)

I think its because the Canny algortihm somehow cannot find lower right virtex edge. (see image below).

The input image (zoomed using Imagj)

image description

and Imagej pixel inspection tool output of lower right virtexd

image description

Here is the Canny output. I've tried numerous thresholds and get the same opening in the lower RHS virtex.

image description

My code

>>> import cv2
>>> import sys
>>> import numpy as np
>>> filename = 'diffGr.png'
>>> img = cv2.imread(filename,cv2.IMREAD_GRAYSCALE)
>>> minVal = 20
>>> maxVal = 200
>>> edges = cv2.Canny(img,minVal,maxVal)
>>> cv2.namedWindow('Edge image')
>>> cv2.imshow('Edge image',edges)
>>> cv2.waitKey(0)

My input file


Any info on understanding why Canny fails would be appreciated. If this is expected, should I use a different edge detection method?


edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by supra56
close date 2020-05-26 07:11:17.346789



Why not Threshold and then find canny edges?

Balaji R gravatar imageBalaji R ( 2016-01-09 01:30:55 -0500 )edit

That worked! Thanks

epatton gravatar imageepatton ( 2016-01-11 13:08:17 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-01-11 13:15:58 -0500

epatton gravatar image

updated 2016-01-11 14:01:36 -0500

Thanks to Balaji, Canny now works

I'd still like to understand why Canny does not find the edge on the lower right virtex but has no issue with the lower left. I guess I could try flipping the image in the vertical axis. Canny should fail on the lower left then if its a data dependent issue.

image description

thresh = 50
maxValue = 255
th, dst = cv2.threshold(img, thresh, maxValue, cv2.THRESH_BINARY);
maxVal = 200
edges1 = cv2.Canny(dst,minVal,maxVal)
cv2.imshow('Edge image after thresholding image',edges) 
edit flag offensive delete link more


OpenCL version of Canny contains bug more than 1 year. Try to compare OpenCL and non-OpenCL (use cv::ocl::setUseOpenCL(false) ) results.

Vit gravatar imageVit ( 2016-01-11 17:22:04 -0500 )edit

Thanks for pointing that out. I tried to set ocl to false but no luck.

>>> cv2.__version__
>>> print(cv2.ocl.haveOpenCL())
>>> cv2.ocl.setUseOpenCL(False)
>>> print(cv2.ocl.haveOpenCL())

It looks like the issue is yet to be resolved according to the link in this post.

epatton gravatar imageepatton ( 2016-01-12 20:56:51 -0500 )edit

Question Tools

1 follower


Asked: 2016-01-08 17:29:09 -0500

Seen: 767 times

Last updated: May 26 '20