0

I'm trying to get the pixels of an ellipse from an image.

For example, I draw an ellipse on a random image (sample geeksforgeeks code):

import cv2
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
image = cv2.imread(path)
window_name = 'Image'
center_coordinates = (120, 100)
axesLength = (100, 50)
angle = 0
startAngle = 0
endAngle = 360
color = (0, 0, 255)
thickness = 5
image = cv2.ellipse(image, center_coordinates, axesLength,
           angle, startAngle, endAngle, color, thickness)
cv2.imshow(window_name, image)

It gives output like below: enter image description here

Now, I want to get the pixel value of boundary line of ellipse. If it is possible I would like to get the pixel of ellipse using cv2.ellipse() back as an array of coordinates.
Can anyone help me with this please.

ArenMayank
  • 17
  • 4
  • 1
    Suggestion : draw ellipse on a other image of same size of initial image. This other image is init to black (0). Ellipse is draw in white (255). Scan (loop on Y and X coordinate) this image to search all pixels not black (not to 0), at each pixel not black, use same coordinate (X,Y) on your image for having pixel value. – Emmanuel DUMAS Apr 13 '22 at 05:42

2 Answers2

1

There is no direct OpenCV way probably to get these points of the ellipse but you can extract your points via indirect way like this:

mask = cv2.inRange(image, np.array(color), np.array(color))
contour = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2][0]

contour will store the outer points of your red ellipse. Here, what I have done is created a mask image of the ellipse and found the externalmost contour's points that is the required thing.

Rahul Kedia
  • 1,400
  • 6
  • 18
1

If you want to obtain points (locations) on an ellipse, you can use ellipse2Poly() function.


If the argument type of ellipse2Poly() is inconvenient, calculating by yourself is most convenient way.

This sample code is C ++, but what calculated is clear.

//Degree -> Radian
inline double RadFromDeg( double Deg ){ return CV_PI*Deg/180.0; }

//Just calculate points mathematically.
//  Arguments are same as cv::ellipse2Poly (alothough ellipse parameters is cv::RotateRect).
void My_ellipse2Poly(
    const cv::RotatedRect &EllipseParam,
    double StartAngle_deg,
    double EndAngle_deg,
    double DeltaAngle_deg,
    std::vector< cv::Point2d > &DstPoints
)
{
    double Cos,Sin;
    {
        double EllipseAngleRad = RadFromDeg(EllipseParam.angle);
        Cos = cos( EllipseAngleRad );
        Sin = sin( EllipseAngleRad );
    }

    //Here, you will be able to reserve the destination vector size, but in this sample, it was omitted.
    DstPoints.clear();

    const double HalfW = EllipseParam.size.width * 0.5;
    const double HalfH = EllipseParam.size.height * 0.5;
    for( double deg=StartAngle_deg; deg<EndAngle_deg; deg+=DeltaAngle_deg )
    {
        double rad = RadFromDeg( deg );
        double u = cos(rad) * HalfW;
        double v = sin(rad) * HalfH;
        double x = u*Cos + v*Sin + EllipseParam.center.x;
        double y = u*Sin - v*Cos + EllipseParam.center.y;
        DstPoints.emplace_back( x,y );
    }
}
fana
  • 1,370
  • 2
  • 7
  • Thanks for the suggestion!!. Could have been super if float was acceptable as delta input (parameter of above function). Because I want 16 equidistant points out of ellipse. – ArenMayank Apr 21 '22 at 11:18
  • As I added to this answer, "points on ellipse arc" is very easy math problem, so if pre-implemented function is inconvenient, do it yourself may be best way. – fana Apr 22 '22 at 01:44