Chain Code
Chain Code
clear
clc
directions = [ 1, 0
1,-1
0,-1
-1,-1
-1, 0
-1, 1
0, 1
1, 1]
I = imread('./IMAGES/1/M_201005_1_0001.pgm');
Ibw = im2bw(I,0.15); % This is also by setting all the pixels with intesity lower than
17 to 0;
tanAngFunc ={};
[row,col] = find(Ibw);
y = col(find(max(row)==row))
x = max(row)
imshow(I)
hold on;
plot(y,x,'r.','MarkerSize',1)
hold on
l=1;
not_done = true;
while not_done
if l== 36
'test'
end
% Right (0)
if Ibw(x+directions(1,2),y+directions(1,1)) ~=0
tanAngFunc{l} = 0;
x= x+directions(1,2);
y= y+directions(1,1);
% Above right(1)
elseif Ibw(x+directions(2,2),y+directions(2,1)) ~=0
tanAngFunc{l} = 2;
x= x+directions(2,2);
y= y+directions(2,1);
% Above (2)
elseif Ibw(x+directions(3,2),y+directions(3,1)) ~=0
tanAngFunc{l} = 2;
x= x+directions(3,2);
y= y+directions(3,1);
% Left (4)
elseif Ibw(x+directions(5,2),y+directions(5,1)) ~=0
tanAngFunc{l} = 2;
x= x+directions(5,2);
y= y+directions(5,1);
% Bottom (6)
elseif Ibw(x+directions(7,2),y+directions(7,1)) ~=0
tanAngFunc{l} = 2;
x= x+directions(7,2);
y= y+directions(7,1);
x= x+directions(8,2);
y= y+directions(8,1);
end
plot(y,x,'y.','MarkerSize',3)
hold on
pause(1)
l = l + 1;