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
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('https://www.publicdomainpictures.net/pictures/40000/velka/british-coins.jpg'); rgb = imresize(rgb, 0.3); imshow(rgb)
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
'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.
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 = 5
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
channelID=17504; writeAPIKey='23ZLGOBBU9TWHG2H'; thingSpeakWrite(channelID, numCircles, 'Writekey', writeAPIKey);
- Get Started with Image Processing Toolbox (Image Processing Toolbox)