我想quantifying colors in an image
。
我研究珍珠母(珍珠母)的彩虹色,我想在这个壳上量化三种颜色(红色,黄色和绿色)(例如在上面链接的右图上)。
所以,我测试了一些软件包(imager
,ImageMagick
,EBImage
...),但我找不到能帮到我的东西。
嗯,我想在R
上用圆圈进行颜色量化。像素中的图元的区域可以表示为等效表面区域的圆的区域。基元是相似颜色的相邻像素的连续区域。圆的中心可以是锚像素。
所以,有一个等式,我认为可以这样做:
DeltaI =平方根[(Ranchor - Ri)² - (Ganchor - Gi)² - (Banchor - 毕)²]
其中R,G和B是像素的颜色分量,范围从0到255,锚是锚像素,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)
有人有想法或任何想法吗? 谢谢你的帮助..
答案 0 :(得分:0)
我已找到另一种方式来回答我的问题:
load.image
包中的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
功能的HEX代码。
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
,我可以得到色调的值,饱和度(色调为白色)和值(色调为暗色),这样我就可以获得质量和关于颜色的数量数据。编辑:所有代码现在都在ImaginR包中发布到CRAN: https://cran.r-project.org/web/packages/ImaginR/ImaginR.pdf
或者在GitHub上: https://github.com/PLStenger/ImaginR
这个版本并没有真正量化颜色,但很快就会出现在下一个版本中。