【问题标题】:How to create circular mask for Mat object in OpenCV / C++?如何在 OpenCV / C++ 中为 Mat 对象创建圆形掩码?
【发布时间】:2015-05-10 08:18:23
【问题描述】:

我的目标是在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;

...

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...根据我的理解,这段代码应该工作,因为circle 应该使用有关centerradius 的信息来修改circleROI,这样圆圈区域内的所有点都应该设置为1... 有没有人向我解释我做错了什么?我是否对问题采取了正确的方法,但实际问题可能在其他地方(这也很有可能,因为我是 C++ 和 OpenCv 的新手)?

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

【问题讨论】:

    标签: c++ opencv mat


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      这是因为您正在用大垫子中的圆坐标填充 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);
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-27
        • 2019-01-12
        • 2021-10-13
        • 1970-01-01
        相关资源
        最近更新 更多