【问题标题】:Partially bluring image with averaging filter使用平均滤波器的部分模糊图像
【发布时间】:2021-05-10 04:58:59
【问题描述】:

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

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

【问题讨论】:

    标签: image function matlab image-processing imagefilter


    【解决方案1】:

    图像的模糊部分/分区

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

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

    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
    

    【讨论】:

      【解决方案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
      

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-02
        • 2016-05-13
        • 1970-01-01
        • 2023-03-24
        • 2013-10-25
        • 2014-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多