找出圆圈内的所有点
find all points in a circle
我用固定的输入点画了一个圆。现在我真的很想获得该圆圈中所有点的矢量,包括内部填充区域。我尝试了下面的代码,但它只得到了边框。我不能使用 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();
}
一种简单的方法是在黑色初始化蒙版上绘制圆圈,并从那里检索非黑色点:
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));
}
}
}
}
我用固定的输入点画了一个圆。现在我真的很想获得该圆圈中所有点的矢量,包括内部填充区域。我尝试了下面的代码,但它只得到了边框。我不能使用 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();
}
一种简单的方法是在黑色初始化蒙版上绘制圆圈,并从那里检索非黑色点:
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));
}
}
}
}