如何在OpenCV / C ++中为Mat对象创建圆形蒙版?

nbu*_*urk 3 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
Run Code Online (Sandbox Code Playgroud)

...修改它使得我获得“圆形”1内它S,SO .eg

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
Run Code Online (Sandbox Code Playgroud)

我目前正在使用以下代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

问题是,我的电话之后circle,有最多只有一个字段的circleROI设置来1......据我的了解,此代码应工作,因为circle应该使用有关的信息centerradius修改circleROI,使得所有的点是圈内的“应该设置为1...”是否有人对我有解释我在做什么错?我是否对问题采取了正确的方法,但实际问题可能还在其他地方(这也是很有可能的,因为我是C ++和OpenCv的新手)?

请注意,我还尝试将circle调用中的最后一个参数(即圆形轮廓粗细)修改为1-1,而没有任何效果。

Ha *_*ang 5

这是因为您要用大垫子中的圆坐标填充您的circleROI。您在circleROI内的圆坐标应相对于circleROI,在您的情况下为:new_center =(c.radius,c.radius),new_radius = c.radius。

这是循环的代码段:

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

}
Run Code Online (Sandbox Code Playgroud)