我正在用MATLAB对RGB图像进行一些处理。 我必须获得如下图所示的圆形模糊:
通过以下代码获得:
A = imread('lena .bmp');
I = rgb2gray(A);
[rNum,cNum,~] = size(I);
%center and radius of the circular mask
x1 = 256.5;
y1 = 256.5;
radius = 100;
%circular mask creation
[x,y] = ndgrid((1:rNum)-y1,(1:cNum)-x1);
mask = (x.^2 + y.^2)<radius^2;
h = ones(30,30)/900; %gaussian filter
J = roifilt2(h,I,mask); %apply the filter at the mask
%filtering plane - by - plane in order to apply the circular blurred mask
%to the RGB image
filtered_im = zeros(size(A));
filtered_im(:,:,1) = roifilt2(h, A(:,:,1), mask);
filtered_im(:,:,2) = roifilt2(h, A(:,:,2), mask);
filtered_im(:,:,3) = roifilt2(h, A(:,:,3), mask);
filtered_im = uint8(filtered_im);
figure
imshow(filtered_im)
title('Circular blurring RGB image');
无论如何,由于模糊的圆形蒙版和图像其余部分之间的过渡太清晰,因此获得的效果太人为。是否有可能使这种过渡更加淡化以获得更自然的效果?
答案 0 :(得分:3)
您可以使用原始图像和修改图像的加权平均值,并根据与圆心的距离使用蒙版作为加权。 原始图像将在圆的外部具有更大的权重,而修改后的图像将在中心具有更大的权重。这可能会导致模糊过渡。