Skip to content

MATLAB program used to count the number of bottles detected in an image

Notifications You must be signed in to change notification settings

djohnson418/Counting_Bottles

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

My approach was to go from completing the easiest tasks to the hardest tasks. We will create a first file containing the function count_bottles and then call this function in a second MATLAB file (count_call.m). We will use the imfindcircles function[1] to find the circles in the image using the Hough transform, and we will set the parameters for the radius range and sensitivity of finding circles. The function finds the centers and radii based on the metrics we set.

We start by reading the image. To make it easier for the function to detect the circles, we will apply a Guassian blur[2] and then use Sobel edge detection[3] to find and isolate the edges in the image. We initialize the bottle_count variable before starting and show the image as a figure. We use the imfindcircles function to find the circles of the bottle caps, then we loop through the detected circles and draw them at the center point with the correct radius using the viscircles function[4] to match the shape of the bottle cap edges. Then we use a second imfindcircles function to find the circles of the bottoms of the bottles which have a larger radius, then we loop through the detected circles and draw them at their center point to match the bottom bottle edges. Finally, we use centroid shape detection and use the Area, Perimeter, and Bounding Box metrics to distinguish the shapes we need to detect the sideways bottles. The pictures are contained in an Images folder in MATLAB, so the call to the function will loop through the image folder directory[5] and perform the bottle_count function on all 24 test case images in the folder.

Changing the radius range to a certain pixel width will allow us to first detect the bottle caps, since it is the easiest task and takes up the majority of the bottles in the crate. I started with a radius range of 20 to 25 pixels and 0.95 sensitivity as a baseline then moved from there, eventually landing on 18 to 23 pixels and 0.929 sensitivity. The sensitivity is a very arbitrary number that removes all false positives and still counts all the correct bottle caps. With different images the program may produce incorrect results; however, it works at a 100% rate for the 24 test cases provided. Some of the images that had issues as I changed the sensitivity were Image 6 with seeing the bigger base of one of the bottles, Image 12 with one of the bottles being covered, Image 18, Image 20, and Image 23. The bottle caps are marked as blue in the final images.

Next, we move on to detecting the bottoms of the bottles, which have a larger radius. It is the same process as for detecting the bottle caps using the imfindcircles function, just with a different radius range and sensitivity. I started with a radius of 32 to 40 pixels and 0.93 sensitivity, and eventually landed on a radius range of 34 to 40 pixels and 0.9264 sensitivity. Once again these are very arbitrary, and early on I had issues detecting bottom sides of bottles in Image 8 and Image 16. Luckily, this radius range does not detect the large cups in some of the images, since their radius is even larger than 40 pixels. The final radius range and sensitivity result in a 100% success rate for detecting the bottom sides of bottles, but again, different images may produce incorrect results. The bottoms of the bottles are marked as green in the final images.

Finally, we need to detect the sideways bottles in Image 10 and Image 15. The method I used to detect this was centroid detection. The Centroid function and regionprops function[5] allow us to detect certain shapes and extract statistical data to categorize and isolate the shapes. We will be looking for shapes with a certain area, perimeter, and bounding box width (i.e. BoundingBox(3) ) in order to detect our shapes. This is contained in one if statement in the program, stating that if the Area of the centroid is between 150 and 220 pixels, then a red circle is drawn. Additionally, if the Perimeter of the centroid is below 180 pixels or above 480 pixels, and the width of the Bounding Box is above 100 pixels, then a red circle is drawn. Some images that had issues were Image 16 and any image with an upside down bottle or a cup, since the area and perimeter often matches those metrics above. This is a very unreliable method and most likely is to make mistakes, either not detecting sideways bottles or giving out false positives; however, it is a 100% success rate for the sideways bottles in Image 10 and 15. The sideways bottles are marked as red in the final images.

[1] https://se.mathworks.com/help/images/ref/imfindcircles.html

[2] https://se.mathworks.com/help/images/ref/imgaussfilt.html

[3] https://se.mathworks.com/help/images/edge-detection.html

[4] https://se.mathworks.com/help/images/ref/viscircles.html

[5] https://se.mathworks.com/matlabcentral/answers/411500-how-do-i-read-all-the-files-in-a-folder

[6] https://se.mathworks.com/help/images/ref/regionprops.html#buorh5j-1 https://se.mathworks.com/matlabcentral/answers/110855-how-to-detect-the-shape-in-matlab

About

MATLAB program used to count the number of bottles detected in an image

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages