OpenCV在椭圆上获取像素

发布于 2025-01-20 19:37:38 字数 734 浏览 2 评论 0原文

我正在尝试从图像中获取椭圆的像素。

例如,我在随机图像上绘制一个椭圆(示例 geeksforgeeks 代码):

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)

它给出如下输出: 输入图片description here

现在,我想获取椭圆边界线的像素值。如果可能的话,我想使用 cv2.ellipse() 获取椭圆的像素作为坐标数组。
任何人都可以帮我解决这个问题吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹂绝世的画 2025-01-27 19:37:38

如果要在椭圆上获取点(位置),则可以使用ellipse2poly()函数。


如果ellipse2poly()的参数类型是不便的,则自己计算是最方便的方法。

该示例代码为C ++,但计算出的是明确的。

//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 );
    }
}

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 );
    }
}
鹊巢 2025-01-27 19:37:38

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.
在这里,我所做的是创建椭圆形的掩码图像,并找到了所需的最外部轮廓点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文