4

I am currently trying to implement an algorithm to identify whether or not a point is in an ellipse in an image such as the following:

microstructure_image

I am using the following algorithm to return a truth value as to whether the point is in the ellipse.

def ellipse_inequality(x, y, x0, y0, a, b, theta):
    lhs = (np.cos(theta)*(x-x0)+np.sin(theta)*(y-y0))**2/a**2
    rhs = (np.sin(theta)*(x-x0)+np.cos(theta)*(y-y0))**2/b**2
    # Returns a truth value.
    return (lhs+rhs) < 1

Which is based around the formula for a rotated ellipse.

If this returns True, then the point is in the ellipse.

The main idea of this is that I am trying to develop an algorithm that can identify if two points are in the same ellipse. Using the following code

# For a given ellipse, this returns a truth value as to whether the point lies in the ellipse.
def is_in_ellipse(point, x0, y0, a, b, theta):
    print(point)
    print(ellipse_inequality(point[0], point[1], x0, y0, a, b, theta))
    return ellipse_inequality(point[0], point[1], x0, y0, a, b, theta)


# This function determines if two points are in the same cluster
def same_cluster(point_1, point_2, csv):
    # for all centroids in the fibers csv...
    for i in range(np.shape(csv)[0]):
        # extract the centroid, minor/major axis length and orientation of each fiber
        x0 = csv.iloc[i]['centroidx']
        y0 = csv.iloc[i]['centroidy']
        a = csv.iloc[i]['major_axis_length']
        b = csv.iloc[i]['minor_axis_length']
        theta = csv.iloc[i]['orientation']
        # Only return True if both points lie in the ellipse.
        if is_in_ellipse(point_1, x0, y0, a, b, theta):
            if is_in_ellipse(point_2, x0, y0, a, b, theta):
                return True
    return False

Where the data regarding the geometry of the ellipse is collected from a csv file. This algorithm is not working, and produces results such as the following:

failed_clustering

Any help regarding this would be much appreciated

# For 10 pairs of points, we run the algorithm.
for i in range(10):
    point_1, point_2 = get_line_points(25, width, height)
    # checking if both points are fibers
    is_fibers = same_cluster(point_1, point_2, fibers)
    # if they are both fibers in the same cluster then plot a green line, otherwise plot a red one
    if is_fibers:
        ax.scatter(point_1[0], point_1[1], c='g', s=5)
        ax.scatter(point_2[0], point_2[1], c='g', s=5)
        line_between_points(point_1, point_2, 'g')
    else:
        ax.scatter(point_1[0], point_1[1], c='r', s=5)
        ax.scatter(point_2[0], point_2[1], c='r', s=5)
        line_between_points(point_1, point_2, 'r')
mcrowls
  • 49
  • 2
  • it might help if you showed us what you do with the True/False information after you have determined it. How are you plotting this on the figure, and what would you expect the figure to look like? – tmdavison Jul 01 '21 at 13:02
  • If both of the points are in the same cluster (ellipse), then a green line is drawn between them. If not, then a red line is drawn between them. So any green lines that aren't drawn on ellipses should not be there. Likewise, a red line is drawn between the points if they are not in the same ellipse. – mcrowls Jul 01 '21 at 14:12
  • 2
    ok, so without your plotting script, it's hard to tell if the error is in the code you've posted or in the plotting code. See [MCVE] – tmdavison Jul 01 '21 at 14:44
  • Will post the plotting script here: – mcrowls Jul 02 '21 at 08:20
  • It is posted at the bottom of the question – mcrowls Jul 02 '21 at 08:21
  • still difficult to debug this without half your code. We are still missing your functions (`get_line_points`, `line_between_points`) and have no dataset to work with (`fibres` or `csv`). we also don't know what you are using as `width` or `height`. I will point you to [MCVE] again... But having said all that, perhaps it's a simple typo - I think you want a minus sign instead of a plus in the `lhs` of `ellipse_inequality` between the x and y terms. I can't test that unfortunately because of the issues listed above. – tmdavison Jul 02 '21 at 13:21
  • this might be helpful: https://stackoverflow.com/questions/37031356/check-if-points-are-inside-ellipse-faster-than-contains-point-method/37032759#37032759 – tmdavison Jul 02 '21 at 13:21

0 Answers0