【问题标题】:Extracting top 2-3 hex colors from images in R从R中的图像中提取前2-3个十六进制颜色
【发布时间】:2019-06-22 01:20:54
【问题描述】:

我想知道是否可以从包含团队运动徽标的图像文件中提取主要的十六进制颜色。我有以下标志向量:

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 中的向量中的每个图像刮取这些十六进制值(因此我不必手动加载每个图像并单击以查找每个十六进制值)。

谢谢!

【问题讨论】:

    标签: r image colors


    【解决方案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 
    

    【讨论】:

      【解决方案2】:

      使用成像器包的另一种选择...

      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)
        })
      
      

      【讨论】:

        猜你喜欢
        • 2013-06-07
        • 1970-01-01
        • 2017-12-05
        • 2020-03-05
        • 2016-02-09
        • 2011-09-16
        • 2020-05-14
        • 2021-04-06
        • 2014-07-03
        相关资源
        最近更新 更多