title: R: Count objects in a picture
body: <p>I am currently working on a problem where I build a classifier using pictures to predict an outcome. Basically, I have objects on a picture (one or several), and I want to identify them. As I can have one object or several of them (not always identical), I first would like to be able to count how many objects are present on my picture. I don't want to run any machine learning algorithm and I'd like to have some way of doing it as fast as possible.</p>
  
  <p>Running a k-means (using color) enables me to separate the picture in 2 color groups which gives already a pretty good separation between background and objects. From this picture I'd like to find a way to count all the different "regions". I tried to run a k-means using both color and positions to try to do all in one step but it does not work out very well since adding positions to the kmeans degrades its result.</p>
  
  <p>Here is an example of what the picture would like, once having separated it in two color regions with the kmeans:</p>
  
  <p><a href="https://i.stack.imgur.com/y2yo8.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/y2yo8.jpg" alt="Objects in background"></a></p>
  
  <p>Obviously there are 5 objects and one background here. Or, alternatively, 6 distinctive regions.</p>
  
  <p>What I would like to do is run an algorithm on that picture that would tell me that number. I am not familiar with image processing so I cannot think of a fast and efficient way of doing it. One solution I would think of would be to take a pixel from the <strong>objects class</strong> and test if the neighbouring pixels belong to the same class or are actually part of the <strong>background class</strong>. But this would be a painfully long way of doing things.</p>
  
  <p>I have tried blob recognition but it does not look like this algorithm is suited for what I need.</p>
  
  <p>I would be quite interested in knowing how to do that kind of problem. I don't mind having to code the algorithm myself but I'm also interested in knowing if there are R packages that enables this kind of thing easily.</p>
  
  <p>I hope everything is clear enough. Thank you a lot in advance!</p>