tags:

views:

186

answers:

2

For a thumbnail-engine I would like to develop an algorithm that takes x random thumbnails (crop, no resize) from an image, analyzes them for contrast and chooses the one with the highest contrast. I'm working with PHP and Imagick but I would be glad for some general tips about how to compute contrast of imagery.

It seems that many things are easier than computing contrast, for example counting colors, computing luminosity,etc.

What are your experiences with the analysis of picture material?

+2  A: 

I'd do it that way (pseudocode):

L[256] = {0,0,0...}

loop over each pixel:
    luminance = avg(R,G,B)
    increment L[luminance] by 1

for i = 0 to 255:
    if L[i] < C: L[i] = 0       // C = threshold of your chose

find index of first and last non-zero value of L[]
contrast = last - first
mateusza
That would just give me the difference between the darkest and the lightest pixel, if I understand your idea correctly. Not very representative in most cases.
tharkun
http://en.wikipedia.org/wiki/Contrast_(vision)
mateusza
as far as I understand that wiki article it would be a standard deviation which helps. so thanks for the link, maybe you can adjust your pseudocode to compute a standard deviation or other variance metric.
tharkun
The check on L[i] < C will remove values that only occupy a few pixels.
ChrisF
+1  A: 

In looking for the image "with the highest contrast," you will need to be very careful in how you define contrast for the image. In the simplest way, contrast is the difference between the lowest intensity and the highest intensity in the image. That is not going to be very useful in your case.

I suggest you use a histogram approach to describe the contrast of a given image and then compare the properties of the histograms to determine the image with the highest contrast as you define it. You could use a variety of well known containers to represent the histogram in code, or construct a class to meet your specific needs. (I am not implying that you need to create a histogram in the form of a chart – just a statistical representation of the intensity values.) You could use the variance of each histogram directly as a measure of contrast, or use the standard deviation if that is easier to work with.

The key really lies in how you define the contrast of the image. In general, I would define a high contrast image as one with values present for all, or nearly all, the possible values. And I would further add that in this definition of a high contrast image, the intensity values of the image will tend to be distributed across the range of possible values in a uniform way.

Using this approach, a low contrast image would tend to have relatively few discrete intensity values and they would tend to be closely grouped together rather than uniformly distributed. (As a general rule, they will also tend to be grouped toward the center of the range.)

TMarshall