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.

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.

lexma source
share