My code for finding connected components in grayscale image is not working. Can anyone help me please?

1 次查看(过去 30 天)
I have tried to implement an algorithm for finding connected components in grayscale image from the pdf inserted. Here is also the link: http://journals.dbuniversity.ac.in/ojs/index.php/AJET/article/view/270. But my code is not working, it is going to infinity loop. Can anyone please correct me? My code is :
clear all; clc;
I = imread('shadow9.jpg');
I1 = rgb2ycbcr(I);
Y = I1(:,:,1);
[r, c] = size(Y);
Out = zeros(r,c);
label = 0; j=0; count =0;
%calculating range
for i=1:255
pixel_range(i) = j;
count = count + 1;
if mod(count,16) == 0
j = j+1;
end
end
for i=1:r
for j=1:c
if Y(i,j)>=0
label = label + 1;
%getting range
pixel_int = ceil(Y(i,j));
if pixel_int == 0
val = 0;
else val = pixel_range(pixel_int);
end
stack = [i j];
while ~isempty(stack)
loc = stack(1,:); %getting top pixel
stack(1,:) = []; % removing first pixel row
%// get the 8 neighbouring locations
locxy = [loc(1),loc(2)+1; loc(1),loc(2)-1; loc(1)+1,loc(2); loc(1)-1,loc(2); loc(1)+1,loc(2)+1; loc(1)+1,loc(2)-1; loc(1)-1,loc(2)+1; loc(1)-1,loc(2)-1];
%mapping pixel range
for p=1:8
if(locxy(p,1)>0 && locxy(p,2)>0)
if(locxy(p,1)<=c && locxy(p,2)<=r)
pixel_int1 = ceil(Y(locxy(p,1),locxy(p,2)));
if pixel_int1 == 0
val1 = 0;
else val1 = pixel_range(pixel_int1);
end
if val == val1
stack = [stack; [locxy(p,1) locxy(p,2)]]; %Adding to stack
Y(locxy(p,1),locxy(p,2)) = -1;
Out(locxy(p,1),locxy(p,2)) = label;
end
end
end
end
end
end
end
end
B = label2rgb(Out);
figure(1); imshow(B);

回答(1 个)

meli brilian
meli brilian 2020-8-17
hello? have you find the solution of your problem? i have this problem too.. please let me know if you already find the solution. Thanks in advance

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by