一. YUV 方法(或者叫YCbCr)
通过YUV改变图像的饱和度,r的范围可以设置为为(1/5, 5) , 如果为r=1 表示不改变图像数据
公式如下:
Y = Y;
U = U*r; //实际上表示色差B-Y 扩大r倍
V = V*r; //实际表示色差R-Y 扩大r倍
二、灰度图法混合法(和YUV法其实是等价的,下面有证明)
原图和灰度图进行加权组合即可改变图像的饱和度
saturate = function (pixels, value) {
var d = pixels.data;
for (var i = 0; i < d.length; i += 4) {
var r = d[i];
var g = d[i + 1];
var b = d[i + 2];
var gray = 0.2989*r + 0.5870*g + 0.1140*b; //weights from CCIR 601 spec
d[i] = -gray * value + d[i] * (1+value);
d[i+1] = -gray * value + d[i+1] * (1+value);
d[i+2] = -gray * value + d[i+2] * (1+value);
//normalize over- and under-saturated values
if(d[i] > 255) d[i] = 255;
if(d[i+1] > 255) d[i] = 255;
if(d[i+2] > 255) d[i] = 255;
if(d[i] < 0) d[i] = 0;
if(d[i+1] < 0) d[i] = 0;
if(d[i+2] < 0) d[i] = 0;
}
return pixels;
};
其实灰度图法和YUV法是等价的, 都是改变色差的权重
如果将一下代码更改权重, 则该算法和算法一是等价的
B = B + r1(B-Y) => r1 = r-1
G = G + r2(G-Y) =>r2 = r-1 (推导见附录r2推导过程)
R = R + r3(R-Y) => r3 = r-1
r2 = r-1 推导过程
由 BT601 YUV公式 可得:
G = Y - 0.3455 * U - 0.7169*V;
U = (B-Y)/1.772;
V = (R-Y)/1.402;
即 G = Y -0.3455 * U - 0.7169 *V
G = Y - r(0.3455 * U + 0.7169 *V) //法一 ①
G = G + r2(G-Y) //法二 ②
①②联立得:
Y - r(0.3455 * U + 0.7169 *V) = (1+r2)(Y - 0.3455 * U - 0.7169*V) - r2*Y
= Y - (1+r2)*( 0.3455*U +0.7169V )
=> 1+r2 = r
=> r2 = r -1
网友评论