【问题标题】:Color extraction, quantification and analysis from image in RR中图像的颜色提取、量化和分析
【发布时间】:2017-06-25 04:42:54
【问题描述】:

我想quantifying colors in an image。 我研究珍珠母(珍珠母)的虹彩,我想量化这个贝壳上的三种颜色(红色、黄色和绿色)(例如上面链接中的右图)。

Iridescence of nacre

所以,我测试了一些软件包(imagerImageMagickEBImage...),但我并没有真正找到对我有帮助的东西。

嗯,我想在R 上用圆圈进行颜色量化。图元以像素为单位的面积可以表示为一个等效表面积的圆的面积。图元是相似颜色的相邻像素的连续区域。圆的中心可以是锚像素。 所以,有一个等式,我认为可以这样做:

DeltaI = 平方根[(Ranchor - Ri)² - (Ganchor - Gi)² - (Banchor - 双)²]

其中 R,G 和 B 是一个像素的颜色分量,范围从 0 到 255,anchor 是锚点像素,i 是锚点像素周围的任何相同颜色的像素。

期望结果有图片链接(来自Alçiçek & Balaban 2012):

Shrimp resulting equivalent circles

所以有我的(可启动工作)代码,但我真的不知道如何继续.. 可以尝试创建一个包?

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)

有人有曲目或任何想法吗? 感谢您的帮助..

【问题讨论】:

  • 您能否提供“虾产生的等效圆圈”的确切引用/文章?我查看了“Alçiçek & Balaban 2012”论文,但没有那张照片!
  • @Daniel,这篇文章有很好的链接(不幸的是,在我有 2 个帖子和 10 个声誉之前,我无法在编辑时发布 url 链接。我明白这就是图片的原因别人看不到..) s3.amazonaws.com/academia.edu.documents/41113722/…
  • 好的,谢谢我更新了你的参考!
  • 完美,非常感谢!

标签: r image colors analysis quantization


【解决方案1】:

好吧,我找到了另一种方式来回答我的问题:

  • 我使用load.imagefrom imager 包上传我的图片。
  • 我使用以下代码提取 RGB 通道:

    # Assign RGB channels to data frame
    nacreRGB <- data.frame(
    x = rep(1:nacreDm[2], each = nacreDm[1]),
    y = rep(nacreDm[1]:1, nacreDm[2]),
    R = as.vector(nacre[,,1]),
    G = as.vector(nacre[,,2]),
    B = as.vector(nacre[,,3])
    )
    # head(nacreRGB)
    
    # Assign RGB channels to data frame without pixel coordinates
    nacreRGB2 <- data.frame(
    R = as.vector(nacre[,,1]),
    G = as.vector(nacre[,,2]),
    B = as.vector(nacre[,,3])
    
  • 使用rgbSVG2rgbCSS 函数将其转换为十六进制代码后。

  • 我将其放入我称之为RGB0 的矩阵中,以创建直方图并用像素频率显示不同的颜色。
  • 在我执行 PCA 以显示这些颜色的分布之后:

    require("ggplot2")
    RGB0 <- as.data.frame(RGB0)
    
    # perform PCA on the nacre data and add the uv coordinates to the 
    dataframe
    PCA = prcomp(RGB0[,c("R","G","B")], center=TRUE, scale=TRUE)
    RGB0$u = PCA$x[,1]
    RGB0$v = PCA$x[,2]
    
  • 我用ggplot2 显示这个PCA。
  • 在此之后,我使用rgb2hsv 将 RGB 代码转换为 HSV 代码,我可以为色调、饱和度(色调到白色)和值(色调到黑暗)设置一个值,这样我就可以获得质量和数量数据关于颜色。

编辑:所有代码现在都发布到 ImaginR 包中的 CRAN 中: https://cran.r-project.org/web/packages/ImaginR/ImaginR.pdf

或者在 GitHub 上: https://github.com/PLStenger/ImaginR

这个版本没有真正量化颜色,但是下个版本很快就会出现。

【讨论】:

  • 能分享一下GitHub链接吗? THX
猜你喜欢
  • 2013-03-15
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
相关资源
最近更新 更多