# Which algorithm for counting the occurrences of a certain pattern (spots) in an image?

The Problem :

Finding the number of occurrences of a certain pattern (or shape) in an image. In my example, the problem is about finding the number of spots (in variety of sizes) in an image. See figure 1 below: What I'm looking for here is an orientation. What class of algorithms can lead to a solution for this type of problems?

• I confess that I know nothing at all about computer vision but this looks like the sort of thing that would be covered in chapter 1 of any textbook. So, what did you try? Where did you get stuck? We expect you to do a reasonable amount of research before asking others to do that for you. Apr 5, 2016 at 19:51
• In tasks like this there are several important questions: is background always that cute? (Plain black helps), are objects overlapping? (If not, it helps). Are objects always in color scheme from example? Are you interested only in angle? What is orientation of perfect circle? Are spots regular enough to say that bounding box will give direction?
– Evil
Apr 5, 2016 at 20:10
• @EvilJS, I suspect here "orientation" means "please give me an overview of the area" (rather than "how do I determine the pose / direction that the blob is pointing?"). Redjimi, If that's not correct, please edit the post.
– D.W.
Apr 5, 2016 at 20:53
• @D.W. I thought rather about collection of {centroid, azimuth}, and about finding this overview about easier, lightweight methods - edges, contour tracing. Of course there is Hough for ellipses, but even in short version it is rather overkill, contour fill would suffice.
– Evil
Apr 5, 2016 at 21:08

The best way to get an orientation is to read a textbook on computer vision or image processing. There are lots of techniques known, and that's often the best way to get an introduction to a broad topic like this.

Anyway, for some specific techniques, I would recommend that you learn about the following candidate approaches:

Your task looks pretty easy, so I imagine you should be able to find a very effective combination of techniques if you learn about them and try them out on sample images.

There is one book freely available worth mentioning: Computer Vision Algorithms and Applications by Richard Szeliski.

If there is possibility that background is black and elements are not overlapping the object detection eases to scan through pixels while they are black and then using modified flood fill algorithm, connecting everything that is not black an marking as visited pixels underneath.
During flood fill all pixel positions should be stored to calculate center and probably other statistics like principal component.

First thing, you must be clear about the pattern because computer vision has still focused on pattern specific algorithms.

If I consider your pattern is circularity if I am not wrong you are asking how to measure the number of circular instances in an image?

To solve such query, you should go for the question "How to measure the circularity?" The question will lead you towards the hough transform algorithms. There are number of algorithms based on Hough Transform.

I prefer this, and that is your answer to measure circular instances in an image. It will also give you how much circular they are.

T.J Atherton, D.J. Kerbyson. "Size invariant circle detection." Image and Vision Computing. Volume 17, Number 11, 1999, pp. 795-803.

If the images your problem should deal with are like the ones you show, then you must check the watershed algorithm. There is a paper by Vicent and Soille, that has a good algorithm. However prior to apply this, you should also implement, digitalization (automatic is preferable), and distance transform. Some filtering is advisable too before you apply watersheding