Ask Your Question

Draw a blurring rectangle

asked 2015-09-17 12:03:09 -0500

John Sami gravatar image

Goal – to provide an easy way to specify one or multiple regions on an image for mosaic effect via mouse operations.

Steps (1) Load an image from your project local drive, say “test.jpg” and display it by OpenCV.

(The example image )

image description

(2) For each region, user just needs two left-clicks to define it. The positions where the mouse clicks occur are considered as the two corners along a diagonal line of a rectangular,

image description

(3) Pixels inside the rectangular region that defined by the user should be blurred with Mosaic effect. The level of blur depends on the user specified value. For example, you can use an integer variable to store it: int blur_degree = 5 Here “blur_degree” defines the length of each Mosaic unit square, By default it is 5 pixels in length. So the bigger of the “blur_degree”, the more blur of the selected region

image description

(4) User can increase the blur level by hitting the keyboard key “i” or “I” and decrease the blur level by hitting the keyboard key “d” or “D”. The way of increasing or decreasing blur level follows the rule below:

a) Increasing Case: when “blur_degree” is less than 5, it increases by 1 each time when the key “i” or “I” is hit; when “blur_degree” is equal to or greater than 5, it increases by 5 each time when the key “i” or “I” is hit;

b) Decreasing Case: when the “blur_degree” is equal to or less than 5 but greater than 1, it decreases by 1 each time when the key “d” or “D” is hit; when “blur_degree” is greater than 5, it decreases by 5 each time when the key “d” or “D” is hit;

(5) During editing, user can remove the blur effect at any time by hitting the key “r” or “R”, just in case the region is not selected properly.

(6) After the user finishes editing, the result image should be saved to a local drive with whatever filename by hitting the “s“ or “S” key.

Thanks a lot

edit retag flag offensive close merge delete



And what is the question? Have you even tried to write some code? You should show some effort in implementing your program or something...

LorenaGdL gravatar imageLorenaGdL ( 2015-09-17 18:41:09 -0500 )edit

This is my code :

Mat image; char click; Point p1; Point p2; static void onMouse(int event, int x, int y, int, void*) {

switch (event)
        if (click == 0){ // first click  top left
    click = 1;
    printf("%d %d", x, y);

    else {          // second click  bottom right
    click = 2;
    printf("%d %d", x, y);



void blurImage() { // compute the block

int Num_cols = (p2.x - p1.x) / block;
int Num_row = (p1.x - p2.x) / block;

for (int i = 0; i < Num_row; i++)
for(int j = 0; j < Num_cols; j++)
    int  x1 = p1.x + j * block;
    int  y1 = p1.y + j * block;
    int avg = sum / block*block;

// assign each pixel value by the block value }

How can I compute the block and assign each pixel ?

John Sami gravatar imageJohn Sami ( 2015-09-18 10:29:36 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2015-09-17 18:52:36 -0500

(1) Take a look display_image.cpp

(2) You can find a solution how to select a region by mouse clicks Take a look opencv_annotation.cpp ( you can find a sample modified code based on opencv_annotation.cpp here ) or another sample code here

(3) i think there is no built-in mosaic effect like you wanted, but it will be solved anyway

(4),(5),(6) you can solve them using waitKey and imwrite

edit flag offensive delete link more


what is your suggestion ?

sturkmen gravatar imagesturkmen ( 2015-09-18 09:49:33 -0500 )edit

Thanks a lot for your help, but I need a solve for number (3) because I need draw a blur rectangle not a normal rectangle , thanks again

John Sami gravatar imageJohn Sami ( 2015-09-18 09:55:23 -0500 )edit

first you try to implement your program as far as you can then ask about missing part.

sturkmen gravatar imagesturkmen ( 2015-09-18 10:20:03 -0500 )edit

Question Tools

1 follower


Asked: 2015-09-17 12:03:09 -0500

Seen: 1,572 times

Last updated: Sep 17 '15