Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Polygon approximation for a contour segement with a given number of sides
def fixedPoly(contour, polysides = 8):
opencv_contour = contour.reshape(-1, 1, 2)
higherr = 50.0
lowerr = 1.0
tryerr = 25.0
poly = []
#binary search polygon approximations until we get a 4 point polygon
maxiterations = 100
while len(poly) != polysides and maxiterations:
poly = cv2.approxPolyDP(opencv_contour, tryerr, False)
maxiterations -= 1
if len(poly) < polysides:
higherr = tryerr
tryerr = (higherr + lowerr) / 2
if len(poly) > polysides:
lowerr = tryerr
tryerr = (higherr + lowerr) / 2
poly = poly.reshape(-1, 2) # get it out of opencv
return poly
@oostendo
Copy link
Author

oostendo commented Jul 11, 2013

this is probably backwards -- i imagine this can be much more efficiently done, but it works for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment