1 % EXAMPLE GVF snake examples on two simulated object boundaries.
5 % traditional snake and distance snake differed from GVF snake only
6 % by using different external force field. In order to produce the
7 % corresponding external force field, use the following (all
8 % assuming edge map f is in memory).
11 % f0 = gaussianBlur(f,1);
12 % [px,py] = gradient(f0);
15 % D = dt(f>0.5); % distance transform (dt) require binary edge map
16 % [px,py] = gradient(-D);
18 % [px,py] is the external force field in both cases
20 % balloon model using a different matlab function "snakedeform2"
21 % instead of "snakedeform". The external force could be any force
22 % field generated above.
24 % an example of using it is as follows
25 % [x,y] = snakedeform2(x,y, alpha, beta, gamma, kappa, kappap, px,py,2);
26 % do a "help snakedeform2" for more details
29 % Chenyang Xu and Jerry Prince 6/17/97
30 % Copyright (c) 1996-97 by Chenyang Xu and Jerry Prince
32 cd ..; s = cd; s = [s, '/snake']; path(s, path); cd examples;
36 % ==== Example 1: U-shape object ====
38 % Read in the 64x64 U-shape image
39 %[I,map] = rawread('../images/cochon64.pgm');
40 I = imread('../images/cochon64.pgm');
41 % Compute its edge map
42 disp(' Compute edge map ...');
43 f = gaussianBlur(1 - I/255,1);
46 % Compute the GVF of the edge map f
47 disp(' Compute GVF ...');
48 [u,v] = GVF(f, 0.2, 80);
49 disp(' Nomalizing the GVF external force ...');
50 mag = sqrt(u.*u+v.*v);
51 px = u./(mag+1e-10); py = v./(mag+1e-10);
55 subplot(221); imdisp(I); title('test image');
56 subplot(222); imdisp(f); title('edge map');
58 % display the gradient of the edge map
60 subplot(223); quiver(fx,fy);
61 axis off; axis equal; axis 'ij'; % fix the axis
62 title('edge map gradient');
65 subplot(224); quiver(px,py);
66 axis off; axis equal; axis 'ij'; % fix the axis
67 title('normalized GVF field');
70 disp(' Press any key to start GVF snake deformation');
72 figure(1); subplot(221); cla;
73 colormap(gray(64)); image(((1-f)+1)*40); axis('equal', 'off');
77 [x,y] = snakeinterp(x,y,2,0.5);
82 [x,y] = snakedeform(x,y,2,0,1,0.1,px,py,5);
83 [x,y] = snakeinterp(x,y,2,0.5);
85 title(['Deformation in progress, iter = ' num2str(i*5)])
89 disp(' Press any key to display the final result');
92 colormap(gray(64)); image(((1-f)+1)*40); axis('equal', 'off');
94 title(['Final result, iter = ' num2str(i*5)]);
96 disp(' Press any key to run the next example');
100 % ==== Example 2: Room like object ====
102 % Read in the 64x64 room image
103 [I,map] = rawread('../images/room.pgm');
105 % Compute its edge map
106 disp(' Compute edge map ...');
109 % Compute the GVF of the edge map f
110 disp(' Compute GVF ...');
111 [px,py] = GVF(f, 0.2, 80);
113 % display the results
115 subplot(221); imdisp(I); title('test image');
116 subplot(222); imdisp(f); title('edge map');
118 % display the gradient of the edge map
119 [fx,fy] = gradient(f);
120 subplot(223); quiver(fx,fy);
121 axis off; axis equal; axis 'ij'; % fix the axis
122 title('edge map gradient');
125 subplot(224); quiver(px,py);
126 axis off; axis equal; axis 'ij'; % fix the axis
130 disp(' Press any key to start GVF snake deformation');
132 figure(1); subplot(221); cla;
133 colormap(gray(64)); image(((1-f)+1)*40); axis('equal', 'off');
137 [x,y] = snakeinterp(x,y,2,0.5);
142 [x,y] = snakedeform(x,y,0.05,0,1,2,px,py,5);
143 [x,y] = snakeinterp(x,y,2,0.5);
145 title(['Deformation in progress, iter = ' num2str(i*5)])
149 disp(' Press any key to display the final result');
151 figure(1); subplot(221); cla;
152 colormap(gray(64)); image(((1-f)+1)*40); axis('equal', 'off');
154 title(['Final result, iter = ' num2str(i*5)]);