Search code examples
phppostgresqlcolorsimagemagickphotos

Group photos by color


I have a pretty big number of photos and a RGB color map (let's say of about 100 colors). How can I group the pictures by color and obtain something like the following: http://labs.ideeinc.com/multicolr ?

My current idea is this: Using ImageMagick, do this for each photo:

  1. Resize it to a smaller size so that it can be processed faster.
  2. Quantize it without dithering using my chosen color map.
  3. Get the photo's histogram to obtain how many times each color appears.
  4. Store the colors in a database, but I haven't figured out what is the best way to do this for fast retrievals.

Do you know any better and more efficient way to do this? My language of choice is PHP since all the heavy processing will be done by ImageMagick, and the database is PostgreSQL. Thank you in advance!


Solution

  • I notice you already figured out how to get the most relevant colors from the image. Don't resize the images so much because the histogram may look different.

    The database may look something like that:

    image table:

    image_id | image_file
    

    color table:

    color_id | color_rgb
    

    image_color table:

    image_id | color_id | color_percent
    

    color_percent column will be used for grouping / where clauses

    Getting images:

    select
        image_id
        sum(color_percent)/count(color_percent) as relevance
    from
        image_color
    where
        color_id IN (175, 243) # the colors you want to involve in this search
        and color_percent > 10 # this will drop results with lower significance
    group by
        image_id
    order by
        relevance