% BoundarySearch.m
% Tom McGrath
% CIS 601, Fall 2004
%

% This program compares boundry strings from segmented images
%

% acquire image
sourceimage = imread('sourcestapler2.jpg');
% reduce to correct dimensions
sourceimage = sourceimage(:,1:640);
% show source image and pause;
imshow( sourceimage );
%pause;

% threshold source image
T = graythresh(sourceimage);
sourceimage = im2bw( sourceimage, T );

% more preprocessing
sourceimage = im2bw(sourceimage,0.5);
sourceimage = imregionalmin(sourceimage);
sourceimage = imfill(sourceimage,'holes');
[g, t] = edge(sourceimage,'canny');

% show segmented image and pause;
imshow( g );
pause;

% get boundaries
B = Boundaries( g );
d = cellfun('length',B);
[max_d,k] = max(d);
v = B{k(1)};

b = B{1};
%b = bound2eight(b);

[M,N] = size(g);

n = bound2im(b,M+1,N+1,min(b(:,1)),min(b(:,2)));

% get pixels subsample and compute connected image
[s,su] = bsubsamp(b, 8);
cn = connectpoly(s(:,1),s(:,2));

% show boundary computed image and pause;
imshow( n);
pause;

% show boundary points image taken from subsample and pause;
newn = bound2im(s,M+1,N+1,min(s(:,1)),min(s(:,2)));
imshow( newn );
pause;

% show the connected boundary points image and pause;
conn = bound2im(cn,M+1,N+1,min(cn(:,1)),min(cn(:,2)));
imshow( conn );
pause;

% obtain the chain code of boundary
c = fchcode(su);

% Now we have a sourceboundary for the "template" image
% and we want to get the same from some target image.
%
% The 2 boundaries will then be compared to get some measure of
% how similar the objects are.



% acquire target image
targetimage = imread('targetstapler.jpg');
% reduce to correct dimensions
targetimage = targetimage(:,40:640);
% show target image and pause;
imshow( targetimage );
pause;

% threshold target image
T2 = graythresh(targetimage);
targetimage = im2bw( targetimage, T2 );

% segment image
% more preprocessing
targetimage = im2bw(targetimage,0.5);
targetimage = imregionalmin(targetimage);
targetimage = imfill(targetimage,'holes');
[g2, t2] = edge(targetimage,'canny');

% show segmented image and pause;
imshow( g2 );
pause;

% get boundaries
B2 = Boundaries( g2 );
d2 = cellfun('length',B2);
[max_d2,k2] = max(d2);
v2 = B2{k2(1)};

b2 = B2{1};
%b = bound2eight(b);

[M2,N2] = size(g2);

n2 = bound2im(b2,M2+1,N2+1,min(b2(:,1)),min(b2(:,2)));

% get pixels subsample and compute connected image
[s2,su2] = bsubsamp(b2, 8);
cn2 = connectpoly(s2(:,1),s2(:,2));

% show boundary computed image and pause;
imshow( n2);
pause;

% show boundary points image taken from subsample and pause;
newn2 = bound2im(s2,M+1,N+1,min(s2(:,1)),min(s2(:,2)));
imshow( newn2 );
pause;

% show the connected boundary points image and pause;
conn2 = bound2im(cn2,M+1,N+1,min(cn2(:,1)),min(cn2(:,2)));
imshow( conn2 );
pause;

% obtain the chain code of boundary


c2 = fchcode(su2);


% compare boundaries

ssim = strsimilarity( c, c2 );