【问题标题】:Creating "holey" median filter in Matlab在 Matlab 中创建“holey”中值滤波器
【发布时间】:2010-12-17 22:05:27
【问题描述】:

我需要做的是在 Matlab 中为图像处理创建一种“特殊”的中值滤波器——“holey”中值滤波器。这是一个过滤器,它排除了区域中心的元素。

对于标准中值滤波器,我使用 medfilt2 函数,但我不能将掩码(内核)作为矩阵传递(它不是线性变换)。
例如,使用标准平均滤波器 3x3,我将掩码(内核)创建为:

h = ones(3,3)/9;

对于“holey”平均滤波器:

h = ones(3,3)/8;
h(2,2) = 0;

如何用中值滤波器做同样的事情?有什么方法可以修改medfilt2 还是我需要自己实现一个运行中位数?

【问题讨论】:

    标签: matlab image-processing filtering


    【解决方案1】:

    使用底层函数ordfilt2 并在那里定义自己的域怎么样?

    https://www.mathworks.com/help/images/ref/ordfilt2.html

    【讨论】:

    • 好的,这与我需要的很接近,但问题是如果我在有孔过滤域中有偶数个元素,则中位数是中间两个元素的算术平均值。所以唯一的解决方案是使用两个 ordfilt2 过滤器并自己计算平均值?
    • @MatlabDoug,MATLAB 的实现是否使用“移动直方图”实现?
    【解决方案2】:

    使用@Doug的思路,下面是一个处理所有情况的例子:

    • 偶数/奇数掩膜尺寸中的孔位置
    • 在偶数/奇数个元素中的中间位置

    例子:

    %%# mask size: N-by-N
    N = 3;
    assert(N>=3);
    
    %%# read image and add noise
    I = im2double( imread('eight.tif') );
    I = imnoise(I, 'salt & pepper',0.05);
    
    %%# build mask with hole in center
    h = true(N,N);
    if mod(N,2) == 0
        %# hole is a 2-by-2 square
        h([N/2 N/2+1],[N/2 N/2+1]) = false(2);
    else
        %# hole is one point
        h((N+1)/2,(N+1)/2) = false;
    end
    
    %%# compute median filter with hole
    num = sum(h(:));
    if mod(num,2) == 0
        %# even case: average from using the two elements in the middle
        I1 = ordfilt2(I, num/2, h, 'symmetric');
        I2 = ordfilt2(I, num/2+1, h, 'symmetric');
        J = imdivide( imadd(I1,I2), 2 );
    else
        %# odd case: note this is never reached
        J = ordfilt2(I, (num+1)/2, h, 'symmetric');
    end
    
    %%# plot and compare against normal median filter
    subplot(121), imshow(J)
    subplot(122), imshow( medfilt2(I,[N N],'symmetric') );
    

    【讨论】:

    • 很好的例子 (+1)。但我建议在 ordfilt2 中使用“对称”参数
    【解决方案3】:

    我的猜测是它不存在,您需要自己实现一个。你可以写一个作为 matlab 函数,或者如果速度有问题,用 C 写一个。

    解决问题的另一种方法是使用rank filter。它与您所要求的不同,但可能是实现您想要的另一种方式。

    【讨论】:

    • 是的,速度是问题所在。我尝试自己实现中值滤波器,但在 Matlab 中过滤一张 512x512 像素的图像大约需要 5 分钟...
    猜你喜欢
    • 2011-10-02
    • 2018-06-23
    • 2015-02-16
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    相关资源
    最近更新 更多