Log in

View Full Version : Plugins - working with YUV


Si
2nd September 2002, 19:07
I'm having a few :rolleyes: problems understanding how things work in YUV colourspace and I wondered if I can pick the brains of the experts :)

I have started on converting my Border Control filter from VirtualDub and immediatly hit upon setting Y, U and V to zero does not give black as I expected but instead gives a grey (when viewed in VirtualDub).

I thought about this and changed U and V to be 128 and I seem to get black.

I then tried to convert my code that fades the borders of an image down to black and started getting in a right mess. Should I reduce Y only or subtract 128 from U and V and then reduce them as well?

I've looked in the source code of Levels.cpp and just become more confused looking at Levels class and the Tweak class.

Any hints/tips/thoughts out there?

regards
Simon

dividee
2nd September 2002, 19:37
In fact Y=16 and U=V=128 is already black. Any value of Y below 16 will also gives black, of course. For fading out a color image to black, I think you have to reduce saturation as well, because as you noticed, if U and V aren't 128, you won't get black.
Concentrate on the C code of the Donald's Tweak plugin. There are some optimizations tricks but it's still easy to follow.
For a fade to black you want to decrease contrast and saturation:
contrast: (Y-16)*scale + 16
saturation: (U-128)*scale + 128 (same for V)
where scale is between 1.0 and 0.0
Hope it helps

Si
2nd September 2002, 23:06
Originally posted by dividee
There are some optimizations tricks but it's still easy to follow

Easy for the person who writes optimised YUV code :D

Thanks for the info on reducing saturation at the same time as luma.

Am I right in thinking that Y shoud be in the range 16 to 235 and U and V should be 16 to 240?

regards
Simon

Guest
3rd September 2002, 02:55
Simon,

Everybody gets baffled at first when going at YUV. :)

The key for me is the equations I posted for you earlier. Make your adjustment with an RGB example. Then run the colors through the equations and you see how things change for YUV. The equations will also answer your question about ranges. For example, put RGB 255/255/255 through the equations and you'll see the maximum Y you can get. Just look at each equation and ask, what is the largest number this equation can generate? (Yes, 235 for Y and 240 for U&V.) Same thing for the minimums.

BTW, the clipping in Tweak is theoretically wrong, but it doesn't seem to create any problems. :)

Si
3rd September 2002, 15:15
@neuron2
Hi Don - I don't seem to see the posting with the equations :confused: but I'm assuming they give the same results as these from from the MSDN Library which it says are from Rec601:-

Given:

Kr = 0.299
Kb = 0.114
L = Kr * R + Kb * B + (1 – Kr – Kb) * G

Then:

Y = round(219 * L / 255) + 16
U = round(224 * 0.5 * (B - L) / ((1 - Kb) * 255)) + 128
V = round(224 * 0.5 * (R - L) / ((1 - Kr) * 255)) + 128

And just as a matter of interest - what's wrong with your clipping?
regards
Simon

Guest
3rd September 2002, 17:45
Look here. It was your thread!

http://forum.doom9.org/showthread.php?s=&threadid=31623

Should clip to 16-235 (eg) for Y, but I do 0-255.

Si
3rd September 2002, 18:18
Time for me to get some spectacles :o :o :o

Simon the Idiot