[datatable-help] Color extraction, quantification and analysis from image in R
Pierrel
Pierrelouis.stenger at gmail.com
Wed Feb 8 00:19:54 CET 2017
Dear R-help Team,
I would like to `quantifying colors in an image`.
I work on the iridescence of nacre (mother of pearl), and I want to
quantifying three colors (**red, yellow and green**) on this shell (for
example on the right picture on the link above).
<http://r.789695.n4.nabble.com/file/n4728690/xt7gE.jpg>
So, I had test some packages (`imager`, `ImageMagick`, `EBImage`...), but I
don't really find something that help me.
Well, I would like to make color quantification on R, 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:
> DeltaI = square root[(Ranchor - Ri)² - (Ganchor - Gi)² - (Banchor -Bi)²]
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.
There is a image link to the expectation results (from Alçiçek & Balaban
2012):
<http://r.789695.n4.nabble.com/file/n4728690/BMgXt.png>
So there is my (bootable worked) code, but I have really no idea how to
continue.. May be try to create a package ?
library(png)
nacre <- 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)
Someone have a track or any idea ?
Thanks for any help..
Link to the article:
http://s3.amazonaws.com/academia.edu.documents/41113722/tiger_prawn.pdf?AWSAccessKeyId=AKIAIWOWYYGZ2Y53UL3A&Expires=1486502777&Signature=ZcX1eV8nqS1%2BYRSgvJZyAURvCwo%3D&response-content-disposition=inline%3B%20filename%3DVisual_Attributes_of_Hot_Smoked_King_Sal.pdf
--
View this message in context: http://r.789695.n4.nabble.com/Color-extraction-quantification-and-analysis-from-image-in-R-tp4728690.html
Sent from the datatable-help mailing list archive at Nabble.com.
More information about the datatable-help
mailing list