%
% hw06.m
%    
%   Apply 3x3 MF, OPEN, and CLOSE to a gray scale byte image.
%   Handle edge effects by making the output zero.
% 
%   The input image is assumed square.
%
%   NOTE: in the code below, W will hold the window set of pixel values.
%         - median(W(:)) will correctly find the median of the window set.
%         - median(W) is NOT correct.  This returns a vector of the column
%                medians of W.
%         - median(median(W)) is also NOT correct.  It implements something
%           called a separable median filter, which is NOT the same thing
%           as the median filter.
% 
%
% 04/25/06 jph
%


%--------------------------------------------------------------------
% Initialize
% 
clear;
size = 256;                         % number of rows/cols in the image
wsize = 3;                          % for 3x3 filter windows
wsizeo2 = fix(wsize/2);             % window half-size ( =1 for 3x3)
FigNum = 1;                         % initialize the figure number

W = zeros(wsize);                   % filter window

yMF = zeros(size);                  % median filtered image
yE = zeros(size);                   % eroded image
yD = zeros(size);                   % dilated image
yO = zeros(size);                   % opened image
yC = zeros(size);                   % closed image


%
% camera99.bin
%
fprintf(1,'Doing camera99.bin...\n');

%
% Read and display the image
%
x = ReadBin('camera99.bin',size);
FigNum = ShowByteImage(x,FigNum);
title('camera99.bin','FontSize',12);
print -deps camera99.eps;

%
% Apply median, erode, and dilate
%
for row=wsizeo2+1:size-wsizeo2
  for col=wsizeo2+1:size-wsizeo2
    W = x(row-wsizeo2:row+wsizeo2,col-wsizeo2:col+wsizeo2);
    yMF(row,col) = median(W(:));
    yE(row,col) = min(W(:));
    yD(row,col) = max(W(:));
  end
end

%
% Apply dilate to yE and erode to yD to finish the open and close
%
for row=wsizeo2+2:size-wsizeo2-1
  for col=wsizeo2+2:size-wsizeo2-1
    W = yE(row-wsizeo2:row+wsizeo2,col-wsizeo2:col+wsizeo2);
    yO(row,col) = max(W(:));
    W = yD(row-wsizeo2:row+wsizeo2,col-wsizeo2:col+wsizeo2);
    yC(row,col) = min(W(:));
  end
end

%
% Display the output images
%
FigNum = ShowByteImage(yMF,FigNum);
title('3x3 Median Fiter (99)','FontSize',12);
print -deps camera99MF.eps;

FigNum = ShowByteImage(yO,FigNum);
title('3x3 Morphological Opening (99)','FontSize',12);
print -deps camera99O.eps;

FigNum = ShowByteImage(yC,FigNum);
title('3x3 Morphological Closing (99)','FontSize',12);
print -deps camera99C.eps;



%--------------------------------------------------------------------
% Do it all over again for camera9.bin
%
fprintf(1,'\nHit any key to continue...\n');
pause;
size = 256;                         % number of rows/cols in the image
wsize = 3;                          % for 3x3 filter windows
wsizeo2 = fix(wsize/2);             % window half-size ( =1 for 3x3)

W = zeros(wsize);                   % filter window

yMF = zeros(size);                  % median filtered image
yE = zeros(size);                   % eroded image
yD = zeros(size);                   % dilated image
yO = zeros(size);                   % opened image
yC = zeros(size);                   % closed image

fprintf(1,'Doing camera9.bin...\n');

%
% Read and display the image
%
x = ReadBin('camera9.bin',size);
FigNum = ShowByteImage(x,FigNum);
title('camera9.bin','FontSize',12);
print -deps camera9.eps;

%
% Apply median, erode, and dilate
%
for row=wsizeo2+1:size-wsizeo2
  for col=wsizeo2+1:size-wsizeo2
    W = x(row-wsizeo2:row+wsizeo2,col-wsizeo2:col+wsizeo2);
    yMF(row,col) = median(W(:));
    yE(row,col) = min(W(:));
    yD(row,col) = max(W(:));
  end
end

%
% Apply dilate to yE and erode to yD to finish the open and close
%
for row=wsizeo2+2:size-wsizeo2-1
  for col=wsizeo2+2:size-wsizeo2-1
    W = yE(row-wsizeo2:row+wsizeo2,col-wsizeo2:col+wsizeo2);
    yO(row,col) = max(W(:));
    W = yD(row-wsizeo2:row+wsizeo2,col-wsizeo2:col+wsizeo2);
    yC(row,col) = min(W(:));
  end
end

%
% Display the output images
%
FigNum = ShowByteImage(yMF,FigNum);
title('3x3 Median Fiter (9)','FontSize',12);
print -deps camera9MF.eps;

FigNum = ShowByteImage(yO,FigNum);
title('3x3 Morphological Opening (9)','FontSize',12);
print -deps camera9O.eps;

FigNum = ShowByteImage(yC,FigNum);
title('3x3 Morphological Closing (9)','FontSize',12);
print -deps camera9C.eps;

fprintf(1,'\n');
return;
