5

I have a script that extracts the mostly commonly occurring colors from an image. I want to store this data in MySQL then be able to pull out images based on the clicking of a color. For example if the user clicks on a red color I want to pull out other images that have a high count for "red" colors. I'm not sure how to search within a range of color values or what values to actually store in the database. Ideally I'd like to present the user with a gradient band and have them click on it to find images that are close in color to what they clicked.

Any help, pointers, or keywords I can used to Google for more information would be userful.

Jereme
  • 51
  • 1
  • 2
  • possible duplicate with this [Database design to store image color pattern in MySQL for searching Image by color][1] [1]: http://stackoverflow.com/questions/19024769/database-design-to-store-image-color-pattern-in-mysql-for-searching-image-by-col – Sendy Oct 05 '13 at 21:01

3 Answers3

4

Take a look at my answer to this question. Basically you store the hex values of each component separately, then you can search for them with a simple mySQL query. I suppose yo could populate a table by iterating every color in an image and putting in the top x colors to the table.

select imageName from imageColors where ( ABS(red - $redHex) + ABS(blue-$blueHex) + ABS(green - $greenHex) < $threshold)

$threshold is the maximum distance between the colors.

Community
  • 1
  • 1
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
  • when you say $redHex, $blueHex, $greenHex, do you mean R G B values, as opposed to hex values, as you can't subtract a hexadecimal value - can you? – Mazatec Oct 24 '12 at 18:53
  • hexadecimal is just base 16, So yes you can add subtract multiply integrate in base16. It would be much easier to store as an INT in the database then convert to hex on the other side. – Byron Whitlock Oct 25 '12 at 23:43
1

My first thought is that your best bet is to use 6-digit hex, and store each component (red, green, blue) in a separate field.

Looking for something "red"? Select those records with a high red count, and relatively low green and blue, and then be ready to tweak your cutoffs.

Chris Tonkinson
  • 13,823
  • 14
  • 58
  • 90
0

There's a class on PHPclasses.org that promises to find the "dominant colour." Haven't used it myself so I can't say anything about its quality but it has been reviewed by PHPclasses, which usually makes it worth checking out.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • The op said "I have a script that extracts the mostly commonly occurring colors from an image", you have just given him another script that extracts colors from an image. He wants to know how to search for the extracted colors. – TheCarver Nov 15 '12 at 21:32