MATLAB: Blur edges of rectangles in the image

digital image processingdigital signal processingfilterimageimage analysisimage processingImage Processing Toolboximage segmentation

I have the intensity image as shown bleow. I want to blur the corners of two rectangles (shown in image) so that it doesn't look like perfect rectangle.
How can i do that?

Best Answer

kumara, try this. Adapt parameters as needed to control the size and amount of the blur.
% Demo to blur edges of rectangles, but not the insides.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing
fontSize = 15;
% Get image.
grayImage = abs(randn(224,224)+50);
m =2*abs(randn(90-10+1,60-10+1)+22); %first rectangle
grayImage(10:90, 10:60) = m;
m1 =3*abs(randn(110-95+1,60-10+1)+19); %second rectangle
grayImage(95:110, 10:60) = m1;
[rows, columns, numberOfColorChannels] = size(grayImage)
subplot(2, 2, 1);
imagesc(grayImage)
impixelinfo; % Let user see RGB values as they mouse around.

colorbar
axis('on', 'image');
title('Original Image', 'FontSize', fontSize);
% Maximize the window to make it easier to draw.
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m ...\n', mfilename);
% Get mask
mask = grayImage > 53 | grayImage < 47;
mask = imfill(mask, 'holes'); % Fill holes.
mask = bwareafilt(mask, 2); % Take 2 largest blobs only.
mask = bwconvhull(mask, 'objects'); % Take convex hull.
subplot(2, 2, 2);
% histogram(originalImage)
imshow(mask);
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize);
% Get larger and smaller masks by dilating and eroding.
r = 3; % Radius for blur
se = strel('disk', r, 0);
bigMask = imdilate(mask, se);
smallMask = imerode(mask, se);
% xor to get outline.
borderMask = xor(bigMask, smallMask);
subplot(2, 2, 3);
imshow(borderMask);
axis('on', 'image');
title('Border Mask Image', 'FontSize', fontSize);
% Blur the entire image
windowSize = 5;
kernel = ones(windowSize) / windowSize^2;
blurredImage = imfilter(grayImage, kernel);
% Now replace the gray image inside the border mask with the blurred values.
grayImage(borderMask) = blurredImage(borderMask);
subplot(2, 2, 4);
imagesc(grayImage)
impixelinfo; % Let user see RGB values as they mouse around.
colorbar
axis('on', 'image');
title('Final Blurred Image', 'FontSize', fontSize);