Main Content

Detect and Measure Circular Objects in an Image

This example shows how to read an image from a website and count the number of circular objects in the image using ThingSpeak™ and Image Processing Toolbox™. The computed value is stored in a ThingSpeak channel.

Read Image from URL

Use webread to import the image from a public URL. Image files have a lot of data, you only need a subset of the image data to count the coins. To keep the processing time short, you can resize the image. Use imresize to cut the image to 30% of its original size.

rgb = webread('');
rgb = imresize(rgb, 0.3);

Adjust Sensitivity Settings and Count Circles

Besides having multiple circles to detect, the image contains coins of different colors, which have different contrast with respect to the background. The brass-colored coins have strong contrast against this background. The silver coins are much closer in color to the background. Use imfindcircles to count the coins.

1. By default, imfindcircles finds circular objects that are brighter than the background. Set its parameter 'ObjectPolarity' to 'dark' to search for dark circles.

2. The function imfindcircles has a parameter 'Sensitivity' which you can use to control the internal threshold while finding circular objects. Set the 'Sensitivity'' to 0.92.

3. Call imfindcircles on this image with the search radius of [80 130] pixels. The length of the centers vector is equal to the number of circles found.

[centers, radii] = imfindcircles(rgb,[80 130],'ObjectPolarity','dark','Sensitivity',0.92);

numCircles = length(centers)
numCircles =


Store the Results in a ThingSpeak Channel

You can store and track the content of a dynamic image with this example and a ThingSpeak channel. Write the number of circles to a ThingSpeak Channel specified by channelID. Change channelID to be your channel ID, and specify the Write API Key for your own channel with writeAPIKey.

thingSpeakWrite(channelID, numCircles, 'Writekey', writeAPIKey);

See Also

(Image Processing Toolbox) | (MATLAB) | (MATLAB)

Related Topics