Borrar filtros
Borrar filtros

Connected Component labeling without using bwlabel or bwconncomp functions ?

7 visualizaciones (últimos 30 días)
I have written a code for labelling and counting number of connected components in binary image. But I am not getting expected results. Can anyone find where I am making mistake ? My code is :
function cca()
A=imread('contours.jpg');
figure,imshow(A);
title('Original Image');
% k indicates number of components in binary image
k = 0;
global B;
B = zeros(size(A,1),size(A,2));
% to make sure boundary conditions, skip first row, column and last row,
% column. These will be taken care by recursive function calls later
for i = 2 : size(A,1) - 1
for j = 2 : size(A,2) - 1
if A(i,j) == 1 && B(i,j) == 0
k = k + 1;
rcca(i,j,A,k);
end
end
for i = 1 : size(A,1)
if A(i,1) == 1 && B(i,1) == 0
k = k + 1;
B(i,1) = k;
else
if A(i,size(A,2)) == 1 && B(i,size(A,2)) == 0
k = k + 1;
B(i,size(A,2)) = k;
end
end
end
for j = 1 : size(A,2)
if A(1,j) == 1 && B(1,j) == 0
k = k + 1;
B(1,j) = k;
else
if A(size(A,1),j) == 1 && B(size(A,1),j) == 0
k = k + 1;
B(size(A,1),j) = k;
end
end
end
fprintf('\ntotal number of components in image = %.0f\n',k);
Function rcca is as follows :
function rcca(x,y,A,k)
global B;
B(x,y) = k;
% dx and dy is used to check for 8 - neighbourhood connectivity
dx = [-1,0,1,1,1,0,-1,-1];
dy = [1,1,1,0,-1,-1,-1,0];
if x > 1 && y > 1 && x < size(A,1) && y < size(A,2)
for i = 1 : 8
nx = x + dx(i);
ny = y + dy(i);
if A(nx,ny) == 1 && B(nx,ny) == 0
rcca(nx,ny,A,k);
end
end
end
Please help me in finding logical error. Here is my image :

Respuesta aceptada

Thorsten
Thorsten el 23 de Sept. de 2015
Editada: Thorsten el 23 de Sept. de 2015
A is not a binary image, but a gray scale image with values ranging from 0 to 255. If you use imhist(A) to view the data, you see that the black background data actually spread from 0 to 55. So if you check for A(i,j)==1 in your code, you do not check for a foreground pixel, but for a background pixel of value 1. Assuming that your code is correct, you are labeling the background pixels set to 1 due to jpg artifacts.
So the first step would be to convert the image to binary image:
A = im2bw(A, 0.5);
Regarding the labeling algorithm, the following page may be useful https://en.wikipedia.org/wiki/Connected-component_labeling.
I implemented a labeling algorithm similar to yours. It works fine, but because of the heavy use of recursion it results in "Maximum recursion limit of 500 reached." for large images.
function B = thlabel
I = imread('contours.jpg');
I = imresize(I, 0.5); % reduce size not to reach Matlab's maximum
% recursion limit
global A
A = im2bw(I, 0.5);
global B
B = zeros(size(A));
global currentlabel
currentlabel = 1;
sz1 = size(A,1);
global offset
offset = [-sz1-1:-sz1+1 -1 +1 sz1-1:sz1+1];
for ind = 1:numel(A)
if A(ind) == 1 && B(ind) == 0
labelconnected(ind)
currentlabel = currentlabel + 1;
end
end
function labelconnected(ind)
global currentlabel
global offset
global A
global B
B(ind) = currentlabel;
imshow(B, []), drawnow
neighbors = ind + offset;
neighbors(neighbors <= 0 | neighbors > numel(A)) = [];
for ind = neighbors
if A(ind) == 1 && B(ind) == 0
labelconnected(ind)
end
end
  1 comentario
Sumit Khatri
Sumit Khatri el 24 de Sept. de 2015
Thanks, I got the correct answer by reducing the image size and changing it to black & white. This suggestion helped me a lot to find mistake in my code.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 24 de Sept. de 2015
I really don't know why you don't just use the solution I gave you, and you accepted, in your duplicate question. What was wrong with it? It worked. Why do you prefer to go through all that complicated stuff, instead of a single line of code, especially since you say it's not working?
  1 comentario
Sumit Khatri
Sumit Khatri el 24 de Sept. de 2015
Actually I just wanted to implement the algorithm and compare the time for inbuilt function and my function. Now I got to know how we should take care of all possible cases to process images. Sorry for so silly questions but since I am beginner to MatLab, I am getting such questions. But Thanks for your suggestion.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by