
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Circles in a Digital Image Using MATLAB
In this article, we will learn to implement MATLAB codes to count the number of circles in a digital image. The counting the number of circles in an image is performed by using various image processing techniques like image erosion, circle detection, etc.
The step?by?step procedure to count the number of circles in a digital image in MATLAB is explained below:
Step (1)? Read the input image.
Step (2)? Convert the input image to grayscale, if necessary.
Step (3)? Create a circular or disk?shaped structuring element of a specific size for erosion of the image.
Step (4)? Erode the grayscale image using the circular structuring element to disconnect the circles from the image.
Step (5)? Apply labels to connected regions through connected component analysis.
Step (6)? Determine the unique component labels.
Step (7)? Count the number of circles in the image.
Step (8)? Display the eroded image and connected component map (label map) along with the number of circles found in the image.
Now, let us understand the implementation of this algorithm to count number of circles in a given digital image using MATLAB.
The following MATLAB program demonstrates the code implementation as per the above algorithm to count the number of circles in a digital image.
Example
%MATLAB code to count number of circles in a digital image % Read the input image in_img = imread('https://solarianprogrammer.com/images/2015/05/08/circles.jpg'); % Convert the input image to grayscale image, if necessary gray_img= rgb2gray(in_img); % Create a circular structuring element for image erosion r = 10; % Radius of the circular structuring element structuring_element = strel('disk', r, 0); % Perform erosion of the grayscale image erd_img = imerode(gray_img, structuring_element); % Apply labels to the connected components con_comp = bwlabel(erd_img, 8); % Determine the unique component labels unique_labels = unique(con_comp); % Count the number of circles in the image circles = numel(unique_labels) - 1; % Display the eroded image and the connected component map figure; subplot(1, 3, 1); imshow(in_img); title('Input Image'); subplot(1, 3, 2); imshow(erd_img); title('Eroded Image'); subplot(1, 3, 3); imshow(con_comp, []); title('Connected Component Map'); % Show the number of circles in the image disp('Number of circles found is:'); disp(circles);
Output
Number of circles found is: 1
Image Output

Code Explanation
This MATLAB program shows the code implementation to count the number of circles in a given digital image. In this code, we start by reading an input image using the ?imread' function and store it in a variable ?in_img'. Then, we convert the input image to grayscale, if not and store in a variable ?gray_img'.
Next, we define a circular or disk?shaped structuring element ?structuring_element' of a specific radius to perform erosion of the image. After that we use the ?imerode' function to perform erosion of the image according to the structuring element and store the result in a variable ?erd_img'. Then, we label the connected component and find the unique component labels. Next, we count the number of circles in the image.
Finally, we use the ?disp' function to display the number of circles, original image, eroded image, and the connected component map.
Conclusion
Hence, this is all about counting the number of circles in a given digital image using MATLAB programming. MATLAB provides various image processing techniques to count the number of circles in an image. In this article, we have explained the easiest technique using image erosion to count the number of circles. Try the above MATLAB code with your own image.