You are currently viewing Matlab Code to Count the no of Objects in a Binary Image

Matlab Code to Count the no of Objects in a Binary Image

Spread the love

Matlab Code to Count the no of Objects in a Binary Image

This code explains to count the binary labeled objects.this code could be used in numerous image processing applications.

Simulation Results

First read the Input Image

clc;
clear;
close all;
% Read the Input Image
InputImage=imread('eight.tif');
% Display the Input Image

subplot(2,2,1);

imshow(InputImage);title('InputImage');

Convert the Gray Scale Image to Binary Image

% Convery Gray Scale Image to Binary Image

BinaryImage=im2bw(InputImage);

%Display Binary Image

subplot(2,2,2);

imshow(BinaryImage);

Complement the Binary Image

% Complement the Binary Image

ComplementImage=imcomplement(BinaryImage);

subplot(2,2,3);

imshow(ComplementImage);

Perform morphological operations

%     Fill in the holes of a binary image
HolesClearedImage = imfill(ComplementImage,'holes');

%Display Holes Cleared Image

subplot(2,2,4);

imshow(HolesClearedImage);title('HolesClearedImage');

Apply labels for each binary connected component

%Apply Labels for each  connected components in 2-D binary image

[L,Num] = bwlabel(HolesClearedImage)

 %Display each label

figure;
for i=1:Num

subplot(2,2,i);
imshow(L==i);
pause(1)
end

matlab-code-to-count-the-no-of-objects-in-image