% dip05_4           gr11mar15   oct20
% root signal of a median filter (1-D & 2-D)
%
clear all;

f = [1 1 1 1 1 2 3 4 5 6 7 8 8 8 8 1 8 8 8 1 1 8 8 8 8 3 3 3 3 3];
fn = f + randn(size(f))*.3;
g3 = medfilt1(f,3);
g5 = medfilt1(f,5); 
gn3 = medfilt1(fn,3);
gn5 = medfilt1(fn,5); 
subplot(221); stem(f); axis([0 30 0 9]); hold on;
subplot(222); stem(fn); axis([0 30 0 9]); hold on;
subplot(223); stem(f); axis([0 30 0 9]); hold on;
subplot(224); stem(fn); axis([0 30 0 9]); hold on;
pause()
subplot(221); stem(g3); hold off;
subplot(222); stem(gn3); hold off;
subplot(223); stem(g5); hold off;
subplot(224); stem(gn5); hold off;
return

%%
f = double(imread('.\img\lena.tif'));

fr = f(300,:);              % -------------------------  1-D
clf; subplot(121);  plot(fr);
for i = 1:15                 % 2-3 iterations yield the root signal
    gr = medfilt1(fr,5);
    subplot(122);  plot(gr);  pause;
    fr = gr;
end;

%% 
f = double(imread('.\img\lena.tif'));
% f = double(imread('eight.tif'));
% f = double(imread('pears.png')); f = rgb2gray(f/255)*255;
f1 = f;
clf
% figure(1);  imshow(f,[]);        % ----------------- 2-D
for i = 1:100
    g = medfilt2(f1,[5 5]);      % try with 5*5 too
    imshow([f, g, (g-f1)*50+128],[0 255]);  pause(.2);
    f1 = g;
end;
% after some 10 iterations the output is apparently stable. Actually,
% some images show tiny permanent oscillations (Lena)


return;


