RGB To Gray Or To Lab

To implement Spectral Residual Approach, I need to transform the images from RGB space into Gray space.

RGB To Gray

1
Gray = R*0.299 + G*0.587 + B*0.114

RGB To Lab

To implement Frequency Tuned Approach, I need to transform the images from RGB space into Lab space.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void RGB2Lab(double R, double G, double B, double &L, double &a, double &b)
{
double X, Y, Z, fX, fY, fZ;
X = 0.412453*R + 0.357580*G + 0.180423*B;
Y = 0.212671*R + 0.715160*G + 0.072169*B;
Z = 0.019334*R + 0.119193*G + 0.950227*B;
X /= (255 * 0.950456);
Y /= 255;
Z /= (255 * 1.088754);
if (Y > 0.008856)
{
fY = pow(Y, 1.0/3.0);
L = 116.0*fY - 16.0;
}
else
{
fY = 7.787*Y + 16.0/116.0;
L = 903.3*Y;
}
if (X > 0.008856) {
fX = pow(X, 1.0/3.0);
}
else {
fX = 7.787*X + 16.0/116.0;
}
if (Z > 0.008856) {
fZ = pow(Z, 1.0/3.0);
}
else {
fZ = 7.787*Z + 16.0/116.0;
}
a = 500.0*(fX - fY);
b = 200.0*(fY - fZ);
}

Transformation Formulas (Page 18-19, Formula 4-4 To 4-9)

References