Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Count the Number of Circles in Given Digital Image Using MATLAB?

  • Last Updated : 06 Mar, 2022

In image processing, connected component analysis is the technique to count and inspect the segments automatically. Let suppose, we have an image consisting of small circles. There are hundreds of circles in the image. We need to count the number of circles. It will take a lot of time if we count manually, and still, the result may not be correct. On the other hand, we can use connected component analysis to count the number easily and accurately.

A necessary condition is that the segments or shapes which we are interested to count should be disconnected from each other. Two circles connected by one common pixel will be counted as 1. Thus, the primary and essential condition is that each shape must be separated.


  • Read the input image.
  • Convert color image into grayscale.
  • Create a disk-shaped structuring element.
  • Perform erosion of the image using structuring elements.
  • Apply connected component analysis and label the components.
  • Count the labels.
  • Show the results.

Functions Used:

  • imread( ) inbuilt function is used to read the image.
  • rgb2gray( ) inbuilt function is used to convert RGB image into grayscale image.
  • strel( ) inbuilt function is used to define structuring element.
  • imerode( ) inbuilt function is used to perform erosion.
  • bwlabel( ) inbuilt function is used to apply connected component analysis.
  • imtool( ) inbuilt function is used to display the image.
  • max( ) inbuilt function is used to find maximum among all values.



% MATLAB code for count the
% number of circles in the image.
% read the colored image.
% Convert into grayscale.
% Create the structuring element.
% Erode the image to disconnect the circles.
% Display the image.
% Apply connected component analysis.
% Find the unique component labels.
% Print the last component label.
% Display the coloured map.

Output: max=20 Therefore, there are 20 circles in the input image.


Figure 1: Original image


Figure 2: Eroded image


Figure 3: Labelled image colour cube 

Code Explanation:

  • k=rgb2gray(k); This line converts colour image into grayscale.
  • SE=strel(‘disk’,9,0); This line defines the structuring element.
  • k1=imerode(k,SE); This line will compute eroded image.
  • b=bwlabel(k1,8); This line will apply connected component analysis.
  • max(c); This line will count the number of circles in the image.

Example 2:


% MATLAB code for
% Connected Component Analysis
% Read the colored image.
% Resize the image for it is large.
% Convert to grayscale.
% Display the image.
% Convert it into binary image.
% Display the binary image.
% Reverse the binary image.
% Display the reversed binary image.
% apply connected component analysis.
% find the unique labels.
c = unique(b);
% Find the max value.
% Display the colored map image.


Output: max = 629, Therefore, there are 629 holes in the beehive.




Figure 4: Original image



Figure 5:  Binary image: Bricks are black



Figure 6: Binary image: Bricks are white

Code Explanation:


  • k1=rgb2gray(k1); This line converts color image into grayscale.
  • k2=im2bw(k1,graythresh(k1)); This line converts grayscale into binary image.
  • b=bwlabel(k3,8); This line applies connected component analysis.
  • c=unique(b); This line finds the unique labels.
  • max(c); This line finds the max value.
  • imtool(b,[]); This line displays the colored map image.



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!