首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中图像的颜色提取、量化与分析

R中图像的颜色提取、量化与分析
EN

Stack Overflow用户
提问于 2017-02-07 19:28:34
回答 1查看 3.1K关注 0票数 2

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

珍珠层彩虹

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

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

DeltaI =平方根(R锚-Ri)2-(G锚-Gi)2- (Banchor -Bi)2

其中R、G和B是像素的颜色分量,范围从0到255,锚点是锚像素,i是锚像素周围的任意像素,它们是相同的等效颜色。

有一个指向预期结果的图像链接(来自Al ek& Balaban 2012):

对虾产生等效圆

这是我的(可引导工作的)代码,但我真的不知道如何继续。可以尝试创建一个包吗?

代码语言:javascript
复制
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)

有人有赛道或者有什么想法吗?谢谢你的帮助..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-10 21:06:50

我找到了另一种方法来回答我的问题:

  • 我用load.imageimager包上传我的照片。
  • 我使用以下代码提取RGB通道:将RGB通道分配给数据帧nacreRGB <- data.frame( x= rep(1:nacreDm2,每条= nacreDm1),y= rep(nacreDm1:1,nacreDm2),R=as.vector(珍珠,1),G=as.vector(珍珠,2),B=as.vector(珍珠,3) # head(nacreRGB) #head(NacreRGB)#将RGB通道分配给没有像素坐标nacreRGB2 <- data.frame( R=as.vector(nacreDm2,1))、G=as.vector(as.vector)的数据帧。,2),B=as.vector(珍珠层,3)
  • 在我用rgbSVG2rgbCSS字体把它转换成HEX代码之后。
  • 我把它放入一个矩阵中,我称之为RGB0来创建直方图,并用像素频率显示不同的颜色。
  • 在执行PCA以显示这些颜色的分布之后: 要求(“ggplot2”) RGB0 <- as.data.frame(RGB0) #对珍珠层数据执行PCA,并将uv坐标添加到dataframe = prcomp(RGB0,c("R","G","B"),center=TRUE,scale=TRUE) RGB0 0$u= PCA$x,1 RGB0 0$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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42098307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档