Blur edges of rectangles in the image
9 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Image Analyst
2020-7-19
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);
2 个评论
Jhe Mag
2022-6-28
Hi @Image Analyst what am I going to do if I am to input RGB image? I tried your code and run it with my image. It says errors like
Error using bwpropfilt
Expected input number 1, BW, to be two-dimensional.
Image Analyst
2022-6-28
@Jhe Mag You can use imsplit() and then process each color channel one at a time.
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
If you have any more questions, then start a new discussion thread and attach your data and code to read it in with the paperclip icon after you read this:
更多回答(1 个)
Raunak Gupta
2020-7-19
Hi,
If you know the corner points of rectangles in the image, then you can extract a matrix around those corners of rectangle with window size depending upon how much area you want to blur. Then you can apply imgaussfilt3 filter on that matrix which will make it blurred. Finally, you can replace the small matrix in the original image with the blurred matrix.
For example, in the image (size 224 x 224 x 3) given in the question one rectangle corner lies at (60,90) then let's say we take a window of 3 around that point.
I = imread('example_iamge.png');
windowSize = 3;
sigma = 2;
cornerPointMatrix = I(60-windowSize:60+windowSize,90-windowSize:90+windowSize,:);
cornerPointMatrixBlur = imgaussfilt3(cornerPointMatrix, sigma);
I(60-windowSize:60+windowSize,90-windowSize:90+windowSize,:) = cornerPointMatrixBlur;
2 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!