File: studyyuv.txt
Name: learn more about the YUV color model.
Author: zyl910
Version: V1.1
Updata: 2006-5-28
Recently, I suddenly became interested in graphics. I found many notes on learning graphics many years ago. So I sorted them out.
I. Basics
The conversion from RGB to YUV is as follows:
[Y] = [0.299 0.587 0.114] [R]
[U] = [-0.148-0.289 0.437] * [g]
[V] = [0.615-0.515-0.100] [B]
The conversion from YUV to RGB is as follows:
[R] = [1 0 1.140] [Y]
[G] = [1-0.395-0.581] * [u]
[B] = [1 2.032 0] [v]
2. How are the transformation coefficients derived?
At first, I wanted to find a fast algorithm for converting RGB to YUV, so I began to study its coefficients.
2.1 YUV to RGB
YUV to RGB is the first breakthrough. Note that there are two zeros in the conversion matrix, so you can remove one multiplication:
B = Y + 2.032 * u
R = Y + 1.140 * V
Use the fact that "Y = 0.299 * r + 0.587 * g + 0.114 * B" to deduce G:
G = (Y-0.299 * r-0.114 * B)/0.587
Computation formulas of B and R are substituted into and simplified:
G = [Y-0.299 * (Y + 1.140 * V)-0.114 * (Y + 2.032 * u)]/0.587
= [Y-(0.299 * Y + 0.299*1.140 * V)-(0.114 * Y + 0.114*2.032 * u)]/0.587
= [(Y-0.299 * Y-0.114 * Y)-0.299*1.140 * V-0.114*2.032 * u]/0.587
= [(1-0.299-0.114) * Y-0.299*1.140 * V-0.114*2.032 * u]/0.587
= (0.587 * Y-0.299*1.140 * V-0.114*2.032 * u)/0.587
= Y + (-0.299*1.140 * V-0.114*2.032 * u)/0.587
= Y-(0.299*1.140/0.587) * V-(0.114*2.032/0.587) * u
= Y-(0.114*2.032/0.587) * U-(0.299*1.140/0.587) * V
= Y-0.394630 * U-0.580681 * V
It is exactly the same as that of YUV to RGB.
Complete Conversion formula:
B = Y + 2.032 * u
R = Y + 1.140 * V
G = Y-(0.114*2.032/0.587) * U-(0.299*1.140/0.587) * V
2.2 Convert RGB to YUV
From the analysis of YUV to RGB, we found two key coefficients: the U-related 2.032 and the V-related 1.140. So let's take them to the coefficients of the conversion matrix from RGB to YUV:
U: 2.032*[-0.148-0.289 0.437] = [-0.300736-0.587248 0.887984]
V: 1.140 * [0.615-0.515-0.100] = [0.701100-0.587100-0.114000]
Observe these coefficients and find two characteristics:
1. Those negative coefficients are very similar to the "color to gray" coefficients (0.299, 0.587, 0.114.
2. The positive coefficient is equal to the absolute value of the sum of two negative coefficients.
So we can regard the conversion matrix of U and V as follows:
U: (1/2.032) * [-0.299-0.587 1-0.114]
V: (1/1.140) * [1-0.299-0.587-0.114]
Complete Conversion formula:
[Y] = [0.299 0.587 0.114] [R]
[U] = [(-0.299)/2.032 (-0.587)/2.032 (1-0.114)/2.032] * [g]
[V] = [(1-0.299)/1.140 (-0.587)/1.140 (-0.114)/1.140] [B]
It is very slow to calculate "RGB to YUV" using a matrix. We can reverse push the following based on "YUV to RGB:
Y = 0.299 * r + 0.587 * g + 0.114 * B
U = (1/2.032) * (B-Y)
V = (1/1.140) * (R-Y)
Conclusion 2.3
The so-called YUV color model is defined by the numbers 0.299, 0.587, 0.114, 2.032, and 1.140. It is very concise and exquisite. However, this is not the most sophisticated. YCbCr is completely defined by the numbers 0.299, 0.587, and 0.114. For details, see "deep learning YCbCr color model".
Iii. Integer Algorithm
First, list the preceding results.
Convert RGB to YUV:
Y = 0.299 * r + 0.587 * g + 0.114 * B
U = (1/2.032) * (B-Y)
V = (1/1.140) * (R-Y)
YUV to RGB:
B = Y + 2.032 * u
R = Y + 1.140 * V
G = Y-(0.114*2.032/0.587) * U-(0.299*1.140/0.587) * V
It can be seen that the calculation of U, V, R, and B is to use multiplication to zoom in and out the numeric value, which can be used to look up the array table.
For the calculation of Y, refer to "color to grayscale Algorithm for complete learning".
The only trouble is that G, because it has two multiplication methods, the direct integer query table may not be accurate. So we can consider zooming the value by 65536 times (16-bit precision ).
This is probably the case:
Y = (R * 19595 + G * 38469 + B * 7472)> 16
U = yuv_b2u [0x100 + B-y]
V = yuv_r2v [0x100 + r-y]
B = Y + yuv_u2b [0x100 + u]
R = Y + yuv_v2r [0x100 + V]
G = Y-(yuv_u2g [0x100 + u] + yuv_v2g [0x100 + V])> 16)