2

I have an Android application that has user contributed images. There are a lot of users that submit black or really dark images to the backend application, that I want to filter out. Best solution would be to filter the images already at the phone and notify the user to make more light and retake the picture. Another solution if easier to achive would be to filter it at the backend written in python. I took a look into opencv, but I do not really have a clue where to start. Any hints where to get information to measure the brightness/color of an image in Android and/or in python ?

georam
  • 494
  • 2
  • 5
  • 14
  • possible duplicate of [What are some methods to analyze image brightness using Python?](http://stackoverflow.com/questions/3490727/what-are-some-methods-to-analyze-image-brightness-using-python) – Spacedman Dec 28 '11 at 18:34

4 Answers4

7

You can try using a histogram of the image as a first approximation.

An example using PIL that alerts you if there are more 'dark' pixels (first 128 values in a greyscale representation) than 'light' ones:

import Image

img = Image.open('m.jpg')  
gsimg = im.convert(mode='L')
hg = gsimg.histogram()
# count should be 256 most/all of the time (an index for each shade of grey)
count = len(hg)

if sum(hg[:count/2]) > sum(hg[count/2:]):
  print "the image is too dark"
Eduardo Ivanec
  • 11,668
  • 2
  • 39
  • 42
2

I'd probably look at calculating some simple statistics over the image. You really don't need opencv to do this properly. I'd take into account average brightness, and variance over the image.

Here is a simple formula to do average brightness:

Formula to determine brightness of RGB color

That coupled with a range of min/max could tell you if you have a mostly black image. Histogram might help here as well, but you might not need the full histogram. And this can all be calculated in Java pretty easily on the phone.

If you wanted to get really fancy you could do the stats calculation, and tell the user it's not bright enough. Then do a brightness adjustment to normalize the image for them. It's a very simple process:

http://en.wikipedia.org/wiki/Normalization_(image_processing)

Then run image stats on that, and see if it helped adjust the image enough for it to be allowed to upload it.

Community
  • 1
  • 1
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
0

In Python, you can use PIL for basic image analysis.

http://effbot.org/imagingbook/imagestat.htm

is the docs for the ImageStat module. Usage like:

from PIL import Image,ImageStat
im = Image.open("bride.jpg")
stat = ImageStat.Stat(im)

Now various bits of 'stat' will tell you about the image's red, green, and blue bands. For a dark image, you expect them to be low, so check the value of stat.mean and if they are below some cutoff declare the image to be dark. You'll have to make yourself some sample images and see what kind of values you get.

[Edit]

It might even be simpler to convert the image to grayscale with ImageOps.grayscale and then you only get one value back for the statistics.

Jonathan Root
  • 535
  • 2
  • 14
  • 31
Spacedman
  • 92,590
  • 12
  • 140
  • 224
0

What about converting the image into greyscale one, calculate average color of the greyscale image, and filter out images than go over some threshold?

ZelluX
  • 69,107
  • 19
  • 71
  • 104