In OpenCV, for a given point (x,y), what is the best way to find the point closest to it that belongs to a known contour cnt? (I assume the point lies outside the contour.)
dist = cv2.pointPolygonTest(cnt,(x,y),True)
pointPolygonTest returns the distance of the closest contour point but I do not see a way to get to the actual point.
Of course, I could loop over the list of contour points and recalculate the distance finding the one with minimum distance. (A couple of questions on SO explain more sophisticated ways for finding the closest point out of a list of points to a given point.)
Alternatively, I could draw a circle with radius dist and see where the circle and the contour touch.
Both options seem a little clunky, so I wonder if I am missing something more straight forward.
Related
I draw an arbitrary point in this image.
I want to find the coordinates of the two lines closest to this point.
It should be a straight line when you connect one of the two points to any point.
Like this :
I want to know two thing
First, Is there a library to find the nearest line from an arbitrary point?
Second, Is there any library or algorithm to find a point perpendicular to an arbitrary point?
Please help me.....
You can do this in two steps: first for the closest line, then for the other one.
The closest line:
Let A be the arbitrary point.
You can calculate the distance of each white pixel to point A and keep the shortest distance (sqrt((x-a.x)^2 + (y-a.y)^2). This closest pixel (let's call it P) is the projected point on the line. By definition of the projected point, the line AP is perpendicular to the line.
The other line: color all the points of the closest line so as not to search again in its pixels, do the same thing as for the closest line.
I am fairly new to openCV and am not sure how to proceed.
I have this thresholded image:
And using this image, I need to calculate the distance between two points. The points are also unknown. Illustrated here:
I need to calculate 'd' value. It is the distance from the midpoint of the middle line to where the top line would have been. I am not sure how to proceed with identifying the points and getting the distance, any help would be appreciated!
While I'm not sure about the selecting points part of your problem, calculating the distance between two points is trivial. You can imagine your two points as the beginning and end of the hypotenuse of a right triangle, and use Pythagoreans Theorem to find the length of that hypotenuse.
The math module has a distance method which takes two points, or you can just write the function yourself very simply.
I want to find the highest distance between two pixels in any labeled area or find the top and bottom pixels.How can do this? Can you explain algorithm that related this topic ,please ?Thank you...
let's say that you are going to find the area in a rectangle, or a a square spaced area. In order to find the longest distance between the pixels, you are willing to use the simple math here. As you know the longest straight line distance between 2 points is called as the diagonal of that figure, hence, you will need to use the following formula: https://gyazo.com/c2bd3e0342008642bfde579816cbfd5e
As a result, by just putting in the coordinates of each point, you can achieve the longest distance!
I'm looking for some solutions that, given a set S of circles with 2D-center points and radii, returns a minimal sub-set M in S that covers entirely a specific circle with 2d-center point and radius. This last circle is not in S.
I've chosen circles, but it doesn't matter if we change them to squares, hexagons, etc.
You have two distinct problems: you need to turn the geometric problem into a combinatoric problem, and then you need to solve the combinatoric problem. For the latter, you are looking at a minimum set cover problem, and there should be plenty of literature on that. Personally I like Knuth's Dancing Links approach to enumerate all solutions of a set cover, but I guess for a single minimal solution you can do better. A CPLEX formulation (to match your tag) would use a binary variable for each row, and a ≥1 constraint for each column.
So now about turning geometry into combinatorics. All the lines of all your circles divide the plane into a bunch of areas. The areas are delimited by lines. Of particular relevance are the points where two or more circles meet. The exact shape of the line between these points is less relevant, and you might imagine pulling those arcs straight to come up with a more classical planar graph representation. So compute all the pair-wise intersections between all your circles. Order all intersections of a single circle by angle and connect them with graph edges in that order. Do so for all circles. Then you can do a kind of bucket fill to determine for each circle which graph faces are within and which are outside.
Now you have your matrix for the set cover: every graph face which is inside the big circle is a column you need to cover. Every circle is a row and covers some of these faces, and you know which.
I have a set of points in a plane where each point has an associated altitude. I'm thinking of using the scipy.spatial library to compute the Delaunay triangulation of the point set and then use the result to interpolate for the points in between.
The library implements a nice function that, given a point, finds the triangle it lies in. This would be particularly useful when calculating the depth map from the mesh. I assume though (please do correct me if I'm wrong) that the search function searches from the same starting point every time it is called. Since the points I will be looking for will tend to lie either on the triangle the previous one lied on or on an adjacent one, I figure that's unneccessary, but can't seem to find a way to optimize the search, other than to implement it myself.
Is there a way to set the initial triangle for the search, or to optimize the depth map calculation otherwise?
You can try point in location test, especially Kirkpatrick algorithm/data structure. Basically you subdivide the mesh in both axis and re-triangulate it. A better and simpler solution is to give each triangle a color and draw a bitmap then check the color of the bitmap with the point.