开发者

detect if an image is generally red, green, blue, or black

I'm processing small images that will either be red, green, blue, or black开发者_开发知识库 (think card ranks in a 4-color deck). What's a good, fast algorithm to determine which color the image is?

For an example set of inputs, see here, except the images can be scaled and such so they won't be as clear-cut.


Are these the exact images you are using? If so, is there a single pixel on all images that will be coloured? I was thinking where the suit is. Look at a pixel where the suit should be and that will tell you what "colour" the card is.

More general approaches might include analysing the whole image (slow but effective), scaling the image down and analysing every pixel (more feasible if you end up with a small image but scaling may affect colours), or sampling at random n pixels (or rows/columns of pixels), maybe something with masking if the shades of colours are known ahead of time, but do you need a general approach?

Oh - another idea: Do you control the source of the input images? Maybe you could override some image metadata tage with a number to indicate the "colour". It's sort of cheating since there's no real image processing involved, and it's vulnerable to the tags getting stripped/modified, but it's probably the fastest method.


This works sufficiently well for one pixel:

def get_ishness(r,g,b):
    h,s,v = rgb_to_hsv(r,g,b) #h from 0-360, s and v from 0-100
    if v < 50: return 'black'
    if s < 15: return None
    if h < 10: return 'red'
    if 80 < h < 100: return 'green'
    if 210 < h < 230: return 'blue'
    return None

For the whole image, I sum up the pixels that are red,green,blue, and black, and return the color of which there are the most pixels.


This post seems to have settled but if you don't mind my 2 cents...

As I take it, you need a general approach and you don't know what the images are in advance. While the answers already posted should be good enough for quick coding with maybe some adjustments, if you still run into problems with them I suggest you use a self-organizing map: http://davis.wpi.edu/~matt/courses/soms/ and http://www.ai-junkie.com/ann/som/som1.html

It might need some (unsupervised) training time to get a decent map/recognizer (hey, it's a neural network after all), but I've encountered a problem a bit like yours and they worked well for me (easy to adapt, flexible with different lighting, etc.).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜