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

1 view (last 30 days)
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);

Answers (1)

meli brilian
meli brilian on 17 Aug 2020
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

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!