Reputation: 4191
Suppose that I have a mask of an object and a point. I want to find the closest point of the object mask to the point.
For example, in my drawing, there is an object, the blue shape in the image (assume inside is also the part of the object mask). And the red point is the point from which I want to find the closest distance to the object mask.
So, I want to find the thick green line as it is the shortest distance to the mask not the other ones (pink, orange, etc). I can do this using one of the following ways:
But none of these methods are is elegant. I am wondering what is a more elegant and the most efficient way to determine this?
Upvotes: 6
Views: 3201
Reputation: 657
You can use pointPolygonTest to find the closest distance between the blue mask region and any point
Find the contours of the blue region and that will be our polygon
If you set the distance flag to true, you can find the closest distance between the point and the polygon
closestDist = cv2.pointPolygonTest(contours[0], redPoint, True)
One more info what we can get from this function is that, if the distance is negative, then the point is out of the polygon, distance is 0 if the point is on the polygon edge, and positive if the point is inside the polygon
Upvotes: 2
Reputation: 15070
You could do a sort of binary search:
This will not work if your mask is not nicely connected, but then if that's not the case I doubt you can do better than brute force...
The total cost should be linear for the circle-mask intersection check times log for the binary search.
Upvotes: 1