2

I don't know what the best technology to use here is, I know PHP can do vaguely similar things but point me in the right direction if I'm wrong.

I'm building an online store and I'd like an easy (automated) way to categorise the colours of each item for sale.

I've seen numerous posts on Stack which are related to this, here are some good discussions for those interested:

Programmatically determine human readable colours

Get Image Colour

Detect overall average colour of a picture

These are all well and good. However, my issue is a little different. The images in question are all on different coloured backgrounds, and these affect the "average colour" of the image. I've tried resizing my images down to 1px to get a colour average, but this doesn't quite work.

Pants

Keys

Tie

As you can see, for image #1 the average colour is going to be a lot whiter than the product colour; for #2 and #3 it's going to be a lot more brown.

Can anyone think of any methods I could use to get the right average colour, in an automated way, with PHP, Ruby, Python, or anything similar? My idea was to take a section from the middle of each photo (which is usually where the product in question is) and take the average of that. For instance, get a 30px x 30px square in the centre of the image and process that.

This won't be absolutely perfect though, and I'm completely new to this sort of programming - is there any better way to determine foreground colour?

Community
  • 1
  • 1
JVG
  • 20,198
  • 47
  • 132
  • 210
  • 1
    PHP has something called `imagecolorat`, which you can find [here](http://www.php.net/manual/en/function.imagecolorat.php) – Daniel May 18 '13 at 06:05
  • @Daniel Yep, that's what I was referring to when I said my idea was to take a square section from the middle of the image and analyse that. Are you agreeing that this is the best possible way to get a close-enough idea of the image colour? – JVG May 18 '13 at 06:49
  • check 99designs repo on github , they have script forthat. – mpm May 18 '13 at 09:40
  • @Jascination - I would think to set up an area where you would scan your image and add the results into an array. I can't say it is THE method since I have never worked with it, but it is the way I would go. You will need to take a few things into account when refining the category of course. – Daniel May 18 '13 at 15:11

2 Answers2

1

I'd suggest you explode the image, giving weight to the center of the image.

convert image_source.jpg -implode -32 image_destination.jpg

Then calculate the average color (by scaling to 1x1) or pick an average from a centered box.

If you need more precision, you'll need a computer vision algorithm, to segregate the foreground from the background; you can have a look at OpenCV

Patrice Levesque
  • 2,079
  • 17
  • 16
  • Sorry mate I'm not too well-versed with PHP and the docs aren't really helping - can you elaborate on what `-implode` and `-32` are doing? – JVG May 19 '13 at 06:28
  • This is a way to give importance to the center pixels; a similar effect to a fish-eyed lens. See the ImageMagick example: http://www.imagemagick.org/Usage/warping/#explode – Patrice Levesque May 19 '13 at 06:49
0

It's quite a hard task what your up to.

My suggestion is that you maybe use quite a little more input One picture only the background (without the object) and one with the object. Now if you threshold the subtraction you can get the object pixels. (I meen just take those who change between the two pictures) Using these Pixels you could take the histogramm and select the most common ones.

(http://php.net/manual/en/imagick.getimagehistogram.php)

Daniel
  • 104
  • 4