在Matlab中创建“多孔”中值滤波器

时间:2009-11-18 15:14:20

标签: matlab image-processing filtering

我需要做的是在Matlab中为图像处理创建一种“特殊”的中值滤波器 - “多孔”中值滤波器。这是一个过滤器,不包括该区域中心的元素。

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

h = ones(3,3)/9;

对于“多孔”平均滤波器:

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

如何使用中值滤波器做同样的事情?有没有办法修改medfilt2或者我需要自己实现运行中位数?

3 个答案:

答案 0 :(得分:4)

如何使用基础函数ordfilt2并在那里定义自己的域?

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

答案 1 :(得分:3)

使用@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') );

答案 2 :(得分:0)

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

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