function imm = immedianm(im,K,L)
// imm = immedianm(im,K,L)
// im : input image
// imm : resulting image
// K, L : vertical/horizontal rectangular window dimensions

K = floor(K/2);
L = floor(L/2);
[N,M] = size(im);

lignn = [];
for n = 1:min(L,N)
//   lignn = [lignn;toeplitz([im(n,K+1:-1:1) zeros(1,K)],[im(n,K+1:M) zeros(1,K)])];
   lignn = [lignn;toeplitz([im(n,K+1:-1:1) im(n,1)*ones(1,K)],[im(n,K+1:M) im(n,M)*ones(1,K)])];
end

imm = zeros(im);
for n = 1:N
   if n+L <= M then
   	lignn = [lignn;toeplitz([im(n+L,K+1:-1:1) im(n+L,1)*ones(1,K)],[im(n+L,K+1:M) im(n+L,M)*ones(1,K)])];
   end
   //disp(lignn)
   imm(n,:) = median(lignn,'r');
   if n > L then
       lignn(1:2*K+1,:) = [];
   end
end

endfunction
