Ask Your Question

Revision history [back]

( i don't know if i am doing right but I can not stop myself from writing code as answer)

here the code you want almost done!

#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 ? msize - 5 : 5;
            image.copyTo(blurredImage);
            doBlur();
        }

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

( i don't know if i am doing right but I can not stop myself from writing code as answer)

here the code you want almost done!

#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
increasing mosaic size
        {
            msize +=5;
            image.copyTo(blurredImage);
            doBlur();
        }

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

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

( i don't know if i am doing right but I can not stop myself from writing code as answer)

here the code you want almost done!

sample output :

image description

#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' ) // for increasing mosaic size
        {
            msize +=5;
            image.copyTo(blurredImage);
            doBlur();
        }

        if( key == 'd' ) // key 'd'  for decreasing mosaic size
        {
            msize = msize < 5 ? msize - 5 : 5;
            image.copyTo(blurredImage);
            doBlur();
        }

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

( i don't know if i am doing right but I can not stop myself from writing code as answer)

here the code you want almost done!

sample output :: ( you can increase and decrease mosaic effect by pressing "i" and "d")

image descriptionimage description

#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' ) // for increasing mosaic size
        {
            msize +=5;
            image.copyTo(blurredImage);
            doBlur();
        }

        if( key == 'd' ) // key 'd'  for decreasing mosaic size
        {
            msize = msize < == 5 ? 5 : msize - 5 : 5;
            image.copyTo(blurredImage);
            doBlur();
        }

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

( i don't know if i am doing right but I can not stop myself from writing code as answer)

here the code you want almost done!

sample output : ( you can increase and decrease mosaic effect by pressing "i" and "d")

image descriptionimage description

#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)
main( int argc, char** argv )
{
    image = imread("test.jpg");
imread( argv[1] );
    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' ) // for increasing mosaic size
        {
            msize +=5;
            image.copyTo(blurredImage);
            doBlur();
        }

        if( key == 'd' ) // key 'd'  for decreasing mosaic size
        {
            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;
}

( i don't know if i am doing right but I can not stop myself from writing code as answer)

here the code you want almost done!

sample output : ( you can increase and decrease mosaic effect by pressing "i" and "d")

image descriptionimage description

#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( int argc, char** argv )
{
    image = imread( argv[1] );
    if( image.empty() )                      // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }
    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' ) // for increasing mosaic size
        {
            msize +=5;
            image.copyTo(blurredImage);
            doBlur();
        }

        if( key == 'd' ) // key 'd'  for decreasing mosaic size
        {
            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;
}