How to blur with a controlled area?
28 views (last 30 days)
Hello, I have to be able to blur only specific part of a .jpg, however the size needs to be about 200 pixels by 200 pixels with a threshold of about plus or minus 4 pixels. The hint my teacher gave was to change the color values into the average of the surrounding plus or minus 4 color values. The last step is to recombine these three new color layers. I have not learned how to manipulate a section of the color layers so that they are blurred, any help would be greatly appreciated.
Image Analyst on 21 Nov 2012
I don't know what that means. Do you mean that you might have a, say, 10 megapixel image but you want to blur only a 200x200 +/- 4? In other words, blur an area that's somewhere between 39,996 and 40,004 pixels? How would you specify where that small area is located inside your 10 megapixel image? Here's some code snippets that may be helpful
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Mask by some binary image.
maskedRed = redChannel .* mask;
maskedGreen = greenChannel .* mask;
maskedBlue = blueChannel .* mask;
% Recombine separate channels into a single RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
maskedRgbImage = cat(3, maskedRed, maskedGreen, maskedBlue);
% Blur an image (average in a window)
blurredImage = conv2(redChannel, ones(windowWidth)/windowWidth^2, 'same');
Note: this is not a working script - it's just some snippets. See what you can get with those code statements I gave you. Write back with your program if you need more hints.
I'm not sure what it means to change a pixel into the average of the surrounding pixels plus or minus 4 color values. The pixel is either the average value or it's some other value, like the average+4 or the average-4, but you have to specify because the pixel has to have one value, not a range of values.
More Answers (1)
DGM on 22 Jun 2022
If the user has Image Processing Toolbox, using roifilt2() or even imfilter()/imgaussfilt() could simplify the task. For a small ROI within a large image, using roifilt2() can avoid excess computations, but it's limited to strictly logical (hard) masking behavior. It tends to lose a lot of that speed advantage when processing RGB images since it has to process each channel independently due to the fact that it only supports single-channel inputs. Whether that's of concern is a question for you to decide.
An example of using roifilt2() on an RGB image can be found here:
An example of doing blurring (hard and soft masks) using basic composition methods and basic MATLAB/IPT tools can be found here:
An example of doing soft-masked blurring using purpose-built composition tools can be found here (the last face-blurring example):