使用Opencv模糊矩形中的内容

dep*_*rai 5 c++ arrays opencv

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

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

此处的帧是图像的多阵列。Point(left,top)是矩形的左上角。
我想以模糊的形式检查此矩形中的所有内容。由于我来自Python编程,因此很难定义这些矩形的数组。如果您能帮助我,那将是非常好的。非常感谢。

nat*_*ncy 7

这是与@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()
Run Code Online (Sandbox Code Playgroud)


Han*_*rse 6

方法是使用来设置相应的兴趣区(ROI)cv::Rect。由于您的左上角和右下角位置已经为cv::Points,因此您或多或少都可以免费获得该位置。之后,仅使用-例如- cv::GaussianBlur仅在该ROI上使用。使用C ++ API,此方法可用于许多OpenCV方法。

代码非常简单,请参见以下代码段:

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

对于这样的示例性输入

输入项

上面的代码生成以下输出:

输出量

希望有帮助!