Creating a Holey Median Filter in Matlab

What I need to do is create a “special” kind of median filter for image processing in Matlab - a “leaky” median filter. This is a filter that excludes an element in the center of the area.

For a standard median filter, I use a function medfilt2, but I can not pass the mask (core) for it as a matrix (this is not a linear transformation).
For example, using the standard 3x3 averaging filter, I create a mask (core) as:

h = ones(3,3)/9;

And for the “holey” averaging filter:

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

How to do the same with the middle filter? Is there a way to change, medfilt2or do I need to implement the current median myself?

+3
source share
3 answers

How about using the base function ordfilt2and defining your own domain there?

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

+4
source

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:

%%# 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') );
+3
source

, , . matlab, , C.

- . , , , .

0

Source: https://habr.com/ru/post/1723232/


All Articles