【问题标题】:Fill Masked Contour Intersected with Rect填充与矩形相交的蒙版轮廓
【发布时间】:2017-01-31 04:27:49
【问题描述】:

我有一个 Mat 图像,它是我分割的二进制掩码和一个标识特定区域的 cv::Rect。当我得到二进制掩码的轮廓时,图像是这样的:

Binary Mask

Contours generated

我想在与矩形相交的区域填充遮罩。我该怎么做?

提前致谢。

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    有一种比@ZdaR 的解决方案更简单的方法:使用感兴趣区域 (ROI),它直接选择要处理的边界矩形区域。

    cv::Rect boundingRect(/* x, y, width, height */);
    contours_image(boundingRect).setTo(255, binary_image(boundingRect));
    

    这里,我用运算符括号contours_image(boundingRect)binary_image(boundingRect)选择每个区域,并使用二值图像部分作为掩码,将所有对应的像素设置为255。

    【讨论】:

      【解决方案2】:

      一个不错的选择是将cv::min() 与二值图像一起使用,另一个cv::Mat() 将cv::Rect() 下的区域涂成白色。它将过滤掉 Rect 下所需的部分:

      // Create a grayscale canvas with black background
      cv::Mat canvas = cv::Mat(binary_img.size(), CV_8UC1, cv::Scalar(0));
      
      // I created a dummy rect replace it with original rect coordinates.
      cv::Rect boundingRect = cv::Rect(100, 100, 200, 200);
      
      // Draw filled rect onto the black canvas with white color
      cv::rectangle(binary_image, boundingRect, cv::Scalar(255), -1);
      
      // Take the min of binary image and canvas to filter out the contours
      cv::min(binary_image, canvas, binary_image);
      

      编辑:

      如果要过滤与cv::Rect 相交的轮廓,则需要遍历每个轮廓,计算 boundingRect 并检查它是否与给定的矩形相交。

      for (int i=0; i<contours.size(); i++) {
          if ((cv::boundingRect(contours[i]) & boundingRect).area() > 0) {
              // Your desired contours found.
          }
      }
      

      【讨论】:

      • 嗨,ZdaR。感谢您的回复!!但是,我想做的是填充与矩形相交的区域(轮廓)。在某些情况下,该区域比矩形大,在某些情况下(如图像中)它完全在内部。所以,我想确定哪个轮廓与矩形相交,并在二进制图像中填充这个区域。
      猜你喜欢
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多