Labelling image
Show older comments
Hello.
I`ve been trying to recognise simple shapes on photos and I`m not allowed to use bwlabel function, so I had to write simple labelling algorithm. It`s based on the one in: Tadeusiewicz R., Korohoda P. "Computer Analysis and Image Processing".
I don`t know the proper name for it in english, but I think it`s called: Connected-component labeling.
It works for basic example (pic1) but when there`s a lot of lines like this -> /, sudennly algorithm starts to assign few colors to one object (pic2 and pic3). bwlabel functions works properly of course.
close all;
clear all;
%%OBRAZKI
%%O = imread('rys1.png'); %%correct
%%O = imread('rys2.png'); %%bad
O = imread('rys3.png'); %%bad
%%binarisation
k = 80;
O = im2bw(O, k/255);
O=~O;
figure(1);
imshow(O);
%%algo
[X Y] = size(O);
OW = zeros(X, Y);
L = 1;
tab = zeros(255);
for i=2:X-1
for j=2:Y-1
if O(i, j) == 1
s = [OW(i-1, j-1), OW(i-1, j), OW(i-1, j+1), OW(i, j-1)];
if max(s) == 0
OW(i, j) = L;
tab(L) = L;
L = L + 1;
elseif min(nonzeros(s)) == max(nonzeros(s))
OW(i, j) = min(nonzeros(s));
else
OW(i, j) = min(nonzeros(s));
tab(max(nonzeros(s))) = min(nonzeros(s));
end
end
end
end
for i=2:X-1
for j=2:Y-1
if OW(i,j) ~= 0
OW(i,j) = tab(OW(i,j));
end
end
end
figure(2);
subplot(1,3,1);
imshow(OW, []);
title('Reczna');
OW = bwlabel(O, 4);
subplot(1,3,2);
imshow(OW, []);
title('BWLABEL 4');
OW = bwlabel(O, 8);
subplot(1,3,3);
imshow(OW, []);
title('BWLABEL 8');
Pictures are here:
Forgive me that you have to change the names yourself.
I would appreciate any suggestions or remarks.
Sincerely, Muss
PS I know my english is terrible, I`m so sorry.
Answers (2)
Image Analyst
on 7 Jan 2012
It looks like, from this line:
s = [OW(i-1, j-1), OW(i-1, j), OW(i-1, j+1), OW(i, j-1)];
that you have some kind of weird pattern for doing connected components labeling. Instead of a cross for 4-connected
0 1 0
1 1 1
0 1 0
it's using an "L" shape:
1 1 0
1 0 0
1 0 0
Instead of just looking at those 4 neighbors, look at all 8, so s will have additional terms to capture the corners:
s = [OW(i-1, j-1), OW(i-1, j), OW(i-1, j+1),... OW(i, j-1), OW(i, j), OW(i, j+1), ... OW(i+1, j-1), OW(i+1, j), OW(i+1, j+1)];
so you get an 8-connected pattern:
1 1 1
1 0 1
1 1 1
Muss
on 7 Jan 2012
0 votes
Categories
Find more on Text Detection and Recognition 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!