MATLAB: I am trying to implement chain code starting from a pixel where intensity value is one I am successfully getting its chain code as 7 and the new co-ordinate should be this point but instead of this updation is not going on this way instead I am get

chain code

I am trying to implement chain code starting from a pixel where intensity value is one I am successfully getting its chain code as 7 and my new co-ordinate should be this point but instead of this updation is not going on this way instead I am getting all the chain codes of coordinates having intensity value 1. I am new to matlab.Image on which I am working is also attached.Any kind of help will be highly appreciated.Thanks in advance. Code is as follows:
clc;
clear;
close all;
I=imread('tool.png');
% imshow(I)
I2 = imcrop(I,[180 174 250 450]);
figure
imshow(I2)
im2double(I2);
t=graythresh(I2);
BW=im2bw(I2,t);
figure
imshow(BW)
J=bwperim(BW);
B=im2double(J);
imshow(B)
[M, N]=size(B);
p=7;
q=6;
h=5;
s=4;
t=3;
u=2;
v=1;
w=0;
for j=2:N-1
for i=2:M-1
if B(i,j)==1
fprintf('Element(%d,%d) = %d.\n',i,j,B(i,j))
x=i;
y=j;
while (x>=2 && y<=246)
if B(x+1,y+1)==1
fprintf('The chain code for (%d,%d)= %d.\n',x,y,p)
newx=x+1;
newy=y+1;
break;
elseif B(x+1,y)==1
fprintf('The chain code for (%d,%d)= %d.\n',x,y,q)
newx=x+1;
newy=y;
break;
elseif B(x+1,y-1)==1
fprintf('The chain code for (%d,%d)= %d.\n',x,y,h)
newx=x+1;
newy=y-1;
break;
elseif B(x,y-1)==1
fprintf('The chain code for (%d,%d)= %d.\n',x,y,s)
newx=x;
newy=y-1;
break;
elseif B(x-1,y-1)==1
fprintf('The chain code for (%d,%d)= %d.\n',x,y,t)
newx=x-1;
newy=y-1;
break;
elseif B(x-1,y)==1
fprintf('The chain code for (%d,%d)= %d.\n',x,y,u)
newx=x-1;
newy=y;
break;
elseif B(x-1,y+1)==1
fprintf('The chain code for (%d,%d)= %d.\n',x,y,v)
newx=x-1;
newy=y+1;
break;
elseif B(x,y+1)==1
fprintf('The chain code for (%d,%d)= %d.\n',x,y,w)
newx=x;
newy=y+1;
break;
end
end
end
end
end

Best Answer

Well, you have a while loop that runs if x or y are within a certain range. Within that loop, you never modify x or y, so in theory, once in the loop it would never exit, since the loop condition never changes. Instead, you've created two new variables in the loop newx and newy that you never use again. That's one of the bug. Simple fix is to update x and y instead of creating newx and newy.
The reason your loop does not run for ever is that you immediately terminate the loop when one of the neighbouring pixel is 1. You're just lucky that none of the pixels are completely isolated otherwise you'd have encountered a forever running loop. I assume you actually want the opposite of what you've coded in that you want the loop to continue if there is a neighbour and exit if there is none, so actually your break statement should be within a final else that you'd hit if no neighbour is found.
Note that the conditional statement for your while loop is also wrong. You never check for an upper x.
Finally, note that you use x to denote rows of your image and y to denote columns. This is contrary to normal convention, x is normally columns and y rows.