Finding the distance from a point to an ellipse, humidity inside or outside the ellipse

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

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

enter image description here

Below is the code to determine the distance of each point to an ellipse. If the distance, D <1, then this means that the point is inside the ellipse, in which case I make it gray. If D> 1, then this means that the point is outside the ellipse, in which case I leave it as it is. The image I get is also shown below. For some reason, I think my distance calculation is correct, but I have a problem with my rotation. For me, everything looks right, I do not see a problem. Please help. I require all the pixels in the ellipse to be gray, but for me the gray area forms an ellipse, but it seems like I'm 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 result that I get. The gray area should be in a pink ellipse. enter image description here

+5
source share
3 answers

- ,

. . ( , ). .

Google - . PDF , : http://www.geometrictools.com/Documentation/DistancePointEllipseEllipsoid.pdf.

Edit
, , OP.

lexma, , , , , tta x. , - (x, y) , .

  • (x, y) (u, v), u, v.

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

  • d 2= (u/α) 2 + (v/β) 2

  • . , d 2 , , , , .

+3

, ,

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

. , . , :

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

, c-d c+d

0

Although the solution in this article is definitely too expensive to determine if the point is inside, on the ellipse, or outside it, it can still help those people who came here using Google mislead the first part of the title. (like me)

0
source

All Articles