How to Count the Number of Circles in Given Digital Image Using MATLAB? Last Updated : 06 Mar, 2022 Comments Improve Suggest changes Like Article Like Report 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: 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.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 whiteCode 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. Comment More infoAdvertise with us P pintusaini Follow Improve Article Tags : Software Engineering MATLAB image-processing Similar Reads How to Draw a Circle of Given Radius R in MATLAB? A closed plane figure, which is formed by the set of all those points which are equidistant from a fixed point in the same plane, is called a Circle. Now we will draw a circle of a given radius R. To draw a circle below are the points or procedures to remember. Steps:declare a variable that stores r 2 min read Image Processing in MATLAB | Fundamental Operations 1. Reading Images Images are read into the MATLAB Environment using imread() function which takes filename with applicable extension as the argument For Example: >> I=imread('nature.jpg'); This will read JPEG image 'nature' into the image array. Note: The semicolon(;) at the end of command lin 3 min read Auto Cropping- Based on Labeling the Connected Components using MATLAB Auto Cropping Based on labeling the connected components is related to digital image processing. So, In this article, we will discuss the Auto Cropping-Based on labeling the connected components. Before that look at the basic terms which are required in this topic. Auto CroppingIn terms of MATLAB, t 3 min read Edge detection using in-built function in MATLAB Edge detection: In an image, an edge is a curve that follows a path of rapid change in intensity of that image. Edges are often associated with the boundaries of the object in a scene environment. Edge detection is used to identify the edges in an image to make image processing easy. Edge detection 2 min read How To Identifying Objects Based On Label in MATLAB? labeling means identifying and placing labels on each program of an object project in the image. in Binary image, it's classified as 4-connected and 8-connected. for performing the labeling in MATLAB we use the Built-in function bwlabel() used to label the object in the binary image. So, In this art 4 min read MATLAB | Display histogram of a grayscale Image An image histogram is chart representation of the distribution of intensities in an Indexed image or grayscale image. It shows how many times each intensity value in image occurs. Code #1: Display histogram of an image using MATLAB library function. MATLAB % Read an Image in MATLAB Environment img=i 2 min read Like