Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the distance of a point to an ellipse, wether its inside or outside of ellipse

Tags:

c++

opencv

I would like to find the distance of every pixel coordinate in an image to an ellipse.

To find the distance, I am using the following formula where p is the point of the pixel and h is the ellipse. x,y is the pixel coordinate, x(c),y(c) is the ellipse center, theta is the ellipse angle, alpha and beta is the major and minor axis of the ellipse respectively.

enter image description here

The code to determine the distance of every point to an ellipse is shown below. If the distance, D < 1 then it means that the point is inside the ellipse, in which case I make it grey. If D > 1 then it means that the point is outside the ellipse, in which case I leave it as it is. Below is also the output image I get. For some reason I think that my distance calculation is right, but I have a problem with my rotation. To me everything looks right, I cant see the problem. Please help. What I require is that the all pixels in the ellipse should be grey, but to me the grey area forms an ellipse but it seems as if I am going wrong with the rotation somewhere.

Mat distance2ellipse(Mat image, RotatedRect ellipse){
float distance = 2.0f;
float angle = ellipse.angle;
Point ellipse_center = ellipse.center;
float major_axis = ellipse.height;
    float minor_axis = ellipse.width;
Point pixel;
float a,b,c,d;

for(int x = 0; x < image.cols; x++)
{
    for(int y = 0; y < image.rows; y++) 
    {
        Scalar intensity = image.at<uchar>(Point(x, y));
        pixel.x=x;
        pixel.y=y;
        a = (cos(angle*PI/180)*(pixel.x-ellipse_center.x))/(major_axis);
        b = (sin(angle*PI/180)*(pixel.y-ellipse_center.y))/(minor_axis);
        c = (sin(angle*PI/180)*(pixel.x-ellipse_center.x))/(major_axis);
        d = (cos(angle*PI/180)*(pixel.y-ellipse_center.y))/(minor_axis);

        distance = sqrt(pow((a-b),2)+pow((c+d),2));

        if(distance<1)
        {
                image.at<uchar>(Point(x,y)) = 140;
        }
    }
}
return image;}

This is the output I get. The grey area should be in the pink ellipse. enter image description here

like image 719
lexma Avatar asked Oct 08 '22 13:10

lexma


1 Answers

For some reason I think that my distance calculation is right

It's not. The distance between some point and an ellipse is a transcendental equation. It cannot be solved by elementary techniques (which is what you did). You need to use root finding techniques.

Google is your friend. Here's a PDF file that provides an algorithm and provides code to implement it: http://www.geometrictools.com/Documentation/DistancePointEllipseEllipsoid.pdf .

Edit
Based on the comments below, my answer is orthogonal to what the OP wants.

lexma, the reason your ellipse doesn't look right is because you have the wrong equation for an ellipse that is rotated by some angle theta with respect to the x-axis. The problem of determining whether some point (x,y) is inside or outside the ellipse is fairly simple.

  1. Convert your (x,y) coordinates to (u,v) to make the ellipse centered at the origin and with major axis along the u-axis, minor along the v-axis.

    u = cos(θ)(x-xc) + sin(θ)(y-yc)
    v = -sin(θ)(x-xc) + cos(θ)(y-yc)

  2. Compute the metric

    d2 = (u/α)2 + (v/β)2

  3. Compare to one. The point is inside the ellipse if d2 is less than one, on the ellipse if it is exactly one, and outside if it is greater than one.

like image 115
David Hammen Avatar answered Oct 13 '22 11:10

David Hammen