[sane-devel] gamma correction procedure

Christopher Marshall christopherlmarshall at yahoo.com
Sun Feb 8 22:53:48 GMT 2004


All:

I think I've basically answered my own question,"How should you attempt to deduce a correct gamma
correction
value for your scanner with sane?"

The procedure is this:
 - open a graphics program like the gimp and make some blocks of solid color (gray scale)
 - I suggest hitting the following (R,G,B) values
   (64,64,64)
   (128,128,128)
   (192,192,192)
- print the page
- scan it 
- open the scan with a photoshop-like program like the Gimp
- measure the values from the corresponding blocks in the image.

Write down your input/output table like this
input -> output
64 -> 30
128 -> 100
192 -> 170

I just made these values up, BTW.

Now, the effect of gamma distortion for monitors is usually like this:

y = x ^ 2.5

(I mean x to the power of 2.5).  In this case, the gamma coefficient is 2.5.  This assumes your
values are between 0 and 1, by the way, so we need to divide the table above by 255.

Now, a "gamma correction of 2.5" is a function that corrects for the distortion formula above. 
Like so

y = x ^ (1/2.5)

Anyway, we want to estimate the parameter k in the distortion formula y = x ^ k.  If we measure
the distortion at three points as I suggested, then we can apply least squares to the observation
equations:

ln(y1) = k * ln (x1)
ln(y2) = k * ln (x2)
ln(y3) = k * ln (x3)

to estimate the parameter k.

Since there is only one parameter to estimate, this results in the following estimate:

k = (ln(y1)*ln(y1) + ln(y2)*ln(y2) + ln(y3)*ln(y3))  /  (ln(x1*ln(x1) + ln(x2)*ln(x2) +
ln(x3)*ln(x3))

In other words, the ratio of the sum of the squares (of the y and x logs).  Don't forget to divide
your observations by 255 so they are between 0 and 1 before you do this.

Don't choose use a value in this formula that is too close to the 0 end of the 0,255 range or you
will pull your k off.  Why?  Um, as I try to explain this I realize I may be wrong.  It may
actually be better to try to cover the low end specifically and ignore the high end.  I'll
experiment with this and report back.

What I was going to suggest was that a given change in k will have a larger effect on the low end
values than it does on the high end, so deviations at the low end are more sensitive to changes in
k than deviations in the midtones and highlights.

I've illustrated the procedure for the value channel gamma correction here.  You could use this
same procedure for the red, green, and blue channels separately to derive individual R, G, and B
gamma factors.

I believe the number you would want to feed to your sane backend would be k and not 1/k, since the
backend is doing "gamma correction" which is the inverse of "gamma distortion".  In other words,
you are measuring a gamma distortion of k, which can be corrected with a gamma distortion of 1/k,
or a gamma correction of k.

If anyone want me to spell this out in more detail, don't hesitate to ask, as I am tickled pink
that I finally got to the bottom of this.  I've wondered what gamma correction was for *years*.

Chris Marshall


__________________________________
Do you Yahoo!?
Yahoo! Finance: Get your refund fast by filing online.
http://taxes.yahoo.com/filing.html




More information about the sane-devel mailing list