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 gettin

1 次查看(过去 30 天)
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

采纳的回答

Guillaume
Guillaume 2016-2-26
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.
  2 个评论
Guillaume
Guillaume 2016-2-26
Note that rather than have a bunch of elseif this is how I would do code the loop:
codes = [3 4 5; 2 NaN 6; 1 0 7] + 1;
for ...
for ...
if ...
while ... %condition needs fixing
validcodes = codes .* B(x-1:x+1, y-1:y+1);
[code, location] = max(validcodes);
if isempty(code) %no neighbour
break;
end
fprintf('The chain code for (%d,%d)= %d.\n',x,y,code)
[ox, oy] = ind2sub([3 3], location); %convert loc in x and y in [1 2 3]
x = x + ox - 2; %we want [1 2 3] to be [-1 0 1] so subtract 2
y = y + oy - 2;
end
...
Ekta Sharma
Ekta Sharma 2016-2-27
Hello Sir
Thanks a lot for your reply.But doing changes as per you code didn't solve my purpose.Can you please make changes in my code as it will be more easy for me to understand.Thanks in advance

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by