title: How to extract coordinates of colored dots from a jpeg image?
body: <p>I am trying to measure distances between objects of interests (in this example year rings in trees) using R. <a href="https://stackoverflow.com/questions/8832810/r-imagej-measuring-shortest-distance-between-points-and-curves">My earlier attempt</a> was so complicated that I have difficulties reproducing the solution for a similar type of a problem using different kinds of figures. I think that there must be an easier way of doing the measurements. As nice as ImageJ might be for picture analysis, I find it too clumsy to use for repetitive work. Why not just to mark the objects of interest with different colors using an image handling program and trying to extract the information about their position? (this is not the question). Here is an example:</p>
  
  <p><img src="https://i.stack.imgur.com/ykqo9.jpg" alt="enter image description here"></p>
  
  <p>(Save the picture as <em>tree.jpg</em>). In order to measure the distance from the beginning (blue dot) to the red and green dots (representing two different arbitrary measurements), I need to extract the centroid and color characteristic (i.e. whether the dot is green, blue or red) of each dot in the picture.</p>
  
  <p>The colors I have used are following:</p>
  
  <pre><code>cols &lt;- list(red = rgb(255/255, 0/255, 0/255), green = rgb(0/255, 255/255, 0/255), blue = rgb(0/255, 0/255, 255/255))
  </code></pre>
  
  <p>I have managed to open the file and plot it:</p>
  
  <pre><code>library("jpeg")
  img &lt;- readJPEG("tree.jpg")
  ydim &lt;- attributes(img)$dim[1] # Image dimension y-axis
  xdim &lt;- attributes(img)$dim[2] # Image dimension x-axis
  plot(c(0,xdim), c(0,ydim), type='n')
  rasterImage(img, 0,0,xdim,ydim)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/0lvYK.jpg" alt="enter image description here"></p>
  
  <p>Dimensions in the plot are in pixels. I can also extract the information in one of the RGB channels (here in green):</p>
  
  <pre><code>plot(c(0,xdim), c(0,ydim), type='n')
  rasterImage(img[,,2], 0,0,xdim,ydim)
  </code></pre>
  
  <p>After this I am starting to experience problems. I have found out that <a href="http://cran.r-project.org/web/packages/Momocs/Momocs.pdf" rel="nofollow noreferrer"><code>Momocs</code> package</a>, might be able to extract the shapes from RGB channel matrices, but I doubt that it is the right tool for this problem. Maybe one of the spatial packages could work? (I did not find a function for this purpose, though). How do I extract the position (in pixels using an arbitrary coordinate system) of colored dots from an image using R?</p>