C++ 如何在OpenCV/C+中为Mat对象创建圆形遮罩+;?
我的目标是在C++ 如何在OpenCV/C+中为Mat对象创建圆形遮罩+;?,c++,opencv,mat,C++,Opencv,Mat,我的目标是在Mat对象上创建一个圆形遮罩,例如,对于看起来像这样的Mat: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 …对其进行修改,以便我在其中获得一个1s的圆形,例如 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 我目前正在使用以下代码: typedef struct { double radius; Point center; } Circle; .
Mat
对象上创建一个圆形遮罩,例如,对于看起来像这样的Mat
:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
…对其进行修改,以便我在其中获得一个1
s的圆形,例如
0 0 0 0 0
0 0 1 0 0
0 1 1 1 0
0 0 1 0 0
0 0 0 0 0
我目前正在使用以下代码:
typedef struct {
double radius;
Point center;
} Circle;
...
for (Circle c : circles) {
// get the circle's bounding rect
Rect boundingRect(c.center.x-c.radius, c.center.y-c.radius, c.radius*2,c.radius*2);
// obtain the image ROI:
Mat circleROI(stainMask_, boundingRect);
int radius = floor(radius);
circle(circleROI, c.center, radius, Scalar::all(1), 0);
}
问题是,在我调用circle
之后,circleROI
中最多只有一个字段设置为1
。。。根据我的理解,此代码应该有效,因为圆
应该使用有关中心
和半径
的信息来修改圆
,以便圆区域内的所有点都应该设置为1
。。。有人能解释我做错了什么吗?我是否正确地处理了这个问题,但是实际问题可能在别的地方(这也是非常可能的,因为我是C++和OpenCv的新手)?< /P>
请注意,我还尝试将
circle
调用中的最后一个参数(即圆轮廓的厚度)修改为1
和-1
,但没有任何效果。查看:getStructuringElement
这是因为你在用大垫子中的圆坐标填充圆。圆内的圆坐标应相对于圆,在您的情况下,即:新的_中心=(c.radius,c.radius),新的_半径=c.radius 以下是循环的snipcode:
for (Circle c : circles) {
// get the circle's bounding rect
Rect boundingRect(c.center.x-c.radius, c.center.y-c.radius, c.radius*2+1,c.radius*2+1);
// obtain the image ROI:
Mat circleROI(stainMask_, boundingRect);
//draw the circle
circle(circleROI, Point(c.radius, c.radius), c.radius, Scalar::all(1), -1);
}