[SOLVED] Detecting polygon coordinates of shapes in images
by boughtonp from LinuxQuestions.org on (#593R3)
I'm trying to detect coordinates of polygons from black & white images.
There's a lot of people doing similar-but-not-quite-the-same things, and every example I've found uses OpenCV findContours. This will find the shapes, but appears to do so by identifying points at every single pixel along the outline of the polygon, instead of only at corners/angles.
The findContours method does allow specifying a Contour Approximation Mode to reduce the points, but CHAIN_APPROX_SIMPLE doesn't handle lines that are not at 45 or 90 degrees, and the only other options of CHAIN_APPROX_TC89_L1 or CHAIN_APPROX_TC89_KCOS still return 20 points for a rotated square (and 23 for an irregular hexagon).
Attached is a simplified test image, plus the actual vs desired outputs.
I can't find any reference to other people having the same problem, so it seems likely I'm going to have to write my own code to filter out unwanted points, but - before I spend too much time on that - does anyone happen to know:
* Is there an option to use a different chain approximation algorithm in OpenCV that can actually identify straight lines.
* Are there any alternatives to findContours (either another part of OpenCV or another library) that might do a better job?
Thanks.
Attached Thumbnails


There's a lot of people doing similar-but-not-quite-the-same things, and every example I've found uses OpenCV findContours. This will find the shapes, but appears to do so by identifying points at every single pixel along the outline of the polygon, instead of only at corners/angles.
The findContours method does allow specifying a Contour Approximation Mode to reduce the points, but CHAIN_APPROX_SIMPLE doesn't handle lines that are not at 45 or 90 degrees, and the only other options of CHAIN_APPROX_TC89_L1 or CHAIN_APPROX_TC89_KCOS still return 20 points for a rotated square (and 23 for an irregular hexagon).
Attached is a simplified test image, plus the actual vs desired outputs.
I can't find any reference to other people having the same problem, so it seems likely I'm going to have to write my own code to filter out unwanted points, but - before I spend too much time on that - does anyone happen to know:
* Is there an option to use a different chain approximation algorithm in OpenCV that can actually identify straight lines.
* Are there any alternatives to findContours (either another part of OpenCV or another library) that might do a better job?
Thanks.
Attached Thumbnails