接上一篇:基于YUV的色相调节(一)
量纲范围归一化
正常情况下UV的量纲范围不一样,这对旋转而言不是个好事情,因为可能需要做一个椭圆形旋转,而实际上公式里的旋转矩阵是圆形的,所以可以考虑将UV的量纲范围设置成一样的,也就是各自乘以一个系数,旋转完毕后再除以相应的系数。
归一化因子: U m , V m U_m, V_m Um,Vm
先给
M
R
G
B
2
Y
U
V
M_{RGB2YUV}
MRGB2YUV的第二行和第三行分别除以
U
m
,
V
m
U_m,V_m
Um,Vm,旋转完毕后再乘回来。
(注:该方法事实上不能将UV真正归一化到相同的量纲范围)
即:
M = [ 1 0 1 V m 1 − W B U m W G − W R V m W G 1 1 U m 0 ] ( [ 1 U m V m ] ⋅ [ 1 0 0 0 c o s θ − s i n θ 0 s i n θ c o s θ ] [ W R W G W B − W R − W G ( 1 − W B ) ( 1 − W R ) − W G − W B ] ) = [ 1 0 1 V m 1 − W B U m W G − W R V m W G 1 1 U m 0 ] [ W R W G W B − U m W R c o s θ − U m ( 1 − W R ) s i n θ − U m W G c o s θ + U m W G s i n θ U m ( 1 − W B ) c o s θ + U m W B s i n θ − V m W R s i n θ + V m ( 1 − W R ) c o s θ − V m W G s i n θ − V m W G c o s θ V m ( 1 − W B ) s i n θ − V m W B c o s θ ] \begin{aligned} M &= \begin{bmatrix} 1 & 0 & \frac{1}{V_m} \\[2ex] 1 & -\frac{W_B}{U_mW_G} & -\frac{W_R}{V_mW_G} \\[2ex] 1 & \frac{1}{U_m} & 0 \end{bmatrix} \left( \begin{bmatrix} 1 \\ U_m \\ V_m \end{bmatrix}\cdot \begin{bmatrix} 1 & 0 & 0\\ 0 & cos{\theta} & -sin{\theta} \\ 0 & sin{\theta} & cos{\theta} \end{bmatrix} \begin{bmatrix} W_R & W_G & W_B \\ -W_R & -W_G & (1-W_B) \\ (1-W_R) & -W_G & -W_B \end{bmatrix} \right) \\[8ex] &= \begin{bmatrix} 1 & 0 & \frac{1}{V_m} \\[2ex] 1 & -\frac{W_B}{U_mW_G} & -\frac{W_R}{V_mW_G} \\[2ex] 1 & \frac{1}{U_m} & 0 \end{bmatrix} \begin{bmatrix} W_R & W_G & W_B \\ -U_mW_Rcos{\theta} - U_m(1-W_R)sin{\theta} & -U_mW_Gcos{\theta} + U_mW_Gsin{\theta} & U_m(1-W_B)cos{\theta} + U_mW_Bsin{\theta} \\ -V_mW_Rsin{\theta} + V_m(1-W_R)cos{\theta} & -V_mW_Gsin{\theta} - V_mW_Gcos{\theta} & V_m(1-W_B)sin{\theta} - V_mW_Bcos{\theta} \end{bmatrix} \\[8ex] \end{aligned} M= 1110−UmWGWBUm1Vm1−VmWGWR0 1UmVm ⋅ 1000cosθsinθ0−sinθcosθ WR−WR(1−WR)WG−WG−WGWB(1−WB)−WB = 1110−UmWGWBUm1Vm1−VmWGWR0 WR−UmWRcosθ−Um(1−WR)sinθ−VmWRsinθ+Vm(1−WR)cosθWG−UmWGcosθ+UmWGsinθ−VmWGsinθ−VmWGcosθWBUm(1−WB)cosθ+UmWBsinθVm(1−WB)sinθ−VmWBcosθ
m 00 = W R + ( 1 − W R ) c o s θ − W R s i n θ m 01 = W G − W G c o s θ − W G s i n θ m 02 = W B − W B c o s θ + ( 1 − W B ) s i n θ m 10 = W R − W R c o s θ + W B ( 1 − W R ) + W R 2 W G s i n θ m 11 = W G + ( 1 − W G ) c o s θ + ( W R − W B ) s i n θ m 12 = W B − W B c o s θ − W B 2 + W R ( 1 − W B ) W G s i n θ m 20 = W R − W R c o s θ − ( 1 − W R ) s i n θ m 21 = W G − W G c o s θ + W G s i n θ m 22 = W B + ( 1 − W B ) c o s θ + W B s i n θ (17) \begin{aligned} m_{00} &= W_R + (1-W_R)cos{\theta} - W_Rsin{\theta} \\[2ex] m_{01} &= W_G - W_Gcos{\theta} - W_Gsin{\theta} \\[2ex] m_{02} &= W_B - W_Bcos{\theta} + (1-W_B)sin{\theta} \\[3ex] m_{10} &= W_R - W_Rcos{\theta}+\frac{W_B(1-W_R)+W_R^2}{W_G}sin{\theta} \\[3ex] m_{11} &= W_G + (1-W_G)cos{\theta} + (W_R - W_B)sin{\theta}\\[3ex] m_{12} &= W_B-W_Bcos{\theta} - \frac{W_B^2 + W_R(1-W_B)}{W_G}sin{\theta} \\[3ex] m_{20} &= W_R-W_Rcos{\theta} - (1-W_R)sin{\theta} \\[2ex] m_{21} &= W_G - W_Gcos{\theta} + W_Gsin{\theta} \\[2ex] m_{22} &= W_B + (1-W_B)cos{\theta} + W_B sin{\theta} \\[2ex] \end{aligned} \tag{17} m00m01m02m10m11m12m20m21m22=WR+(1−WR)cosθ−WRsinθ=WG−WGcosθ−WGsinθ=WB−WBcosθ+(1−WB)sinθ=WR−WRcosθ+WGWB(1−WR)+WR2sinθ=WG+(1−WG)cosθ+(WR−WB)sinθ=WB−WBcosθ−WGWB2+WR(1−WB)sinθ=WR−WRcosθ−(1−WR)sinθ=WG−WGcosθ+WGsinθ=WB+(1−WB)cosθ+WBsinθ(17)
在网上找了一圈发现,mozilla里面有个hue_rotate功能比较相似,并且是开源的。
网址:https://hg.mozilla.org/mozilla-central
代码文件路径:ROOT_FOLDER/gfx/wr/webrender/res/blend.glsl
其代码如下:
else if (op == FILTER_HUE_ROTATE) {
float c = cos(amount);
float s = sin(amount);
color_mat = mat4(
vec4(lumR + oneMinusLumR * c - lumR * s, lumR - lumR * c + 0.143 * s, lumR - lumR * c - oneMinusLumR * s, 0.0),
vec4(lumG - lumG * c - lumG * s, lumG + oneMinusLumG * c + 0.140 * s, lumG - lumG * c + lumG * s, 0.0),
vec4(lumB - lumB * c + oneMinusLumB * s, lumB - lumB * c - 0.283 * s, lumB + oneMinusLumB * c + lumB * s, 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);
color_offset = vec4(0.0);
}
注意mozilla这里的color_mat
跟公式(17)比起来有个转置,除此之外一模一样。
归一化因子: U m a x , V m a x U_{max}, V_{max} Umax,Vmax
上述归一化实际上不能将UV的范围归到相同的范围,上面的 U m , V m U_m, V_m Um,Vm是我们自己定义的,而Rec.709中定义的常数是 U m a x , V m a x U_{max}, V_{max} Umax,Vmax,使用 U m a x , V m a x U_{max}, V_{max} Umax,Vmax能够将UV的范围真正归一化到 [-1, 1] 之间。
M = [ 1 0 1 V m 1 − W B U m W G − W R V m W G 1 1 U m 0 ] ( [ 1 U m a x V m a x ] ⋅ [ 1 0 0 0 c o s θ − s i n θ 0 s i n θ c o s θ ] [ W R W G W B − W R 1 − W B − W G 1 − W B 1 1 − W G 1 − W R − W B 1 − W R ] ) = [ 1 0 1 − W R V m a x 1 − ( 1 − W B ) W B U m a x W G − ( 1 − W R ) W R V m a x W G 1 1 − W B U m a x 0 ] [ W R W G W B − U m a x W R 1 − W B c o s θ − U m a x s i n θ − U m a x W G 1 − W B c o s θ + U m a x W G 1 − W R s i n θ U m a x c o s θ + U m a x W B 1 − W R s i n θ − V m a x W R 1 − W B s i n θ + V m a x c o s θ − V m a x W G 1 − W B s i n θ − V m a x W G 1 − W R c o s θ V m a x s i n θ − V m a x W B 1 − W R c o s θ ] \begin{aligned} M &= \begin{bmatrix} 1 & 0 & \frac{1}{V_m} \\[2ex] 1 & -\frac{W_B}{U_mW_G} & -\frac{W_R}{V_mW_G} \\[2ex] 1 & \frac{1}{U_m} & 0 \end{bmatrix} \left( \begin{bmatrix} 1 \\ U_{max} \\ V_{max} \end{bmatrix}\cdot \begin{bmatrix} 1 & 0 & 0\\ 0 & cos{\theta} & -sin{\theta} \\ 0 & sin{\theta} & cos{\theta} \end{bmatrix} \begin{bmatrix} W_R & W_G & W_B \\ -\frac{W_R}{1-W_B} & -\frac{W_G}{1-W_B} & 1 \\ 1 & -\frac{W_G}{1-W_R} & -\frac{W_B}{1-W_R} \end{bmatrix} \right) \\[8ex] &= \begin{bmatrix} 1 & 0 & \frac{1-W_R}{V_{max}} \\[2ex] 1 & -\frac{(1-W_B)W_B}{U_{max}W_G} & -\frac{(1-W_R)W_R}{V_{max}W_G} \\[2ex] 1 & \frac{1-W_B}{U_{max}} & 0 \end{bmatrix} \begin{bmatrix} W_R & W_G & W_B \\[2ex] -\frac{U_{max}W_R}{1-W_B}cos{\theta} - U_{max}sin{\theta} & -\frac{U_{max}W_G}{1-W_B}cos{\theta} + \frac{U_{max}W_G}{1-W_R}sin{\theta} & U_{max}cos{\theta} + \frac{U_{max}W_B}{1-W_R}sin{\theta} \\[2ex] -\frac{V_{max}W_R}{1-W_B}sin{\theta} + V_{max}cos{\theta} & -\frac{V_{max}W_G}{1-W_B}sin{\theta} -\frac{V_{max}W_G}{1-W_R}cos{\theta} & V_{max}sin{\theta} - \frac{V_{max}W_B}{1-W_R}cos{\theta} \end{bmatrix} \\[8ex] \end{aligned} M= 1110−UmWGWBUm1Vm1−VmWGWR0 1UmaxVmax ⋅ 1000cosθsinθ0−sinθcosθ WR−1−WBWR1WG−1−WBWG−1−WRWGWB1−1−WRWB = 1110−UmaxWG(1−WB)WBUmax1−WBVmax1−WR−VmaxWG(1−WR)WR0 WR−1−WBUmaxWRcosθ−Umaxsinθ−1−WBVmaxWRsinθ+VmaxcosθWG−1−WBUmaxWGcosθ+1−WRUmaxWGsinθ−1−WBVmaxWGsinθ−1−WRVmaxWGcosθWBUmaxcosθ+1−WRUmaxWBsinθVmaxsinθ−1−WRVmaxWBcosθ
m 00 = W R + ( 1 − W R ) c o s θ − 1 − W R 1 − W B W R s i n θ m 01 = W G − W G c o s θ − 1 − W R 1 − W B W G s i n θ m 02 = W B − W B c o s θ + ( 1 − W R ) s i n θ m 10 = W R − W R c o s θ + ( 1 − W B ) 2 W B + ( 1 − W R ) W R 2 W G ( 1 − W B ) s i n θ m 11 = W G + ( 1 − W G ) c o s θ + ( 1 − W R ) 2 W R − ( 1 − W B ) 2 W B ( 1 − W R ) ( 1 − W B ) s i n θ m 12 = W B − W B c o s θ − ( 1 − W B ) W B 2 + ( 1 − W R ) 2 W R W G ( 1 − W R ) s i n θ m 20 = W R − W R c o s θ − ( 1 − W B ) s i n θ m 21 = W G − W G c o s θ + 1 − W B 1 − W R W G s i n θ m 22 = W B + ( 1 − W B ) c o s θ + 1 − W B 1 − W R W B s i n θ (18) \begin{aligned} m_{00} &= W_R + (1-W_R)cos{\theta} - \frac{1-W_R}{1-W_B}W_Rsin{\theta} \\[2ex] m_{01} &= W_G - W_Gcos{\theta} - \frac{1-W_R}{1-W_B}W_Gsin{\theta} \\[2ex] m_{02} &= W_B - W_Bcos{\theta} + (1-W_R)sin{\theta} \\[3ex] m_{10} &= W_R - W_Rcos{\theta}+\frac{(1-W_B)^2W_B+(1-W_R)W_R^2}{W_G(1-W_B)}sin{\theta} \\[3ex] m_{11} &= W_G + (1-W_G)cos{\theta} + \frac{(1-W_R)^2W_R-(1-W_B)^2W_B}{(1-W_R)(1-W_B)}sin{\theta}\\[3ex] m_{12} &= W_B-W_Bcos{\theta} - \frac{(1-W_B)W_B^2+(1-W_R)^2W_R}{W_G(1-W_R)}sin{\theta} \\[3ex] m_{20} &= W_R-W_Rcos{\theta} - (1-W_B)sin{\theta} \\[2ex] m_{21} &= W_G - W_Gcos{\theta} + \frac{1-W_B}{1-W_R}W_Gsin{\theta} \\[2ex] m_{22} &= W_B + (1-W_B)cos{\theta} + \frac{1-W_B}{1-W_R}W_B sin{\theta} \\[2ex] \end{aligned} \tag{18} m00m01m02m10m11m12m20m21m22=WR+(1−WR)cosθ−1−WB1−WRWRsinθ=WG−WGcosθ−1−WB1−WRWGsinθ=WB−WBcosθ+(1−WR)sinθ=WR−WRcosθ+WG(1−WB)(1−WB)2WB+(1−WR)WR2sinθ=WG+(1−WG)cosθ+(1−WR)(1−WB)(1−WR)2WR−(1−WB)2WBsinθ=WB−WBcosθ−WG(1−WR)(1−WB)WB2+(1−WR)2WRsinθ=WR−WRcosθ−(1−WB)sinθ=WG−WGcosθ+1−WR1−WBWGsinθ=WB+(1−WB)cosθ+1−WR1−WBWBsinθ(18)