基于YUV的色相调节(二)

接上一篇:基于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 UmVm,旋转完毕后再乘回来。
(注:该方法事实上不能将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= 1110UmWGWBUm1Vm1VmWGWR0 1UmVm 1000cosθsinθ0sinθcosθ WRWR(1WR)WGWGWGWB(1WB)WB = 1110UmWGWBUm1Vm1VmWGWR0 WRUmWRcosθUm(1WR)sinθVmWRsinθ+Vm(1WR)cosθWGUmWGcosθ+UmWGsinθVmWGsinθVmWGcosθWBUm(1WB)cosθ+UmWBsinθVm(1WB)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+(1WR)cosθWRsinθ=WGWGcosθWGsinθ=WBWBcosθ+(1WB)sinθ=WRWRcosθ+WGWB(1WR)+WR2sinθ=WG+(1WG)cosθ+(WRWB)sinθ=WBWBcosθWGWB2+WR(1WB)sinθ=WRWRcosθ(1WR)sinθ=WGWGcosθ+WGsinθ=WB+(1WB)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= 1110UmWGWBUm1Vm1VmWGWR0 1UmaxVmax 1000cosθsinθ0sinθcosθ WR1WBWR1WG1WBWG1WRWGWB11WRWB = 1110UmaxWG(1WB)WBUmax1WBVmax1WRVmaxWG(1WR)WR0 WR1WBUmaxWRcosθUmaxsinθ1WBVmaxWRsinθ+VmaxcosθWG1WBUmaxWGcosθ+1WRUmaxWGsinθ1WBVmaxWGsinθ1WRVmaxWGcosθWBUmaxcosθ+1WRUmaxWBsinθVmaxsinθ1WRVmaxWBcosθ

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+(1WR)cosθ1WB1WRWRsinθ=WGWGcosθ1WB1WRWGsinθ=WBWBcosθ+(1WR)sinθ=WRWRcosθ+WG(1WB)(1WB)2WB+(1WR)WR2sinθ=WG+(1WG)cosθ+(1WR)(1WB)(1WR)2WR(1WB)2WBsinθ=WBWBcosθWG(1WR)(1WB)WB2+(1WR)2WRsinθ=WRWRcosθ(1WB)sinθ=WGWGcosθ+1WR1WBWGsinθ=WB+(1WB)cosθ+1WR1WBWBsinθ(18)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值