【问题标题】:Blur content from a rectangle with Opencv使用 Opencv 模糊矩形中的内容
【发布时间】:2020-01-29 12:04:37
【问题描述】:

在下面的矩形函数中,绘制矩形。

// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame)
{
    //Draw a rectangle displaying the bounding box
    rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50),LINE_4);

    //bluring region
    cout << frame; 

    //Get the label for the class name and its confidence
    string label = format("%.2f", conf);
    if (!classes.empty())
    {
        CV_Assert(classId < (int)classes.size());
        label = classes[classId] + ":" + label;
    }

    //Display the label at the top of the bounding box
    int baseLine;
    Size labelSize = getTextSize(label, FONT_ITALIC, 0.5, 1, &baseLine);
    top = max(top, labelSize.height);
    putText(frame, label, Point(left, top), FONT_ITALIC, 0.5, Scalar(255, 255, 255), 1);
}

frame 这里是图像的多数组。 Point(left, top) 是矩形的左上角。
我想以模糊的形式审查这个矩形中的所有内容。 由于我来自 Python 编程,定义这些矩形的数组有点困难。 如果你能帮助我,那就太好了。 非常感谢和最好的问候。

【问题讨论】:

    标签: c++ arrays image opencv image-processing


    【解决方案1】:

    可行的方法是使用cv::Rect 设置相应的感兴趣区域 (ROI)。由于您已经将左上角和右下角的位置设置为cv::Points,因此您或多或少地免费获得了它。之后,只需在该 ROI 上使用 - 例如 - cv::GaussianBlur。使用 C++ API,这种方法适用于很多 OpenCV 方法。

    代码比较简单,看下面sn-p:

    // (Just use your frame instead.)
    cv::Mat image = cv::imread("path/to/your/image.png");
    
    // Top left and bottom right cv::Points are already defined.
    cv::Point topLeft = cv::Point(60, 40);
    cv::Point bottomRight = cv::Point(340, 120);
    
    // Set up proper region of interest (ROI) using a cv::Rect from the two cv::Points.
    cv::Rect roi = cv::Rect(topLeft, bottomRight);
    
    // Only blur image within ROI.
    cv::GaussianBlur(image(roi), image(roi), cv::Size(51, 51), 0);
    

    对于像这样的一些示例性输入

    以上代码生成如下输出:

    希望有帮助!

    【讨论】:

      【解决方案2】:

      这是与@HansHirse 的答案等效的 Python。思路是一样的,只是我们使用 Numpy 切片来获得 ROI

      import cv2
      
      # Read in image
      image = cv2.imread('1.png')
      
      # Create ROI coordinates
      topLeft = (60, 40)
      bottomRight = (340, 120)
      x, y = topLeft[0], topLeft[1]
      w, h = bottomRight[0] - topLeft[0], bottomRight[1] - topLeft[1]
      
      # Grab ROI with Numpy slicing and blur
      ROI = image[y:y+h, x:x+w]
      blur = cv2.GaussianBlur(ROI, (51,51), 0) 
      
      # Insert ROI back into image
      image[y:y+h, x:x+w] = blur
      
      cv2.imshow('blur', blur)
      cv2.imshow('image', image)
      cv2.waitKey()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-30
        • 2014-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多