Ask Your Question

Revision history [back]

try the modified code below

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

#include <iostream>

using namespace std;
using namespace cv;

bool selectObject = false;
Rect selection;
Point origin;
int msize = 3;
Mat image;

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 = image(blurredAreas[i]);
        doMosaic(roi, msize);
    }
    imshow("Demo", image);
    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:
    {
        blurredAreas.push_back(selection);
        selectObject = false;
        selection.width = 0;
        selection.height = 0;
        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 roi = image(selection);
            bitwise_not(roi, roi);
        }
    }
}

int main(int argc, char** argv)
{
    VideoCapture cap("../data/vtest.avi");

    cap >> image;
    if (image.empty())                      // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

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

    while (true)
    {
        cap >> image;

        if (image.empty())                      // Check for invalid input
        {
            cout << "end." << std::endl;
            return -1;
        }

        if (selection.width > 0 && selection.height > 0)
        {
            Mat roi = image(selection);
            bitwise_not(roi, roi);
        }

        doBlur();

        int key = waitKey(30);

        if (key == 27)
            break;

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

        if (key == 'i') // for increasing mosaic size
        {
            msize += 3;
        }

        if (key == 'd') // key 'd'  for decreasing mosaic size
        {
            msize = msize == 3 ? 3 : msize - 3;
        }

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

try the modified code below

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

#include <iostream>

using namespace std;
using namespace cv;

bool selectObject = false;
Rect selection;
Point origin;
int msize = 3;
Mat image;

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 = image(blurredAreas[i]);
            rectangle(image, blurredAreas[i], Scalar(0, 0, 255), 2);            
            doMosaic(roi, msize);
    }
    imshow("Demo", image);
    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:
    {
        blurredAreas.push_back(selection);
        selectObject = false;
        selection.width = 0;
        selection.height = 0;
        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 roi = image(selection);
            bitwise_not(roi, roi);
        }
    }
}

int main(int argc, char** argv)
{
    VideoCapture cap("../data/vtest.avi");

    cap >> image;
    if (image.empty())                      // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

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

    while (true)
    {
        cap >> image;

        if (image.empty())                      // Check for invalid input
        {
            cout << "end." << std::endl;
            return -1;
        }

        if (selection.width > 0 && selection.height > 0)
        {
            Mat roi = image(selection);
            bitwise_not(roi, roi);
        }

        doBlur();

        int key = waitKey(30);

        if (key == 27)
            break;

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

        if (key == 'i') // for increasing mosaic size
        {
            msize += 3;
        }

        if (key == 'd') // key 'd'  for decreasing mosaic size
        {
            msize = msize == 3 ? 3 : msize - 3;
        }

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

try the modified code below

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

#include <iostream>

using namespace std;
using namespace cv;

bool selectObject = false;
Rect selection;
Point origin;
int msize = 3;
Mat image;

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 = image(blurredAreas[i]);
            rectangle(image, blurredAreas[i], Scalar(0, 0, 255), 2);            
            doMosaic(roi, msize);
    }
    imshow("Demo", image);
    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:
    {
        blurredAreas.push_back(selection);
        selectObject = false;
        selection.width = 0;
        selection.height = 0;
        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 roi = image(selection);
            bitwise_not(roi, //bitwise_not(roi, roi);
        }
    }
}

int main(int argc, char** argv)
{
    VideoCapture cap("../data/vtest.avi");

    cap >> image;
    if (image.empty())                      // Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

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

    while (true)
    {
        cap >> image;

        if (image.empty())                      // Check for invalid input
        {
            cout << "end." << std::endl;
            return -1;
        }

        if (selection.width > 0 && selection.height > 0)
        {
            Mat roi = image(selection);
            bitwise_not(roi, //bitwise_not(roi, roi);
            rectangle(image, selection, Scalar(0, 0, 255), 2);
        }

        doBlur();

        int key = waitKey(30);

        if (key == 27)
            break;

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

        if (key == 'i') // for increasing mosaic size
        {
            msize += 3;
        }

        if (key == 'd') // key 'd'  for decreasing mosaic size
        {
            msize = msize == 3 ? 3 : msize - 3;
        }

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