使用平均滤波器部分模糊图像

时间:2021-02-05 20:36:50

标签: image function matlab image-processing imagefilter

我创建这个函数是为了部分模糊图像:

function [ T ] = floutage(I,XA,YA,XB,YB)
H=fspecial('average',[11 11]);
t=0;
for i=XA:XB
    for j=YA:YB
        t=imfilter(I(i,j),H) ; 
        I(i,j)=t;
    end   
end
T=I;
end

我在脚本中这样调用它:

T1=floutage(Iref,10,10,350,350);
figure
imshow(T1);

但结果不是模糊而是一个黑框:

the result that i got

2 个答案:

答案 0 :(得分:3)

图像的模糊部分/分区

imfilter() 函数涵盖/处理卷积过程,允许我们跳过实现 for 循环。为了过滤图像的一部分,该部分可以直接传递给 imfilter() 函数,模糊部分将被返回。模糊部分返回后,可以通过索引覆盖原始图像的相应部分。

为了处理沿边框的填充,我使用了 'replicate' 属性,这是众多处理方法之一:MATLAB Documentation: imfilter; Boundary Options

Blurring Portion/Partition of Image

Iref = rgb2gray(imread("peppers.png"));
[T1] = floutage(Iref,10,10,350,350);
imshow(T1);

function [Iref] = floutage(Iref,XA,YA,XB,YB)
H = fspecial('average',[11 11]);

%Grabbing portion of image to be blurred%
Sub_Image = Iref(YA:YB,XA:XB);

%Filtering/convolving the portion of the image%
Blurred_Sub_Image = imfilter(Sub_Image,H,'replicate'); 

%Replacing the old portion with the blurred portion%
Iref(YA:YB,XA:XB) = Blurred_Sub_Image; 
end

答案 1 :(得分:2)

这里有一个简单的 MichaelTr7's answer 替代方法。我们不是仅在图像区域上应用模糊,而是将其应用于整个图像(无论如何这很便宜),然后切出一部分模糊结果以粘贴到原始图像中。一般来说,这可以更好地表示朝向框边缘的模糊:

img = rgb2gray(imread('peppers.png'));
out = floutage(img,120,250,180,310);
imshow(out);

function img = floutage(img, XA, YA, XB, YB)
% Filter the whole image
H = fspecial('average', [11 11]);
blur = imfilter(img, H); 
% Write blurry box into image
img(YA:YB, XA:XB) = blur(YA:YB, XA:XB);
end

output of function floutage

这种方法的一个优点是它直接导致了一种避免模糊框锐边的方法:应用羽毛。这是一种实现方式:

function img = floutage2(img, XA, YA, XB, YB)
% We'll be working with doubles
img = double(img);
% Filter the whole image
H = fspecial('average', [11 11]);
blur = imfilter(img, H); 
% Create mask
mask = zeros(size(blur));
mask(YA:YB, XA:XB) = 1;
mask = imfilter(mask, H);
% Write blurry box into image by weighting with mask
img = blur .* mask + img .* (1 - mask);
% Convert back to uint8
img = uint8(img);
end

output of the function floutage2