Search code examples
pythonnumpygeometrylinescikit-image

How to find the intersection between 2 skimage.measure.LineModelND objects?


Here is a description of what I am trying to accomplish. I have a set of 3D points and I am trying to fit 2 lines on those points and after defining those lines I want to find their intersection.

Here are more details about what I have done so far, starting from the begin. I have a set of 3D points stored in an np.ndarray of shape (N, 3) one can find the points here.

After that I use the scikit-image library to define the lines using ransac. And I get lines of the type LineModelND which are defined by a point (origin) and a unit vector (direction) using ransac. Here is a code snippet. And after one line is fitted to the data, I fit on the outliers another line, so now I have 2 LineModelND objects and I want find their intersection. Any ideas ?

model = LineModelND()
model.estimate(proj_points_array)

model_robust, inliers = ransac(proj_points_array, LineModelND, min_samples=2,
                               residual_threshold=0.02, max_trials=1000)

print('line params: ', model_robust.params, ' line is type of: ', type(model_robust))

outliers = inliers == False

model_2 = LineModelND()
model_2.estimate(proj_points_array[outliers,:])

Here is the geometry of the points.


Solution

  • The solution I've found so far is to use the skspatial.objects.Line.intersect_line method. This method requires the points to be co-planar and the lines to actually have an intersection or in other words to not be parallel. The LineModelND returns a line model defined by point and direction as the line object definition of the skspatial.objects.Line object, so after defining the skspatial Lines you can use the intersect_line method of this class.

    In my case, the lines are not coplanar. So I had to project the points to a plane, find the 2D intersection, compute the 3D axis value from the planar equation and then reverse the transformation. This results to the following output (I can't post images so I've posted a link to the image.