I have this image:
and I am using cv2.goodFeaturesToTrack
to detect the coroners, so now I have this:
The corners are in red and the numbers show the order of which goodFeaturesToTrack
got the corners.. for example, corner with number 0 is the first detected one, etc...
If I were to connect the dots based on that order, I would get a messy polygon so I thought of using a function that given a random set of points, it returns them in an order with which the polygon wouldn't intersect..
I found this function and it does exactly what I want.
However, although the polygon doesn't intersect, for this example I am not getting the same shape as the initial one (I am getting a non self-intersecting polygon but a different shape).
Does anyone have an idea to fix this? I was thinking of making cv2.goodFeaturesToTrack
return an ordered set of points but I couldn't figure out how to do that.
Thank you so much!
If you want to get the polygon, you can threshold the image and extract the outer contour with findContours
, using CV_RETR_EXTERNAL
as the mode to obtain the outer contour and CV_CHAIN_APPROX_SIMPLE
as the method. CV_CHAIN_APPROX_SIMPLE
compresses horizontal, vertical, and diagonal segments and leaves only their end points (see the documentation).
If you want to use corner detection results and arrange them in correct order to make the polygon, you'll have to trace the boundary of the shape and add those corner points into a list as you find them along the boundary. For this, I think you can use findContours
with CV_RETR_EXTERNAL
and CV_CHAIN_APPROX_NONE
to get every pixel. Still, you might not find your detected corner points exactly on the contour returned from findContours
, so you'll have to use a proximity threshold.