Defining non-linear limiting lines to dataset in python - python

I have some set of (x,y) data. Plotting this in python simply gives
I want to be able to fit some lines or contours to this data, defining the inner and outer radii, such that I can then randomly select a point within those limits.
However, I have no idea where to even start. Any ideas?

If that are concentric circles, I would calculate average x and y coordinates (that would be the center), and then compute distances between each point and the center. Maximal and minimal distance would be the radii of inner and outer circle. (You could also choose other percentile if you wish).
If that are not circles, but something more complex, you could approximate outer shape as poligon using convex hull, and innner using something like marching squares (Or use marching squares for both). Than you could do point inside poligon test for points you choose.

Related

How to determine if 3d point lie in a certain volume or not?

I have a set of 3d points in a txt file in the form of (x,y,z) as shown in figure 1. I want to specify the boundaries of these points as in figure 2 such that if any new points were added outside the boundaries they are deleted as the blue points, and if they are inside the boundaries as the green ones they are kept. How can I achieve that in python? I tried convex hull but it only gets the boundary points !
The real data can be found here, I used figures for simplification. https://drive.google.com/file/d/1ei9NaJHN922pYItK2CRIXyLfwqm_xgrt/view?usp=sharing
Figure 1
Figure 2
For 2D points, you can apply the test as described in Wikipedia:
One simple way of finding whether the point is inside or outside a simple polygon is to test how many times a ray, starting from the point and going in any fixed direction, intersects the edges of the polygon. If the point is on the outside of the polygon the ray will intersect its edge an even number of times. If the point is on the inside of the polygon then it will intersect the edge an odd number of times. The status of a point on the edge of the polygon depends on the details of the ray intersection algorithm.
The n-dimensional case involves a convex hull test and requires linear programming techniques as described here.

How to find the sides of a polygon in an image?

I have a list of connected pixels (tuples of x and y coordinates) forming the outer boundaries of a polygon in an image. How can I pick out its sides suppose they are all straight? What should I do if the lines are not too straight, and contain deviations? Is there anything readily made in Python Image Library or Numpy for such a task? Thanks!
You could use OpenCVs approxPolyDP
You can reduce the number of vertices using that function.
The functions approxPolyDP approximate a curve or a polygon with
another curve/polygon with less vertices so that the distance between
them is less or equal to the specified precision. It uses the
Douglas-Peucker algorithm
http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm

Find triangle containing point in spherical triangular mesh (Python, spherical coordinates)

Main problem
In Python, I have triangulated the surface of a (unit) sphere using an icosahedral mesh. I have a list simplices of tuples containing the indices of the three vertices of each triangle, and I have a two lists describing the coordinates (in radians) of each vertex: its latitude and longitude.
For about a million points, I want to determine which triangle each point is in. I am looking for an efficient algorithm that returns the list indices of each triangle (indices corresponding to list simplices).
I am willing to sacrifice memory over efficiency, so I am fine with constructing a tree or using some lookup method.
Caveats
The triangles are of roughly equal size, but not exactly, so I suspect that a simple nearest-neighbor KDTree implementation is not exact.
Extra information
The icosahedral mesh has been obtained using the stripy package. It projects the vertices of the icosahedron onto the unit sphere and subsequently bisects the triangles, so that each edge is split in half, or conversely, each triangle is split in four. stripy has a built-in method for calculating the triangle a point is contained in, but for a mesh refinement of 6 (i.e. 6 bisections) and about a million points, this takes hours. I suspect that this method does not make use of a tree/lookup method and I hope there is a method that significantly improves on this.
Compute a latitude/longitude bounding box for each triangle. Remember that the largest-magnitude latitudes may be on an edge (easily found by considering the normal to the great circle including each edge) or (if the pole is enclosed) in the interior.
Divide all triangles that cross the periodic longitude boundary in two—or, to be cheap, just their bounding boxes.
Build an extended-object k-d tree over the triangles (and triangle pieces from above). This still uses only the latitude/longitude values.
Run the obvious recursive, conservative containment search to find candidate triangles. (It doesn’t matter which piece of the divided triangles you find.)
Test carefully for triangle inclusion: for each triangle side, judge which hemisphere (defined by the great circle containing the segment) contains the query point in a fashion (probably just a cross product on the three-dimensional vectors) that doesn’t depend on the order in which the vertices are presented and never produces “on the dividing line”. Then every point is guaranteed to be in exactly one triangle.

Remove outer hull of 2D scatterpoints in Python

For my project i use 2D images from a telescope. The outer border of each image is known to be oversatured with points due to telescope malfunction. Therefor i want to extract the points that make up the outer border of the 2D image.
So what i want to do is somehow extract the points that make up the outer shell, with a desired width of the shell according to my preference.
What i have tried so far:
In Python i have tried finding the points that make up the edge by using scipy.ConvexHull to find the outer points and then removing these points. When doing this in a loop it should remove the outer edge with a width dependant on the amount of iterations. However, this method is dependant on the point density, and removes less points for places on the edge where the density is large. What i want is that an about equal width of outer edge is removed of the whole image, see images below :
To show what i mean, i have added the ConvexHull result, in red the points it gives as outer edge points after 15x iterations:
For clarification, this is the desired result i would like my algorithm to give me, an outer edge with equal width over the whole image, which is independant of point density.
Since you showed only ideas and graphics without code, I will do the same.
I see several ways to get the smaller polygon within your convex hull with a near-constant width between them. There are also variations on each. I illustrate with a convex hull that is a simplified version of the one in your graphics. Each of my solutions ignores the majority of points in the problem and uses only the vertices of the convex hull, so the "point density" is ignored.
Before choosing a polygon, you could find the "center point" of your convex hull. There are multiple ways to define this. You could use the centroid of the vertices of the hull, where the x- and y-coordinates are the averages of the coordinates of the vertices, but this biases toward parts of the hull with many small segments. You could use the center of the bounding rectangle, where the x- and y-coordinates are the average of the maximum and the minimum coordinates of the hull's vertices. This is the approach I used in my graphics. There are other possible "center points."
My first inner polygon moves each vertex a proportional distance toward the center point. In my example, I moved each point one-fourth of the distance toward the center point.
My second inner polygon moves the vertices a fixed distance toward the center point. I chose a distance one-fourth of the average distance of the vertices from the center point. Note that for this particular example there is very little difference between this polygon and my previous one. The differences would be more obvious for a hull where come points are much closer to the center point than some other points.
My third polygon abandons the center point. It moves each side of the hull a fixed distance toward the inside of the hull. The intersections of these new segments are used to define the new polygon. In other words, I did "inward polygon offsetting" or "polygon buffering." This is a non-trivial task in computational geometry, but some discussion on this task and similar tasks can be found at this SO question. This does look different from the other polygons, since the smaller sides of the hull tend to shrink or completely disappear from the result.
Choose whichever polygon suits your needs--the first two are easier to compute than the third, but the third comes closest to your ideal of "equal width of outer edge."

OpenCV find object's position with solvePnPRansac with not-corresponding points

I am trying to find object's position relative to camera position in real-world coordinates by tracking a known 2D LED pattern on the object.
I did camera calibration. I was able to sucessfully detect LEDs in the pattern and find their exact coordinates in the image frame. These points however do not correspond exactly 1-to-1 to the known coordinates in the pattern, they are in random order. The correspondence is important in functions like solvePnPRansac or findHomography, which would be my first choice to use.
How can I find the correspondence between these sets of points or maybe should I use some other function to calculate transformation just like solvePnPRansac does?
As you did not ask about the way to estimate the relative pose between your object and your camera, I will let this topic aside and focus on the way to find correspondences between each LED and their 2D projections.
In order to obtain a unique 1-to-1 correspondence set, the LED pattern you use should be unambiguous with respect to rotation. For example, you may use a regular NxN grid with the top-left cell containing an additional LED, or LEDs located on a circle with one extra LED underneath a single one, etc. Then, the method to find the correspondences depends on the pattern you chose.
In the case of the circle pattern, you could do the following:
Estimate the center of gravity of the points
Find the disambiguing point, which is the only one not lying on a circle, and define the closest of the other points as the first observed point
Order the remaining points by increasing angle with respect to the center of gravity (i.e. clock-wise order)
In the case of the regular grid pattern, you could try the following:
Find the four corners of the grid (those with min/max coordinates)
Estimate the homography which transforms these four corners to the corners of a regular NxN square (with orthogonal angles)
Transform the other points using this homography
Find the disambiguing point, which is the only one for which X-floor(X) and Y-floor(Y) are close to 0.5, and define the closest of the four initial corners as the first observed point
Order the remaining points by increasing angle with respect to the center of the grid and decreasing distance to the center of the grid
You could also study the algorithm used by the function findChessboardCorners (see calibinit.cppin the calib3D module), which uses a similar approach to order the detected corners.

Categories

Resources