Using the @Doug idea , an example is given that handles all cases:
- hole position in even / odd mask
- median position in an even / odd number of elements
Example:
%%
N = 3;
assert(N>=3);
%%
I = im2double( imread('eight.tif') );
I = imnoise(I, 'salt & pepper',0.05);
%%
h = true(N,N);
if mod(N,2) == 0
%
h([N/2 N/2+1],[N/2 N/2+1]) = false(2);
else
%
h((N+1)/2,(N+1)/2) = false;
end
%%
num = sum(h(:));
if mod(num,2) == 0
%
I1 = ordfilt2(I, num/2, h, 'symmetric');
I2 = ordfilt2(I, num/2+1, h, 'symmetric');
J = imdivide( imadd(I1,I2), 2 );
else
%
J = ordfilt2(I, (num+1)/2, h, 'symmetric');
end
%%
subplot(121), imshow(J)
subplot(122), imshow( medfilt2(I,[N N],'symmetric') );
source
share