首页 > 解决方案 > 图像模糊导致边缘锐利

问题描述

我正在用 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');

无论如何,获得的效果太人工了,因为模糊的圆形蒙版和图像的其余部分之间的过渡太锐利了。是否有可能使这种过渡更加褪色以获得更自然的效果?

标签: matlabimage-processing

解决方案


您可以使用原始图像和修改后图像的加权平均值,使用基于到圆心的距离的蒙版作为权重。原始图像在圆圈外部的权重更大,而修改后的图像在中心。这可能会在模糊中产生过渡。


推荐阅读