Image Magick / Detect colors contained on a image

折月煮酒 提交于 2019-12-05 05:16:19

ImageMagick's "convert" command can generate a histogram.

$ 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)
   .
   .
   .

Depending on your language of choice and how you want the colors represented, you could go lots of directions from here. Here's a quick Ruby example though:

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] .....
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!