使用Opencv模糊矩形中的内容 [英] Blur content from a rectangle with Opencv

查看:38
本文介绍了使用Opencv模糊矩形中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 //绘制预测的边界框void drawPred(int classId,float conf,int left,int top,int right,int bottom,Mat& frame){//绘制一个显示边框的矩形矩形(frame,Point(left,top),Point(right,bottom),Scalar(255,178,50),LINE_4);//起泡区域cout<<框架;//获取类名称及其置信度的标签字符串标签= format(%.2f",conf);如果(!classes.empty()){CV_Assert(classId<(int)classes.size());label = classes [classId] +:" +标签;}//在边界框顶部显示标签int baseLine;大小labelSize = getTextSize(label,FONT_ITALIC,0.5,1,& baseLine);顶部=最大(顶部,labelSize.height);putText(frame,label,Point(left,top),FONT_ITALIC,0.5,Scalar(255,255,255),1);} 

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

解决方案

方法是使用

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

希望有帮助!

in the following rectangle function, rectangles are drawn.

// 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 here is a multi-array of the image. Point(left, top) is the top-left point of the rectangle.
I would like to censor everything in this rectangle in the form of a blur. Since I come from Python programming, it is a bit difficult to define the array of these rectangles. It would be very nice if you could help me. Thank you very much and best regards.

解决方案

The way to go is setting up a corresponding region of interest (ROI) by using cv::Rect. Since you already have your top left and bottom right locations as cv::Points, you get this more or less for free. Afterwards, just use - for example - cv::GaussianBlur only on that ROI. Using the C++ API, this approach works for a lot of OpenCV methods.

The code is quite simple, see the following snippet:

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

For some exemplary input like this

the above code generates the following output:

Hope that helps!

这篇关于使用Opencv模糊矩形中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆