Connected component Labelling
This program demonstrate the segmentation process by classifying the connected pixels in the image. This program can be used
for automatic coins counting machine, if images of coins taken with Black background and the coins are places at a safe distance.
to try this, take an image of coins placing them on black surface and use this image as input for this program
Contents
Start
clear all;
close all;
clc;
Read the Image
I = rgb2gray(imread('coin.jpg'));
title('Original Image');
imshow(I);
Convert it to binary image
ima = I>128;
ima = bwareaopen(ima,30);
figure; imshow(ima);
Connected Component Labelling Algorithm
[a b] = size(ima);
label = 0;
D = [];
for i=1:a
for j = 1:b
if ima(i,j) == 0
Im(i,j) = 0;
else
switch ima(i-1,j)+10*ima(i, j-1)
case 0
label = label+1;
Im(i,j) = label;
case 1
Im(i,j) = Im(i-1, j);
case 10
Im(i,j) = Im(i ,j-1);
case 11
if Im(i-1,j) == Im(i, j-1)
Im(i,j) = Im(i, j-1);
else
Im(i,j) = Im(i-1, j);
Im(Im == Im(i-1,j)) = Im(i, j-1);
end
end
end
end
end
Label Conflicts Compensation
for i = 1:2:length(D)
m = minmax(D(i:i+1));
Im(Im==m(2)) = m(1);
d((i+1)/2) = D(i);
end
Renaming the labels
label = 1;
for i = 1:max(max(Im))
if sum(sum(Im==i))>10
Im(Im==i) = label;
label = label+1;
end
end
Display of the results
figure;imshow(10*uint8(Im));
t = ['Number of connected regions = ' num2str(max(max(Im)))];
title(t);
disp(['Number of coins in the image are: ', num2str(max(max(Im)))]);
Number of coins in the image are: 9
No comments:
Post a Comment