# 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.

Steps:

• 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:

• 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.

Example:

## Matlab

 `% MATLAB code for count the``% number of circles in the image.``% read the colored image.``k=imread(``"CCA1.png"``);` `% Convert into grayscale.``k=rgb2gray(k);` `% Create the structuring element.``SE=strel(``'disk'``,9,0);` `% Erode the image to disconnect the circles.``k1=imerode(k,SE);` `% Display the image.``imtool(k1);` `% Apply connected component analysis.``b=bwlabel(k1,8);` `% Find the unique component labels.``c=unique(b);` `% Print the last component label.``max(c);` `% Display the coloured map.``imtool(b,[]);`

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

 `% MATLAB code for``% Connected Component Analysis``% Read the colored image.``k=imread(``"7.jpg"``);` `% Resize the image for it is large.``k1=imresize(k,0.3);` `% Convert to grayscale.``k1=rgb2gray(k1);` `% Display the image.``imtool(k1,[]);` `% Convert it into binary image.``k2=im2bw(k1,graythresh(k1));` `% Display the binary image.``imtool(k2,[]);` `% Reverse the binary image.``k3=1-k2;` `% Display the reversed binary image.``imtool(k3,[]);` `% apply connected component analysis.``b=bwlabel(k3,8);` `% find the unique labels.``c = unique(b);` `% Find the max value.``max(c)` `% Display the colored map image.``imtool(b,[]);`

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