title: Color extraction, quantification and analysis from image in R
body: <p>I would like to <code>quantifying colors in an image</code>. 
  I work on the iridescence of nacre (mother of pearl), and I want to quantifying three colors (<strong>red, yellow and green</strong>) on this shell (for example on the right picture on the link above).</p>
  
  <p><a href="https://i.stack.imgur.com/BIynZ.jpg" rel="nofollow noreferrer">Iridescence of nacre</a></p>
  
  <p>So, I had test some packages (<code>imager</code>,  <code>ImageMagick</code>, <code>EBImage</code>...), but I don't really find something that help me.</p>
  
  <p>Well, <strong>I would like to make color quantification</strong> on <code>R</code>, with circles. The area of ​​the primitive in pixel may be expressed as that of a circle of equivalent surface area. The primitive is a contiguous area of neighboring pixels of similar color. The center of the circle can be the anchor pixel. 
  So, there is the equation which I think it's ok to do this:</p>
  
  <blockquote>
    <p>DeltaI = square root[(Ranchor - Ri)² - (Ganchor - Gi)² - (Banchor -
    Bi)²]</p>
  </blockquote>
  
  <p>Where R,G and B are color components of a pixel, ranging from 0 to 255, anchor is the anchor pixel and i is any pixel around the anchor pixel which are the same equivalent color.</p>
  
  <p>There is a image link to the expectation results (from <a href="http://s3.amazonaws.com/academia.edu.documents/41113722/tiger_prawn.pdf?AWSAccessKeyId=AKIAIWOWYYGZ2Y53UL3A&amp;Expires=1486502777&amp;Signature=ZcX1eV8nqS1%2BYRSgvJZyAURvCwo%3D&amp;response-content-disposition=inline%3B%20filename%3DVisual_Attributes_of_Hot_Smoked_King_Sal.pdf" rel="nofollow noreferrer">Alçiçek &amp; Balaban 2012</a>):</p>
  
  <p><a href="https://i.stack.imgur.com/BMgXt.png" rel="nofollow noreferrer">Shrimp resulting equivalent circles</a></p>
  
  <p>So there is my (bootable worked) code, but I have really no idea how to continue.. May be try to create a package ?</p>
  
  <pre><code>library(png)
  nacre &lt;-  readPNG("test.png")
  nacre
  
  dim(nacre)
  
  # show the full RGB image
  grid.raster(nacre)
  
  # show the 3 channels in separate images
  nacre.R = nacre
  nacre.G = nacre
  nacre.B = nacre
  
  # zero out the non-contributing channels for each image copy
  nacre.R[,,2:3] = 0
  nacre.G[,,1]=0
  nacre.G[,,3]=0
  nacre.B[,,1:2]=0
  
  # build the image grid
  img1 = rasterGrob(nacre.R)
  img2 = rasterGrob(nacre.G)
  img3 = rasterGrob(nacre.B)
  grid.arrange(img1, img2, img3, nrow=1)
  # Now let’s segment this image. First, we need to reshape the array into a data frame with one row for each pixel and three columns for the RGB channels:
  
  
  # reshape image into a data frame
  df = data.frame(
  red = matrix(nacre[,,1], ncol=1),
  green = matrix(nacre[,,2], ncol=1),
  blue = matrix(nacre[,,3], ncol=1)
    )
  
  
  ### compute the k-means clustering
  K = kmeans(df,4)
  df$label = K$cluster
  
  ### Replace the color of each pixel in the image with the mean 
  ### R,G, and B values of the cluster in which the pixel resides:
  
  # get the coloring
  colors = data.frame(
    label = 1:nrow(K$centers), 
    R = K$centers[,"red"],
    G = K$centers[,"green"],
    B = K$centers[,"blue"]
  )
  
  # merge color codes on to df
  df$order = 1:nrow(df)
  df = merge(df, colors)
  df = df[order(df$order),]
  df$order = NULL
  
  # get mean color channel values for each row of the df.
  R = matrix(df$R, nrow=dim(nacre)[1])
  G = matrix(df$G, nrow=dim(nacre)[1])
  B = matrix(df$B, nrow=dim(nacre)[1])
  
  # reconstitute the segmented image in the same shape as the input image
  nacre.segmented = array(dim=dim(nacre))
  nacre.segmented[,,1] = R
  nacre.segmented[,,2] = G
  nacre.segmented[,,3] = B
  
  # View the result
  grid.raster(nacre.segmented)
  </code></pre>
  
  <p>Someone have a track or any idea ?
  Thanks for any help..</p>