Image Magick/Detect图像中包含的颜色

Ben*_*Ben 7 pixel colors imagemagick image-processing

什么是简单的过程会给出图像中包含的颜色数组?

小智 10

ImageMagick的"转换"命令可以生成直方图.

$ convert image.png -define histogram:unique-colors=true -format %c histogram:info:-

19557: (  0,  0,  0) #000000 gray(0,0,0)
 1727: (  1,  1,  1) #010101 gray(1,1,1)
 2868: (  2,  2,  2) #020202 gray(2,2,2)
 2066: (  3,  3,  3) #030303 gray(3,3,3)
 1525: (  4,  4,  4) #040404 gray(4,4,4)
   .
   .
   .
Run Code Online (Sandbox Code Playgroud)

根据您选择的语言以及您想要的颜色表示方式,您可以从这里获得很多指示.这是一个快速的Ruby示例:

out = `convert /tmp/lbp_desert1.png \
               -define histogram:unique-colors=true \
               -format %c histogram:info:- \
       | sed -e 's/.*: (//' \
       | sed -e 's/).*//'`

out.split("\n")
    .map{ |row| row.split(",").map(&:to_i) }


# => [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4] .....
Run Code Online (Sandbox Code Playgroud)