Gamma Correction

A "new fashioned" televisor, using an Arduino to drive the motor and display.

Moderators: Dave Moll, Andrew Davie, Steve Anderson

Gamma Correction

Postby Andrew Davie » Sat Apr 01, 2017 11:55 am

Any LED responds (almost) linearly to pulse-width modulation (PWM), putting out twice as much light when it’s on for twice as long, but the human eye is dramatically nonlinear. Consequently it's necessary for mechanical TV to correct the video signal sent to the LED array to cater for this non-linearity in the eye. It's called "gamma correction" and here's my first attempt at doing that. It's all a bit dark because it's quite bright today and the picture isn't as easy to see as at night time when I usually work. I cheated a bit and processed the video to increase the brightness a bit because of the daytime issues. But still, I think there's more visible detail in the picture and the gamma correction is doing its job.


youtu.be/ripFpSgmT4c

I only had to add a small bit of code - a one-liner, actually - that's my first attempt at gamma correction. This page gives a quick and dirty 256 byte table and I've taken that on faith and passed my brightness through that conversion to give my first gamma-corrected attempt.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Gamma Correction

Postby Steve Anderson » Sat Apr 01, 2017 2:06 pm

Actually Gamma encoding/decoding/'correction' has nothing to with our eyes. It's historical and was put to good use in masking noise in the darker scenes in a picture.

The camera tubes prior to CCDs and the like have a non-linear response to light levels. This generally has a law of 0.5, the square root of the illumination. Luckily the opposite happens in a CRT, well approximately, an average of around 2.2, slightly more than the square. So across the transmission chain it all evens out - approximately. This results in the signal having stretched low-level signals and compressed highlights.

Noise in a video signal, or even in a still photo, is more noticeable in the darker areas rather than in the highlights. So this Gamma 'correction' masked a significant portion of the apparent noise prevalent in fringe-reception areas.

LEDs are generally linear as a function of the current whether modulated by PWM or a linear current source. So what needs to be done is the recreate the '2' law of a CRT. As you have done, this is easily implemented in a look-up table in a micro. Other ways are using the Vbe voltage of a transistor which is also close to a logarithm over many decades. Alternatively a 'breakpoint' non-linear feedback around an op-amp will approximate the law required depending on the number of 'breaks' it has. This is how Klaas's LED driver works, the two diodes and associated resistors providing two brealpoints, i.e. three different slopes of current-verses-voltage. But this arrangement will not work with PWM.

Steve A.
User avatar
Steve Anderson
"Fester! Don't do that to 'Thing'"
 
Posts: 5385
Joined: Fri Mar 30, 2007 10:54 pm
Location: Bangkok, Thailand

Re: Gamma Correction

Postby Klaas Robers » Sat Apr 01, 2017 9:53 pm

Andrew, Gamma correction in TV is a quadratic curve, a parabola. The table that you used isn't. And more, it is wise to have the first part (all the zeros) linear. We used for that a practical factor of 8, so a parabola where the first beginning is linear with a steepness of 1/8. At the end the steepness is 2. You will see, it makes your images less dark than they are now. The table ends with 249, 251, 253, 255 and starts with 4 zeros, 8 ones, 8 twoos.......

I calculated the whole table for you in Excell, but don't know how I can get it to you.......
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: Gamma Correction

Postby Andrew Davie » Sat Apr 01, 2017 10:04 pm

Klaas Robers wrote:Andrew, Gamma correction in TV is a quadratic curve, a parabola. The table that you used isn't. And more, it is wise to have the first part (all the zeros) linear. We used for that a practical factor of 8, so a parabola where the first beginning is linear with a steepness of 1/8. At the end the steepness is 2. You will see, it makes your images less dark than they are now. The table ends with 249, 251, 253, 255 and starts with 4 zeros, 8 ones, 8 twoos.......

I calculated the whole table for you in Excell, but don't know how I can get it to you.......



Thanks Klaas. You could email to me or post as an attachment here, zip it up and post as attachment, or share it via Dropbox....?
I expected the table to be correct as the site I got it from was dealing with exactly this problem (gamma correcting LEDs). I am surprised it's wrong but glad you caught that. yes, it's pretty dark at the moment.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Gamma Correction

Postby Andrew Davie » Sat Apr 01, 2017 11:48 pm

Here's the new version using Klaas's correct gamma table - thanks for that, Klaas!


youtu.be/oXBlzEInD9c

For anyone wanting the data, here 'tis...

Code: Select all
const uint8_t PROGMEM gamma8[] = {
    0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,
    2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,
    5,  5,  6,  6,  6,  6,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10,
   10, 10, 11, 11, 12, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17,
   17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, 25,
   26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 36,
   37, 38, 39, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 47, 48, 49,
   50, 51, 52, 53, 54, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
   65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 78, 79, 80, 81,
   82, 83, 84, 85, 87, 88, 89, 90, 91, 92, 94, 95, 96, 97, 99,100,
  101,102,104,105,106,107,109,110,111,113,114,115,117,118,119,121,
  122,123,125,126,128,129,130,132,133,135,136,138,139,141,142,144,
  145,147,148,150,151,153,154,156,157,159,160,162,164,165,167,168,
  170,172,173,175,177,178,180,182,183,185,187,188,190,192,194,195,
  197,199,201,202,204,206,208,209,211,213,215,217,219,220,222,224,
  226,228,230,232,234,235,237,239,241,243,245,247,249,251,253,255
  };
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Gamma Correction

Postby Klaas Robers » Sun Apr 02, 2017 7:17 am

This looks quite natural Andrew. What is it? Your wedding film?
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: Gamma Correction

Postby Andrew Davie » Thu Apr 06, 2017 10:13 pm

Klaas Robers wrote:This looks quite natural Andrew. What is it? Your wedding film?


An episode of Doctor Who. I was much more terrified than this at my wedding.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Gamma Correction

Postby Andrew Davie » Mon Apr 24, 2017 2:24 am

I thought I'd post a better video of the gamma being toggled on/off, now that I have a stable picture... the bright/washed-out bits are with gamma correction OFF, and the good-looking bits are with it ON. I was manually toggling this via the touchpanel UI.


youtu.be/NuNxdp-c1Xg
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: Gamma Correction

Postby Klaas Robers » Mon Apr 24, 2017 3:09 am

I see that the black sync bar changes its brightness too, when you toggle the gamma. That should not occur. Black should stay black. Something goes / has gone wrong in your DC restoration / clamping.

But over all it behaves as I expected. This is the visible effect of the correction of the gamma.
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: Gamma Correction

Postby Andrew Davie » Mon Apr 24, 2017 10:27 am

Klaas Robers wrote:I see that the black sync bar changes its brightness too, when you toggle the gamma. That should not occur. Black should stay black. Something goes / has gone wrong in your DC restoration / clamping.

But over all it behaves as I expected. This is the visible effect of the correction of the gamma.



I guess I've been a bit lazy in the conversion process. I cut off the synch, straddle the remaining signal evenly around "halfway', apply maximum amplification without clipping. But what I don't do is DC-offset so that black is 0. I'll redo the conversions including that last step, and then the synch bars will be true black. Thanks for pointing it out :)
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia


Return to Andrew Davie's Arduino Televisor

Who is online

Users browsing this forum: No registered users and 4 guests