I’m implementing chain code and I stumbled across a little problem. First of all what I do:
I start at a certain pixel on my boundary and then I check which neighboring pixel is present acoriding to this:
3 2 1
\ | /
4-- --0
/ | \
5 6 7
Though here i have a problem:

the little red dot is where the algorithm starts. then the first direction is 2 then 1 then 0 then 0 etc…
If you follow the yellow dots you’ll see it ends at two dots next to each other. This is where the algorithm went in direction 4 (since there was no pixel with value 1 at direction 0,1,2 or 3). Though at the next step it checks the direction 0 (which is always the first direction to start) … and of course it exists, since it is the previous spot and will go right. Here it is stuck in an infinite loop (going left to right and right to left).
My question is now, how can i fix this ?
The code that I use is:
% Implementation of tangent angle function (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);
% Above left (3)
elseif Ibw(x+directions(4,2),y+directions(4,1)) ~=0
tanAngFunc{l} = 2;
x= x+directions(4,2);
y= y+directions(4,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 left (5)
elseif Ibw(x+directions(6,2),y+directions(6,1)) ~=0
tanAngFunc{l} = 2;
x= x+directions(6,2);
y= y+directions(6,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);
% Bottom right (7)
elseif Ibw(x+directions(8,2),y+directions(8,1)) ~=0
tanAngFunc{l} = 3;
x= x+directions(8,2);
y= y+directions(8,1);
end
plot(y,x,'y.','MarkerSize',3)
hold on
pause(1)
l = l + 1;
not_done = (x ~= col(find(max(row)==row)) && y ~= max(row));
end
On this image :

In case you want to try this yourself
Edit:
As suggested in the comments, In my 2nd version I made sure the algorithm didn’t use previously visited pixels. The result:

As you can see this isn’t a working answer.
Edit 2
Concerning the updated solution of @jonas. I’ve quickly put it in excel so it’s easier to understand. (pixel with 1 is not yet found, pixel x is already found).

So this doesn’t work 100%, It’s rather impossible to make sure the edge of every picture is 2 pixels wide when working with a huge dataset.
The problem with your algorithm is that it only checks whether the “candidate” pixel to be visited is part of the object — but performs no additional checks to make sure that the pixel is part of the boundary. This is why it will happily walk through the inside of the object.
A popular algorithm for boundary tracing is the Moore-neighbourhood tracing algorithm. In addition to the Wikipedia page, take a look at this page, which explains the idea behind the algorithm in more detail.