找到圆圈中的所有点

时间:2016-04-30 07:03:16

标签: opencv drawing geometry points

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

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

1 个答案:

答案 0 :(得分:1)

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

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