%
% hw02.m
%
% 04/20/02 jph
%
% 02/05/11: for semesters up until SP 2011 this was hw03.  Starting in SP 2011,
%           it is now hw02.  jph
% 01/20/14: took out some loops to make it run faster. jph
%

size = 256;                       % use this line for running on a big image
%size = 8;                        % use this line for DEBUG on small test image
BlobLabels = zeros(size,size);

%
% Suzi1
%

%
% Read Suzi1.bin, transpose, display, and print
%

% The following lines are for running on the big image
fidSuzi1=fopen('Suzi1.bin','r');    
[Suzi1,junk] = fread(fidSuzi1,[size,size],'uchar');
Suzi1 = Suzi1';
% the following lines are for DEBUG -- instead of reading the big image,
%    make a small 8x8 test image for debugging.
%Suzi1 = zeros(size,size);
%Suzi1(1,1) = 128;
%Suzi1(1,2) = 128;
%Suzi1(2,1) = 128;
%Suzi1(2,2) = 128;
%
%Suzi1(2,5) = 128;
%Suzi1(3,5) = 128;
%
%Suzi1(3,7) = 128;
%Suzi1(3,8) = 128;
%
%Suzi1(5,1) = 128;
%Suzi1(6,1) = 128;
%Suzi1(7,1) = 128;
%Suzi1(7,2) = 128;
%Suzi1(7,3) = 128;
%Suzi1(7,4) = 128;
%Suzi1(7,5) = 128;
%Suzi1(6,5) = 128;
%Suzi1(5,5) = 128;
%
%Suzi1(5,3) = 128;
%
%Suzi1(5,8) = 128;
%
%Suzi1(6,7) = 128;
%
%Suzi1(8,7) = 128;
% END of the DEBUG section (which makes the small 8x8 test image)

figure(1);colormap(gray(256));
image(Suzi1);axis('image');
title('Original Suzi1.bin Image','FontSize',12);
print -deps MSuzi1.eps;
pause;

%
% Threshold at the empirically determined value thresh=95.
%   Thresholded image = 255 if original < thresh.
%
x = zeros(size,size);

thresh = 95;

x(Suzi1 < thresh) = 255;             % this line for running on big image
%x(Suzi1 > thresh) = 255;            % this line for DEBUG on 8x8 test image

%
% Display and print thresholding result
%
figure(2);colormap(gray(256));
image(x);axis('image');
title('Threshold at T=95','FontSize',12);
print -deps MSuzi1Thresh.eps;
pause;

%
% Perform Blob Coloring on the thresholding result; display and print
%
[BlobLabels,MaxLabel] = BlobColor(x,size,BlobLabels);
y = i2bdbx(BlobLabels,size);
figure(3);colormap(gray(256));
image(y);axis('image');
title('Result after Blob Coloring','FontSize',12);
print -deps MSuzi1Blob.eps;
pause;

%
% Perform Blob Counting, remove minor regions, display, and print
%
BlobCounts = BlobCount(size,BlobLabels,MaxLabel);
x = RemoveMinorRegions(x,size,BlobLabels,BlobCounts);
figure(4);colormap(gray(256));
image(x);axis('image');
title('Result after minor region removal','FontSize',12);
print -deps MSuzi1BlobMinor.eps;
pause;

%
% Invert image and repeat blob coloring and minor region removal
%
x = 255 - x;
[BlobLabels,MaxLabel] = BlobColor(x,size,BlobLabels);
BlobCounts = BlobCount(size,BlobLabels,MaxLabel);
x = RemoveMinorRegions(x,size,BlobLabels,BlobCounts);

%
% Invert to get the final result
%
x = 255 - x;
figure(5);colormap(gray(256));
image(x);axis('image');
title('Final Result','FontSize',12);
print -deps MSuzi1Final.eps;
pause;

%
% Make a special fused image that combines the original with the final blob
%   coloring result... this is just to make a fancy display.
%
y = 255 - ((x .* (255 - Suzi1)) / 255);
figure(6);colormap(gray(256));
image(y);axis('image');
title('Fused Final Result','FontSize',12);
print -deps MSuzi1Fuse.eps;
pause;





%
% ct_scan
%

size = 256;                       
BlobLabels = zeros(size,size);

%
% Read ct_scan.bin, transpose, display, and print
%

fidct_scan=fopen('ct_scan.bin','r');    
[ct_scan,junk] = fread(fidct_scan,[size,size],'uchar');
ct_scan = ct_scan';
figure(11);colormap(gray(256));
image(ct_scan);axis('image');
title('Original ct\_scan.bin Image','FontSize',12);
print -deps Mct_scan.eps;
pause;

%
% Threshold at the empirically determined value thresh=1.
%   Thresholded image = 255 if original >= thresh.
%
x = zeros(size,size);
thresh = 1;
x(ct_scan >= thresh) = 255;

%
% Display and print thresholding result
%
figure(12);colormap(gray(256));
image(x);axis('image');
title('Threshold at T=1','FontSize',12);
print -deps Mct_scanThresh.eps;
pause;

%
% Perform Blob Coloring on the thresholding result; display and print
%
[BlobLabels,MaxLabel] = BlobColor(x,size,BlobLabels);
y = i2bdbx(BlobLabels,size);
figure(13);colormap(gray(256));
image(y);axis('image');
title('Result after Blob Coloring','FontSize',12);
print -deps Mct_scanBlob.eps;
pause;

%
% Perform Blob Counting, remove minor regions, display, and print
%
BlobCounts = BlobCount(size,BlobLabels,MaxLabel);
x = RemoveMinorRegions(x,size,BlobLabels,BlobCounts);
figure(14);colormap(gray(256));
image(x);axis('image');
title('Result after minor region removal','FontSize',12);
print -deps Mct_scanBlobMinor.eps;
pause;

%
% Invert image and repeat blob coloring and minor region removal
%
x = 255 - x;
[BlobLabels,MaxLabel] = BlobColor(x,size,BlobLabels);
BlobCounts = BlobCount(size,BlobLabels,MaxLabel);
x = RemoveMinorRegions(x,size,BlobLabels,BlobCounts);

%
% Invert to get the final result
%
x = 255 - x;
figure(15);colormap(gray(256));
image(x);axis('image');
title('Final Result','FontSize',12);
print -deps Mct_scanFinal.eps;
pause;

%
% Make a special fused image that combines the original with the final blob
%   coloring result... this is just to make a fancy display.
%
y = 255 - ((x .* (255 - ct_scan)) / 255);
figure(16);colormap(gray(256));
image(y);axis('image');
title('Fused Final Result','FontSize',12);
print -deps Mct_scanFuse.eps;
pause;

