开发者

Image color recognition, the general color

开发者 https://www.devze.com 2023-02-08 14:52 出处:网络
I want to get the most prominent c开发者_如何学JAVAolor of an image, and the language can be in either python or ruby.

I want to get the most prominent c开发者_如何学JAVAolor of an image, and the language can be in either python or ruby.

Is this easily done?


I don't know if this is what you mean, but maybe it will be helpful:

require 'rubygems'
require 'RMagick'
include Magick

image = Image.read("stack.png")[0]
hash = image.color_histogram
color, number = hash.max{|a,b| a[1] <=> b[1]}
puts color.to_color

This worked like a charm for very simple image (only 5 colors), but should work for more complex images too (I did not tested that; returned hash will be quite big in that case, so you might want to use quantize on your image before using color_histogram).

Some resources :

color_histogram quantize

I hope this was useful to you. :)


OK. Let me introduce the library for Ruby.

Using Camellia, http://camellia.sourceforge.net/examples.html, you can label the area with the most prominent color.


Not sure if this is what you mean, but the Python PIL has im.histogram() and im.getcolors() functions. http://effbot.org/imagingbook/image.htm

0

精彩评论

暂无评论...
验证码 换一张
取 消