1 function [Svd_dist_bdry, Svd_dist_reg] = Svd_dist (Region1, Region2)
\r
2 % first represent the two boundaries as matrices where ones stand for
\r
3 % boundary pixels and zeros elsewhere, next use SVD decomposition to
\r
4 % find eigenvalues, and finally adjust those (take shorter array and
\r
5 % multiply it by the lengths ratio) - observed from resizing the same
\r
11 if length(Region1.Boundary) < length(Region2.Boundary) % rotate 1 is quicker
\r
18 % finding the first region boundary and filled
\r
19 Max_x1 = Region1.Shifts(1, 2); Min_x1 = Region1.Shifts(1, 1);
\r
20 Max_y1 = Region1.Shifts(2, 2); Min_y1 = Region1.Shifts(2, 1);
\r
21 Mb1 = zeros(Max_x1-Min_x1+1, Max_y1-Min_y1+1);
\r
22 Mb1(sub2ind(size(Mb1), Region1.Boundary(1, :)-Min_x1+1, ...
\r
23 Region1.Boundary(2, :)-Min_y1+1)) = 1;
\r
25 Bdry_diag1 = svd(Mb1);
\r
26 To_start_fill = [Region1.Inner_pts(2, :)-Min_y1+1; ...
\r
27 Region1.Inner_pts(1, :)-Min_x1+1];
\r
28 Mb1 = bwfill(Mb1, To_start_fill(1, :), To_start_fill(2, :));
\r
34 Reg_diag1 = svd(double(Mb1));
\r
35 Ld1 = length(Reg_diag1);
\r
36 [Rows1, Cols1] = size(Mb1);
\r
38 % finding the second region filled
\r
39 Max_x2 = Region2.Shifts(1, 2); Min_x2 = Region2.Shifts(1, 1);
\r
40 Max_y2 = Region2.Shifts(2, 2); Min_y2 = Region2.Shifts(2, 1);
\r
41 Mb2_orig = zeros(Max_x2-Min_x2+1, Max_y2-Min_y2+1);
\r
42 Mb2_orig(sub2ind(size(Mb2_orig), Region2.Boundary(1, :)-Min_x2+1, ...
\r
43 Region2.Boundary(2, :)-Min_y2+1)) = 1;
\r
44 To_start_fill = [Region2.Inner_pts(2, :)-Min_y2+1; ...
\r
45 Region2.Inner_pts(1, :)-Min_x2+1];
\r
46 Mb2_orig = bwfill(Mb2_orig, To_start_fill(1, :), To_start_fill(2, :));
\r
50 [Rows, Cols] = find(Mb2_orig);
\r
51 Mb2_orig = [Rows'; Cols'];
\r
54 % obtaining rotated second filled and extracting boundary next
\r
55 Theta = (i-1)*(pi/2)/(Subints+1);
\r
56 Matrix = [cos(Theta), sin(Theta); -sin(Theta), cos(Theta)];
\r
57 Mb2_ind = Matrix * Mb2_orig;
\r
58 Mb2_ind = [round(Mb2_ind), floor(Mb2_ind), ceil(Mb2_ind)];
\r
59 Max_x2 = max(Mb2_ind(1, :)); Min_x2 = min(Mb2_ind(1, :));
\r
60 Max_y2 = max(Mb2_ind(2, :)); Min_y2 = min(Mb2_ind(2, :));
\r
61 Mb2 = zeros(Max_x2-Min_x2+1, Max_y2-Min_y2+1);
\r
62 Mb2(sub2ind(size(Mb2), Mb2_ind(1, :)-Min_x2+1, Mb2_ind(2, :)-Min_y2+1)) = 1;
\r
63 Reg_diag2 = svd(Mb2);
\r
64 Ld2 = length(Reg_diag2);
\r
65 Mb2 = double(bwperim(Mb2));
\r
66 Bdry_diag2 = svd(Mb2);
\r
67 [Rows2, Cols2] = size(Mb2);
\r
70 Bdry_diag_1 = Bdry_diag1(1:Ld2);
\r
71 Reg_diag_1 = Reg_diag1(1:Ld2);
\r
73 Bdry_diag_1 = Bdry_diag1;
\r
74 Reg_diag_1 = Reg_diag1;
\r
75 Bdry_diag2 = Bdry_diag2(1:Ld1);
\r
76 Reg_diag2 = Reg_diag2(1:Ld1);
\r
79 Factor = sqrt((Rows1*Cols1)/(Rows2*Cols2));
\r
81 Bdry_diag2 = Bdry_diag2*Factor;
\r
82 Reg_diag2 = Reg_diag2*Factor;
\r
84 Bdry_diag1 = Bdry_diag1/Factor;
\r
85 Reg_diag1 = Reg_diag1/Factor;
\r
88 Svd_dist_bdry(i) = norm(Bdry_diag_1-Bdry_diag2);
\r
89 Svd_dist_reg(i) = norm(Reg_diag_1-Reg_diag2);
\r
92 Svd_dist_bdry = min(Svd_dist_bdry);
\r
93 Svd_dist_reg = min(Svd_dist_reg);
\r