【问题标题】:find all points in a circle找到一个圆圈中的所有点
【发布时间】:2016-04-30 17:58:10
【问题描述】:

我用固定的输入点画了一个圆。现在我真的很想得到那个圆圈中所有点的向量,包括里面的填充区域。我尝试了下面的代码,但它只得到了边框。我不能使用 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();
}

【问题讨论】:

  • 一些图像和更好的解释/问题定义会有所帮助。问题不清楚。
  • 您是否考虑过计算距所选中心的欧几里得距离?如果它小于半径(20),您可以 push_back() 向量中的点

标签: opencv drawing geometry points


【解决方案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));
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 2017-06-13
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    相关资源
    最近更新 更多