繁体   English   中英

如何在图像上绘制模糊的矩形?

[英]How can I draw a blurring rectangle on the image?

我找到了一个代码,可以让您在凸轮上绘制一个模糊的矩形

我尝试将其从在凸轮上绘制模糊的矩形更改为图像,因为我想在图像上绘制模糊的矩形。

这是我的代码

 #include < stdio.h>  
 #include < iostream>  
 #include < opencv2\opencv.hpp>  
 #include < opencv2/core/core.hpp>  
 #include < opencv2/highgui/highgui.hpp>  
 #include < opencv2/video/background_segm.hpp>  
 #include < opencv2\gpu\gpu.hpp>  
 #include < opencv2\legacy\legacy.hpp>  

 using namespace std;
 using namespace cv;

 bool selectObject = false;
 Rect selection;
 Point origin;
 int trackObject = 0;
 Mat image;

 static void onMouse(int event, int x, int y, int, void*)
 {
 if (selectObject)
{
    selection.x = MIN(x, origin.x);
    selection.y = MIN(y, origin.y);
    selection.width = std::abs(x - origin.x);
    selection.height = std::abs(y - origin.y);

    selection &= Rect(0, 0, image.cols, image.rows);
} 

switch (event)
{
case CV_EVENT_LBUTTONDOWN:

    origin = Point(x, y);
    selection = Rect(x, y, 0, 0);
    selectObject = true;
    break;
case CV_EVENT_LBUTTONUP:
    selectObject = false;
    if (selection.width > 0 && selection.height > 0)
        trackObject = -1;
    break;

  }
}


 int main(void)
 {
Mat img;
img = imread("C:/Users/faho0odywbas/Desktop/test.jpg");
Mat frame;
namedWindow("Demo");
setMouseCallback("Demo", onMouse);

for (;;)
{

     frame.copyTo(image);


    if (selectObject && selection.width > 0 && selection.height > 0)
    {
        Mat roi(image, selection);
        bitwise_not(roi, roi);
        printf("%d %d %d %d\n", selection.x, selection.y, selection.width, selection.height);
    }
    while (1){
    imshow("Demo", img);

    if (waitKey(10) > 10)
        break;
    }
}

return 0;
 }

代码显示图像,但是我不能在其上绘制矩形!

我想念什么?

谢谢

我对您的来源做了一些修改。 几乎完成了您想要的一切!

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>

using namespace std;
using namespace cv;

bool selectObject = false;
Rect selection;
Point origin;
int msize = 5;
Mat image,blurredImage;

vector<Rect> blurredAreas;

static bool doMosaic( Mat img, int msize)
{
    for (int i = 0; i < img.cols-msize; i+=msize)
        for(int j = 0; j < img.rows-msize; j+=msize)
        {
            Rect r = Rect(i,j,msize,msize);
            Mat mosaic = img( r );
            mosaic.setTo(mean(mosaic));
        }
    return true;
}

static bool doBlur()
{
    for(size_t i = 0; i< blurredAreas.size(); i++)
    {
        Mat roi = blurredImage(selection);
        //GaussianBlur(roi,roi,Size(),5,5);
        doMosaic(roi,msize);
    }
    imshow("Demo", blurredImage);
    return true;
}



static void onMouse(int event, int x, int y, int, void*)
{
    switch (event)
    {
    case CV_EVENT_LBUTTONDOWN:
        origin = Point(x, y);
        selectObject = true;
        break;
    case CV_EVENT_LBUTTONUP:
    {
        selectObject = false;
        blurredAreas.push_back(selection);
        doBlur();
        break;
    }
    }

    if (selectObject)
    {
        selection.x = MIN(x, origin.x);
        selection.y = MIN(y, origin.y);
        selection.width = std::abs(x - origin.x)+1;
        selection.height = std::abs(y - origin.y)+1;
        selection &= Rect(0, 0, image.cols, image.rows);

        if ( selection.width > 0 && selection.height > 0)
        {
            Mat blurredImagecopy;
            blurredImage.copyTo(blurredImagecopy);
            Mat roi = blurredImagecopy(selection);
            bitwise_not(roi, roi);
            imshow("Demo", blurredImagecopy);
        }
    }
}


int main(void)
{
    image = imread("test.jpg");
    image.copyTo(blurredImage);

    namedWindow("Demo");
    setMouseCallback("Demo", onMouse );

    imshow("Demo", image);

    while( true )
    {
        int key = waitKey(0);

        if( key == 27 )
            break;

        if( key == 's' ) // saves result image
        {
          imwrite("result.jpg",blurredImage);
        }

        if( key == 'i' ) // space key for clear blurred areas
        {
            msize +=5;
            image.copyTo(blurredImage);
            doBlur();
        }

        if( key == 'd' ) // space key for clear blurred areas
        {
            msize = msize == 5 ? 5 : msize - 5;
            image.copyTo(blurredImage);
            doBlur();
        }

        if( key == 32 ) // space key for clear blurred areas
        {
            blurredAreas.clear();
            image.copyTo(blurredImage);
            doBlur();
        }
    }
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM