我想知道是否有可能从包含团队运动标志的图像文件中提取出主要的十六进制颜色。我有以下徽标矢量:
dput(team.logos[1:5))
c("https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/399.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/2066.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/42.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/311.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/160.png")
使用以下网站(https://html-color-codes.info/colors-from-image/)-我可以看到第一张图片(UAlbany)中的十六进制颜色值为黄色的#FEBE10
和紫色的#3F1E6B
以及白色。
我的问题是-有没有办法在R中的向量中为每个图像刮取这些十六进制值(因此,我不必手动加载每个图像并单击以查找每个十六进制值)。
谢谢!
答案 0 :(得分:1)
尝试一下。 png库允许一个加载RGB文件,然后只需将三个通道转换为十六进制代码即可。
我确认代码对于第一张图片是正确的,其余部分祝您好运。
logos<-c("https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/399.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/2066.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/42.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/311.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/160.png")
plot(NA, xlim = c(0, 2), ylim = c(0, 5), type = "n", xaxt = "n", yaxt = "n", xlab = "", ylab = "")
library(png)
for (filen in seq_along(logos)) {
#download and read file
#this will overwrite the file each time,
#create a list if you would like to save the files for the future.
download.file(logos[filen], "file1.png")
image1<-readPNG("file1.png")
#plot if desired
#plot(NA, xlim = c(0, 2), ylim = c(0, 5), type = "n", xaxt = "n", yaxt = "n", xlab = "", ylab = "")
rasterImage(image1, 0, filen-1, 1, filen)
#convert the rgb channels to Hex
outR<-as.hexmode(as.integer(image1[,,1]*255))
outG<-as.hexmode(as.integer(image1[,,2]*255))
outB<-as.hexmode(as.integer(image1[,,3]*255))
#paste into to hex value
hex<-paste0(outR, outG, outB)
#remove the white and black
hex<-hex[hex != "ffffff" & hex != "000000"]
#print top 5 colors
print(head(sort(table(hex), decreasing = TRUE)))
}
这是示例输出,十六进制颜色以及具有该颜色的像素数。
print(head(sort(table(hex), decreasing = TRUE)))
#hex
#c3c4c6 00275d 00265c c2c3c5 001e57 00255c
#67929 39781 838 744 649 633
答案 1 :(得分:1)
使用成像仪包装的另一种选择...
require('imager')
require('data.table')
team.logos <- c("https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/399.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/2066.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/42.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/311.png",
"https://a.espncdn.com/combiner/i?img=/i/teamlogos/ncaa/500/160.png")
#this function takes an image in imager's cimg format and
#returns the hex colour codes for any colours covering more than
#a threshold proportion of pixels (default is set to 0.05)
getHexPrimaries <- function(img, pcnt.threshold = 0.05){
#convert cimg to workable format
channel.labels <- c('R','G','B','A')[1:dim(img)[4]]
img <- as.data.table(as.data.frame(img))
img[,channel := factor(cc ,labels=channel.labels)]
img <- dcast(img, x+y ~ channel, value.var = "value")
#sort by unique rgb combinations and identify the primary colours
colours.sorted <- img[, .N, by=list(R,G,B)][order(-N)]
colours.sorted[ , primary := N/sum(N) > pcnt.threshold]
#convert to hex
hex.primaries <-
apply(colours.sorted[primary==TRUE], 1, function(row){
hex <- rgb(row[1], row[2], row[3], maxColorValue=1)
hex
})
hex.primaries
}
hex.list <- lapply(team.logos, function(logo.url) {
download.file(logo.url,'temp.png', mode = 'wb')
img <- load.image('temp.png')
getHexPrimaries(img)
})