使用R的图像颜色组成 [英] Image colors composition using R

查看:255
本文介绍了使用R的图像颜色组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得构成给定图片的每种颜色的百分比。
我使用以下代码获取RGB矩阵:

I'm trying to obtain the percentage of each color which compose a given picture. I get the RGB matrix using this code:

library(jpeg)
img <- readJPEG("C:/Users/Pictures/img.jpg")
dim(img)
#145 371   3
img<-img*255
#this operation is necessary to obtain an RBG scale

在这一步,我不确定哪种方法是正确的。无论如何,我想得到这样的东西:

At this step I'm not sure which is the right way to go ahead. Anyway, I would like to obtain someting like this:

Count    RGB vector

200614   (255,255,255) 

4758     (253,253,218) 

4312     (250,250,229) 

1821     (235,237,242) 

1776     (212,214,226)

...

然后我可以计算每种颜色的百分比。
最后我会尝试将标签与每个RGB矢量相关联。

and then I can calculate the percentage of each color. Lastly I'll try to associate a label to each RGB vector.

任何人都可以帮助我?

推荐答案

使用将像素数组转换为#rrggbb值后,您可以轻松地使用计算颜色.raster (请注意,您不需要通过将值乘以255来缩放值)。可以通过 col2rgb 获得十六进制颜色字符串中的单个颜色分量。

You can easily count colors with table after converting the pixel array to #rrggbb values with as.raster (note that you don't need to scale the values by multiplying them by 255). Individual color components from hex color strings can be obtained by col2rgb.

library(jpeg)
img <- readJPEG("C:/Users/Pictures/img.jpg")

# Convert the 3D array to a matrix of #rrggbb values
img <- as.raster(img)

# Create a count table
tab <- table(img)

# Convert to a data.frame
tab <- data.frame(Color = names(tab), Count = as.integer(tab))

# Extract red/green/blue values
RGB <- t(col2rgb(tab$Color))
tab <- cbind(tab, RGB)

# Preview
head(tab)

这篇关于使用R的图像颜色组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆