% ==================================================================================================
% Module:   pj1.m
%
% Usage:    Perfom the first experiement of cse 585.
%
% Purpose: 
%     This script is used to execute project 1 of cse 585
%
%     
% Input Variables:
%    none
%     
% Returned Results:
%    none
%     
% Restrictions/Notes:
%     
% See Also:
%       erosion(), dilation(), erode(), imavgfilter(), median(),
%       setdifference(), med()
%
% References:
%   http://www.cse.psu.edu/~cg585/projects/p1.pdf
% 
% Author:       Isaac Gerg & Justin Ford
% Date:         2/1/04
% Revisions:    none
% ==================================================================================================

% No declarations here.

disp('Running...');

% ==================================================================================================
% Part 1 - 5x5 Average filter
    disp('Part 1 Running...');
	nImage = imread('images\lenna.gif', 'gif');
	nAvgImage = imavgfilter(nImage);
	nAvgImage = nAvgImage ./ (max(max(nAvgImage))); 
	imwrite(nAvgImage, 'images\Lenna_5x5_averaged.jpg', 'jpg');
    
% ==================================================================================================
% Part 2 - Binary Image Processing
    % A. Spatial Filtering
    disp('Part 2 Running...');

     % Create the masks.
    I = imread('images/match1.gif', 'gif');
	B = [0 1 1 1 0;
         1 1 1 1 1;
         1 1 1 1 1;
         1 1 1 1 1;
         0 1 1 1 0];
 	B_2 = dilation(B,B);
 	B_3 = dilation(B_2,B);
 	
 	% I dilated by 3B
 	O_dilated_3B = dilation(I, B_3);
 	imwrite(O_dilated_3B, 'images\X_dilation_3B.png', 'png');
 	
 	% I eroded by 3B
 	O_eroded_3B = erosion(I, B_3);
 	imwrite(O_eroded_3B, 'images\X_erosion_3B.png', 'png');
 	
 	% I close 3B
 	O_close = erosion(O_dilated_3B, B_3);
 	
 	% I open 3B
 	O_open = dilation(O_eroded_3B, B_3);
 	
 	imwrite(O_open, 'images\X_open_3B.png', 'png');
 	imwrite(O_close, 'images\X_close_3B.png', 'png');

 	% B. Border Detection
	I = imread('images\penn256.gif', 'gif');
	IeB = erosion(I, B);
	IdB = dilation(I,B);
	
	X1 = setdifference(I, IeB);
	imwrite(X1, 'images\X1.png', 'png');
	
	% Adjust image size do to the dilation. 
    % Dilation adds a conservative border to the image that is
    % the radius of the structuring element.
    IdB = IdB(3:size(IdB,1)-2, 3:size(IdB,2)-2);
	X2 = setdifference(I, IdB);
	imwrite(X2, 'images\X2.png', 'png');
	
	X3 = erosion(setdifference(IdB, IeB), B);
	imwrite(X3, 'images\X3.png', 'png');

 	% C. Proposition 6.7.4 of P&V
 	I = imread('images\bear.gif', 'gif');
 	B = [1; 1; 1];
 	B_2 = dilation(B, B);
 	O_open = dilation(erosion(I, B_2), B_2);
 	O_close = erosion(dilation(O_open, B_2), B_2);
 	imwrite(O_close, 'images\2c.png', 'png');
 	A = ones(9,1);
 	O_med = median(O_close, A);
 	imwrite(O_med, 'images\2c_median.png', 'png');
 	% Now that the median filters and open and closes are done, compute the
 	% difference.
    O_setdiff = setdifference(O_med, O_close);
 	imwrite(O_setdiff, 'images\2c_difference.png', 'png');


% ==================================================================================================
% Part 3 - Shape-Based Filtering
% A. Remove the thin appendage...
	disp('Part 3 Running...');
	
	I = imread('images\TheThreeShapes.gif' ,'gif');
	B = [ 1 1 1;
          1 1 1;
          1 1 1];
	A = B;
 	% Perform the erosions and dialtions several times outputting the
 	% results of each interation.
    for x = 1:5
         O = erosion(I, A);
         imwrite(O, sprintf('images\\3erosion%d.png', x), 'png');
         A = dilation(A, B);
     end;
 	I = imread('images\3erosion4.png', 'png');
 	for x = 1:5
         I = dilation(I, B);
         imwrite(I, sprintf('images\\3dilation%d.png', x), 'png');
    end;
    
    % Image 4 seemed to have the best results. We now show 'how good' the
    % results are by differencing image 4 with the original.
 	J = imread('images\TheThreeShapes.gif' ,'gif');
 	I = imread('images\3dilation4.png', 'png');
 	I = I(5:size(I,1)-4, 5:size(I,2)-4);
 	O = setdifference(J,I);
 	imwrite(O, 'images\3difference.png', 'png');
	
	% B. ...Fill the interior cavity...
	S = ones(256, 256);
	J = imread('images\TheThreeShapes.gif' ,'gif');
	C = setdifference(S,J);
    imwrite(C, 'images\difference_image.png', 'png');
    % Remove the 'hole' by 0'ing it out.
    for x=30:66
        for y=49:96,
            C(x,y) = 0;
        end
    end
    imwrite(C, 'complement_with_no_cavities.png', 'png');
    C = Setdifference(C,S);
    imwrite(C, 'images\no_cavities.png', 'png');

% ==================================================================================================
disp('Done.');