R中图像的颜色提取、量化和分析
Color extraction, quantification and analysis from image in R
我愿意quantifying colors in an image
。
我研究珍珠母(珍珠母)的虹彩,我想在这个 shell 上量化三种颜色(红色、黄色和绿色)(例如右边图片在上面 link 上)。
Iridescence of nacre
所以,我测试了一些包(imager
、ImageMagick
、EBImage
...),但我没有真正找到对我有帮助的东西。
嗯,我想在R
上做颜色量化,用圆圈。以像素为单位的图元面积可以表示为等效表面积的圆的面积。图元是具有相似颜色的相邻像素的连续区域。圆心可以是锚点像素。
所以,有一个方程式,我认为这样做是可以的:
DeltaI = square root[(Ranchor - Ri)² - (Ganchor - Gi)² - (Banchor -
Bi)²]
其中R、G、B是像素的颜色分量,范围从0到255,anchor是锚点像素,i是锚点像素周围任意一个具有相同等效颜色的像素。
有一张图片link到期望结果(来自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)
有人有曲目或任何想法吗?
感谢您的帮助..
好吧,我找到了另一种方式来回答我的问题:
- 我用
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
函数将其转换为十六进制代码后。
- 我将其放入一个矩阵中,我称之为
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
这个版本并没有真正量化颜色,但是下个版本很快就会出现。
我愿意quantifying colors in an image
。
我研究珍珠母(珍珠母)的虹彩,我想在这个 shell 上量化三种颜色(红色、黄色和绿色)(例如右边图片在上面 link 上)。
Iridescence of nacre
所以,我测试了一些包(imager
、ImageMagick
、EBImage
...),但我没有真正找到对我有帮助的东西。
嗯,我想在R
上做颜色量化,用圆圈。以像素为单位的图元面积可以表示为等效表面积的圆的面积。图元是具有相似颜色的相邻像素的连续区域。圆心可以是锚点像素。
所以,有一个方程式,我认为这样做是可以的:
DeltaI = square root[(Ranchor - Ri)² - (Ganchor - Gi)² - (Banchor - Bi)²]
其中R、G、B是像素的颜色分量,范围从0到255,anchor是锚点像素,i是锚点像素周围任意一个具有相同等效颜色的像素。
有一张图片link到期望结果(来自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)
有人有曲目或任何想法吗? 感谢您的帮助..
好吧,我找到了另一种方式来回答我的问题:
- 我用
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
函数将其转换为十六进制代码后。- 我将其放入一个矩阵中,我称之为
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
这个版本并没有真正量化颜色,但是下个版本很快就会出现。