Connected Component labeling without using bwlabel or bwconncomp functions ?
23 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 :
0 commentaires
Réponse acceptée
Thorsten
le 23 Sep 2015
Modifié(e) : Thorsten
le 23 Sep 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
Plus de réponses (1)
Image Analyst
le 24 Sep 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?
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!