opencv - 找出一个圆的所有点

标签 opencv drawing geometry points

我画一个带有固定输入点的圆。现在,我真的想得到该圆圈中所有点的 vector ,其中包括内部填充区域。我尝试了下面的代码,但它只能得到边框。我不能使用轮廓函数,因为我已经使用了很多次,所以它将非常复杂。请给我建议,非常感谢

 vector<Point> allpoints;
 Point center = Point(370, 200);

void getPoints()
{
   Size axes(20, 20);
   ellipse2Poly(center, axes, 0, 0, 360, 1, allpoints);
}

void draw(Mat &BGR_frame)
{
    circle(BGR_frame, center, 20, Scalar(0, 255, 0),CV_FILLED ,2);
    getPoints();
}

最佳答案

一种简单的方法是在初始化为黑色的蒙版上绘制圆,然后从那里检索非黑点:

void draw(Mat &BGR_frame)
{
    circle(BGR_frame, center, 20, Scalar(0, 255, 0),CV_FILLED ,2);

    // Black initialized mask, same size as 'frame'
    Mat1b mask(frame.rows, frame.cols, uchar(0));

    // Draw white circle on mask
    circle(mask, center, 20, Scalar(255), CV_FILLED, 2);

    // Find non zero points on mask, and put them in 'allpoints'
    findNonZero(mask, allpoints);
}

另外,您可以扫描矩阵的所有像素,并保留满足以下条件的点作为圆的内部点:
Point c(370, 200);
int r = 20;

void draw(Mat &BGR_frame)
{        
    circle(BGR_frame, c, r, Scalar(0, 255, 0),CV_FILLED ,2);

    for (int y = 0; y < mask.rows; ++y) {
        for (int x = 0; x < mask.cols; ++x) {

            // Check if this is an internal point
            if ((x - c.x)*(x - c.x) + (y - c.y)*(y - c.y) <= (r*r)) {
                allpoints.push_back(Point(x,y));
            }
        }
    }
}

关于opencv - 找出一个圆的所有点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36951873/

相关文章:

opencv - 如何检测对象是否为 3D?

c++ - 如何传递 opencv 图像以在不同线程的 Qt 场景中显示?

python - Cmake - 使用 OpenCV 生成共享库

c++ - 遍历区域并在 OpenCV 中获取平均像素值?

c# - Metro 风格应用程序中成熟的绘画(绘图)应用程序

WPF Canvas - 单像素网格

c++ - 如何渲染一个顶点尽可能少的圆?

html - 具有属性位置(相对、绝对)或 float 的 Shape-outside

c# - 在图像上绘制缩放图片

geometry - 如何找到一般方程形式的两条线的交点?