Pages: [1]   Go Down

Author Topic: Gamma-correct image processing  (Read 12317 times)

knweiss

  • Jr. Member
  • **
  • Offline Offline
  • Posts: 71
Gamma-correct image processing
« on: August 30, 2008, 06:35:20 pm »

I would like to discuss monitor Gamma and its effect on image processing algorithms because there are some experts here in this forum.

After reading an interesting article about Gamma in the current issue of the German magazine "Digital Production" today I've also read the following two web pages referred in the article: Gamma error in picture scaling and Gamma Correction in Computer Graphics.

Basically, what they both are saying is that the image scaling algorithms used in most image processing software (e.g. Photoshop CS3, Gimp, etc) are wrong because they operate in the assumption that luminosity (0-255) is linear and 50% gray is 127 whereas in sRGB it actually is 186. You can actually measure this with the light meter of your DSLR: Set it to Av mode and compare the shutter speed of a pure white (255) image with the shutter speed of a pure 127 gray and a pure 186 gray image. You will see that the 127 image has 4x the shutter speed whereas the 186 image has a 2x shutter speed i.e. half the brightness (50% gray).

What this means: If you have an image with a black&white checkerboard pattern (0 and 255) and resize this image to 50% the resulting image should consist of 186-gray pixels (to have the same luminosity as the original). However, with most programs you get 127==0.5*(0+255) gray because they treat luminosity as a linear space whereas in sRGB it is a exponential with gamma 2.2 (see the links for elaborate explanations).

The human eye is more sensitive to changes in dark colors and by using the Gamma curve you get more bits to encode the darks (where it matters the most) than for the lights.

Now, did you know that PS CS3 will produce the correct (Gamma-corrected) image scaling result only in 32-bits/channel mode? I.e. both 8-bits/channel and the 16-bits/channel mode behave differently (not Gamma-corrected) and this can cause bad artifacts.

Take a look at the first link above and you'll find great examples of (constructed) photos which e.g. Photoshop or web browsers like Firefox&Safari can not scale correctly. They both create very different looking scaled images. See this link or try it for yourself in PS.

What's your take on this?

BTW: A notable exception apparently is Aperture which does Gamma-corrected scaling on export.
« Last Edit: August 30, 2008, 08:26:06 pm by knweiss »
Logged

rdonson

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 3263
Gamma-correct image processing
« Reply #1 on: August 30, 2008, 06:56:03 pm »

A very interesting phenomenon.  I have no earthly idea how to comment on it.  I don't know if its for real or a parlor trick.

I suspect we're going to wait for Eric to weigh in on this.
Logged
Regards,
Ron

Schewe

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 6229
    • http:www.schewephoto.com
Gamma-correct image processing
« Reply #2 on: August 30, 2008, 07:12:28 pm »

Quote
What's your take on this?
[a href=\"index.php?act=findpost&pid=218347\"][{POST_SNAPBACK}][/a]


Well, if you have high or low contrast scan lines in your image, you've got a lot more problems in the first place. For "normal" photographs, I doubt this is a real issue...
Logged

knweiss

  • Jr. Member
  • **
  • Offline Offline
  • Posts: 71
Gamma-correct image processing
« Reply #3 on: August 30, 2008, 07:24:45 pm »

Quote
Well, if you have high or low contrast scan lines in your image, you've got a lot more problems in the first place. For "normal" photographs, I doubt this is a real issue...
[{POST_SNAPBACK}][/a]
Jeff, thanks for answering. Did you notice that there are also "normal" photograph examples shown in the [a href=\"http://www.4p8.com/eric.brasseur/gamma.html#examples]"Examples" section[/url] of the first link? Also, the (and only the) 32-bits/channel mode in PS gets it right.
« Last Edit: August 30, 2008, 07:26:18 pm by knweiss »
Logged

madmanchan

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 2115
    • Web
Gamma-correct image processing
« Reply #4 on: August 30, 2008, 07:53:44 pm »

There's not a clear-cut answer to this, because the question of what's "right" or "wrong" largely depends on what type of image processing you want to do.

Yes, it is true that in 8-bit mode and 16-bit mode in Photoshop (and most rendered image processing software) you are typically operating on gamma-encoded images and therefore results will depend on your choice of working space.

I don't know where they got the number of 186 for sRGB corresponding to middle gray. I suppose it depends on what your definition of middle gray is. If we go by the CIE L*a*b* model, middle gray is L* = 50. On an 8-bit scale, this is about 118 for sRGB and Adobe RGB (gamma 2.2 for the latter, almost gamma 2.2 for the former) and 100 for ProPhoto RGB (gamma 1.8).

For certain types of operations, such as white balance, using an encoded space is definitely the wrong thing to do, particularly when the curve is not strictly a power curve. (In the case of white balance, doing it post-raw-conversion in PS is almost always the wrong thing to do, not because of gamma encoding, but because nearly all raw converters apply a highly non-linear tone mapping curve to the raw data.)

If you are trying to any type of physical simulations (such as colorimetric calculations, for the additions of color for HDR processing) then you definitely want linear data, not tone-mapped and not gamma encoded. This is partly why 32-bit mode is different in PS: it is generally assumed that if you have 32-bit-per-channel data then you're really trying to take linear data and do some tone mapping with it (e.g., HDR).

Other types of computations are much less clear cut, such as sharpening. Most sharpening algorithms are based on edge enhancement, and internally, typically blurs of different sizes are used to find the edges (yes, I know it's counterintuitive to think of blurs when discussing sharpening). But whether you perform this blur in linear space or gamma-encoded space is debatable; it has different effects on whether you end up emphasizing lighter halos vs. darker halos.

In general I would say that the reason why the answer isn't clear-cut is because many phenomena happen at the linear level (so you need linear data if you're trying to model that) but humans perceive them non-linearly (such as lightness, where a material with only 18% reflectance appears "middle gray"). So the question is whether you should tackle the problem at the physical level or the perceptual level.
Logged
Eric Chan

knweiss

  • Jr. Member
  • **
  • Offline Offline
  • Posts: 71
Gamma-correct image processing
« Reply #5 on: August 31, 2008, 05:07:03 am »

Quote
I don't know where they got the number of 186 for sRGB corresponding to middle gray. I suppose it depends on what your definition of middle gray is. If we go by the CIE L*a*b* model, middle gray is L* = 50. On an 8-bit scale, this is about 118 for sRGB and Adobe RGB (gamma 2.2 for the latter, almost gamma 2.2 for the former) and 100 for ProPhoto RGB (gamma 1.8).
According to the sRGB gamma curve 186 is the color where you have 50% of the brightness of white. At 128 you get 25% of the brightness of white even though your eyes maybe are not convinced because they perceive non-linear, too. So middle gray is not "50% of the brightness of white".

Now the idea is that if you scale a b&w checkerboard the resulting image should still retain the original brightness. I.e. if you scale it by 50% you have to interpolate 0% brightness (black) and 100% brightness (white) which is 50% brightness (186). If you take a look at their examples you'll notice that the Photoshop-scaled images really become (partially) darker than the original.
Logged

madmanchan

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 2115
    • Web
Gamma-correct image processing
« Reply #6 on: August 31, 2008, 09:45:42 am »

Quote
According to the sRGB gamma curve 186 is the color where you have 50% of the brightness of white.

This I don't understand at all.

What, exactly, is your definition of "brightness"?

And exactly what is the math used to derive the value 186?

And what exactly do you mean by "scaling the image"?  Do you mean using the white/black points of the Levels tool in PS?
Logged
Eric Chan

knweiss

  • Jr. Member
  • **
  • Offline Offline
  • Posts: 71
Gamma-correct image processing
« Reply #7 on: August 31, 2008, 11:17:39 am »

Quote
This I don't understand at all.

What, exactly, is your definition of "brightness"?

And exactly what is the math used to derive the value 186?

And what exactly do you mean by "scaling the image"?  Do you mean using the white/black points of the Levels tool in PS?
[{POST_SNAPBACK}][/a]
Eric, please check out the [a href=\"http://www.4p8.com/eric.brasseur/gamma.html#explanation]explanation section[/url] (of the first link in my initial post) where it is explained in detail with tables, example images and formulas much better than I can reproduce here in one or two sentences. I probably should have said "luminosity" instead of "brightness".

By "scaling the image" I mean "Image"->"Image Size..." in Photoshop.
Logged

glennchan

  • Newbie
  • *
  • Offline Offline
  • Posts: 10
Gamma-correct image processing
« Reply #8 on: August 31, 2008, 10:05:09 pm »

I'm on the fence about this one. But I'm leaning towards linear light processing not being necessary.  (By linear light processing, I mean converting the gamma corrected R'G'B' components into linear light, apply the operation, then converting back to gamma corrected.)

I have an article with some pictures here:
http://www.glennchan.info/articles/technic...linearLight.htm

It's worth looking at pictures scaled both ways.

If you do the scaling in a gamma corrected domain (e.g. sRGB in Photoshop / sRGB transfer function applied), then the resulting image will be "perceptually similar".  (My term.)  By that I mean that the text in the book example (see link above) is more legible.

If you do it in the linear light domain, then it's optically correct.
Things like the specular highlights on the bee are a little closer to what they should be.

In either case, IMO the difference is fairly subtle and not worth the effort.  There are much worse effects that happen... e.g.
http://colorcorrection.info/color-science/...raw-processors/
Doing saturation algorithms in the linear light domains can potentially be problematic.  Though they don't have to be.  I wrote my own saturation algorithm and it is a hybrid of calculations in the gamma-corrected and linear light domains (i.e. BOTH were necessary).
« Last Edit: August 31, 2008, 10:14:19 pm by glennchan »
Logged

glennchan

  • Newbie
  • *
  • Offline Offline
  • Posts: 10
Gamma-correct image processing
« Reply #9 on: August 31, 2008, 10:13:49 pm »

The sRGB transfer function is not really exponential.  It is composed of two segments, a linear segment and a power function segment.  It is not a straight power function, so that could be something to watch out for.

2- When doing white balance, it's better to shoot RAW and WB the image that way.  Usually the camera's RAW processing will introduce some non-linearities... theoretically it is possible to apply the opposite operations but only if you knew what the original operations were (which is hard when the camera manufacturers don't disclose em; and it would be different from camera to camera anyways).

2b- You can apply white balance by simply multiplying the R'G'B' channels by some number.  If the transfer function were simply a power function, this would work... but because the sRGB transfer function is a combination of two functions, this technically doesn't work and is cheating.  But it looks ok and nobody notices small color inaccuracies.

The real problem is that the camera's raw processing hoses the signal and makes white balance non-perfect.  If that problem were solved then I'd worry about this.

Hopefully this clarifies the math:
You might think that the transfer function of still image is this:
f(x) = x ^ 1/2.2

However, the sRGB standard calls for this:
http://books.google.ca/books?id=ra1lcAwgvq...num=3&ct=result

Mathematically, they are obviously two different things.  The sRGB transfer function has a linear segment, so that the slope near 0 doesn't go to infinity.

With f(x) = x ^ 1/2.2, you can scale the linear light components by multiply the gamma-corrected components by some constant.  You can't do this with sRGB.
« Last Edit: August 31, 2008, 10:20:47 pm by glennchan »
Logged

glennchan

  • Newbie
  • *
  • Offline Offline
  • Posts: 10
Gamma-correct image processing
« Reply #10 on: August 31, 2008, 10:22:14 pm »

As far as Photoshop goes, you can do linear light rescaling by scaling in 32-bit mode.  Photoshop applies the inverse of the sRGB transfer function when convert things into 32-bit mode.  BUT you are limited to bilinear rescaling.

The real benefit of that mode is that you can make optically-correct glow effects and compositing effects.  IMO.

2- For real world images, the benefits are a lot less.  A properly engineered camera will apply optical low pass filtering (Leica M8 is an exception; it omits the OLPF).  Essentially, they have blurry glass in front of the sensor.  Or you might have mediocre optics in front of your sensor, or the aperture is at the extremes.  So anyways, your pictures will have a slight blur to them and this will greatly reduce the difference between linear light and gamma corrected rescaling.  The difference in the rescaling methods is greatest when you have high contrast detail / when the details have a large numerical difference between them.
« Last Edit: August 31, 2008, 10:28:14 pm by glennchan »
Logged

knweiss

  • Jr. Member
  • **
  • Offline Offline
  • Posts: 71
Gamma-correct image processing
« Reply #11 on: September 01, 2008, 01:36:57 pm »

Quote
As far as Photoshop goes, you can do linear light rescaling by scaling in 32-bit mode. Photoshop applies the inverse of the sRGB transfer function when convert things into 32-bit mode.  BUT you are limited to bilinear rescaling.
Thanks for your postings! Yes, I already knew that scaling in 32-bit mode is fine (and even mentioned it in my initial posting) but so far I did not notice that the image scaling algorithm is affected. I normally use bicubic (but so far did not use 32-bit mode very often). Thanks for mentioning it!

My personal conclusion is that I consider the lack of gamma-correction in image scaling a real problem. I agree with the authors that Photoshop's scaling is wrong. In my opinion image scaling should not influence the brightness of an image. And I don't really want to agree with you that the effect is "subtle and not worth the effort". In the checkerboard example Photoshop's brightness is 31% off (128 vs 186). In terms of brightness it is even 50% off (128 is half as bright as 186). Thus, I would love to have the option of gamma-correct scaling in Photoshop.

Regarding your book+magnifying glass example: Your argument that "the typical image has text that's slightly easier to read" is IMHO invalid because the artifact (lower brightness) has just accidentally a positive effect to the picture but this doesn't make it right. IMHO this photo  even is a good real life example why gamma-correction is needed because the brightness change is not subtle either. I definitely prefer the gamma-corrected (linear) version.

The tragic point about all this is that the lack of gamma-correction does not only affect image scaling but many other image processing algorithms, too. Gamma correction in Computer Graphics e.g. also shows convincing anti-aliasing and black and white conversion examples.
Logged

glennchan

  • Newbie
  • *
  • Offline Offline
  • Posts: 10
Gamma-correct image processing
« Reply #12 on: September 03, 2008, 01:12:29 pm »

- One situation where you would see a big visible difference is with green text on a magenta background.  (Though I don't know how often that would happen.)  You see a dark band where the edges are.
The text anti-aliasing example wasn't very convincing to me... it looks very similar.
In regards to black and white conversions, one of the most powerful tools in the B&W photographer's arsenal is to do it wrong.    They use color filters to manipulate the colors.  It's about what looks right in the end, in my opinion.

- The examples in my article I believe are a little contrived... I went out and found situations where there would be a difference in the scaling.  Also, the reduction factor is very large... if you went with a milder rescaling like 0.5x, then you wouldn't see much of a difference at all since there is little high contrast detail.

- I don't think linear light rescaling is necessarily correct.  Some things look right in a gamma-corrected domain and other things look right in a linear light domain.  (Or some combination of both.)  Linear light processing isn't always correct.

-Lately I've become more of a big picture person, and in the big picture this difference doesn't really make a difference as to how your pictures look.  Whereas there are some other areas of image manipulation where linear light processing does make a noticeable difference, e.g. glows.
Logged
Pages: [1]   Go Up