3

I took the average of each color by this method and wrote the average of red, green and blue to database.

Here are the images sorted by "-blue". As you can see the 5th image has the most blue. Am I doing something wrong, or is it not possible to get average color from the histogram?

This is the handler where I create the histogram:

class ImageSave(webapp.RequestHandler):
    def post(self):
        homepage = HomePage()
        original_image = self.request.get("img")
        url = self.request.get("url")
        firm_name = self.request.get("firm_name")

        original_image = db.Blob(original_image)
        thumbnail = images.resize(original_image, 250, 250)
        img = images.Image(thumbnail)
        hist = img.histogram()
        rgb_weighed_average = hist_weighed_average(hist)

        #update database
        homepage.original_image = original_image            
        homepage.thumbnail = thumbnail
        homepage.firm_name = firm_name
        homepage.url = url

        homepage.red = rgb_weighed_average[0]
        homepage.green = rgb_weighed_average[1]
        homepage.blue = rgb_weighed_average[2]

        homepage.put()

        self.redirect("/imageupload")

Thanks!

Community
  • 1
  • 1
Zeynel
  • 13,145
  • 31
  • 100
  • 145
  • 1
    You need a color space with brightness and saturation separated ([HSL/HSV](http://en.wikipedia.org/wiki/HSL_and_HSV)). Then find the dominating values for the hue component of each pixel. – Paolo Moretti Sep 27 '11 at 19:19
  • Paolo Moretti: I used Python's colorsys to get the hue: `hue = colorsys.rgb_to_hls(r,g,b)[0]` and sorted by hue but still images of similar colors do not group together. – Zeynel Sep 27 '11 at 22:50
  • This doesn't really have anything to do with App Engine, does it? Also, you're asking about histograms and image manipulation, but haven't included any of the image manipulation code. – Nick Johnson Sep 28 '11 at 00:51
  • @ Nick Johnson: I removed the GAE tag. The code includes how I calculate the histogram and the average, but unfortunately when I sort by rgb or by hue images with similar colors do not group together. – Zeynel Sep 28 '11 at 11:15

1 Answers1

5

Actually, the fifth image doesn't have the most blue. Note that white is (255, 255, 255) as rgb, so an image that is completely white has just as much blue as an image that is completely blue. A darker blue has a smaller blue component than white.

omz
  • 53,243
  • 5
  • 129
  • 141
  • omz: thanks! So is there a way to organize similar colors together from whitest to bluest or darkest? Should I use another method? – Zeynel Sep 27 '11 at 18:49
  • Perhaps you could calculate the hue of the average color (search for rgb to hsv/hsl conversion) and sort based on that. – omz Sep 27 '11 at 19:26
  • omz: sorting by hue does not work either. An image which looks visually the most blueish is sorted next to the most white image; exactly like you say in your comment. Is there a way to find the visually dominant color of an image? Thanks again. – Zeynel Sep 27 '11 at 22:54
  • I don't think that's possible with the histogram alone. – omz Sep 27 '11 at 23:09
  • omz: yes, it looks like it. I put various sort orders here: `http://ting-1.appspot.com/displayimage` and as you can see, colors do not group well. Any suggestions? – Zeynel Sep 27 '11 at 23:39
  • Maybe sorting by hue _and_ brightness could produce slightly better results but overall a histogram doesn't contain enough information to get the "dominant" color of an image. You would need to know which actual colors the image contains; the histogram only tells you about luminance values (separated by rgb channels). An image with one red pixel and one blue pixel has the same histogram as an image with one magenta and one black... – omz Sep 27 '11 at 23:59