用c++實現顏色空間rgb,grey,luv和lab的互轉
1 rgb轉grey,rgb轉luv,rgb轉lab
1. 1 rgb轉grey
-
void RgbToGrey(unsigned char *rgb, double *grey)
-
{
-
double R = ((double) rgb[0]) / (double)255.0;
-
double G = ((double) rgb[1]) / (double)255.0;
-
double B = ((double) rgb[2]) / (double)255.0;
-
double y = (double)0.212671 * R + (double)0.715160 * G + (double)0.072169 * B;
-
if (y > (double)0.008856) grey[0] = (double)116.0 * pow(y, (double)1.0 / (double)3.0) - (double)16.0;
-
else grey[0] = (double)903.3 * y;
-
}
1. 2 rgb轉luv
-
void RgbToLuv(unsigned char *rgb, double *luv)
-
{
-
double L, u, v;
-
double R = ((double) rgb[0]) / (double)255.0;
-
double G = ((double) rgb[1]) / (double)255.0;
-
double B = ((double) rgb[2]) / (double)255.0;
-
double x = (double)0.412453 * R + (double)0.357580 * G + (double)0.180423 * B;
-
double y = (double)0.212671 * R + (double)0.715160 * G + (double)0.072169 * B;
-
double z = (double)0.019334 * R + (double)0.119193 * G + (double)0.950227 * B;
-
if (y > 0.008856) L = (double)116.0 * pow(y, (double)1.0 / (double)3.0) - (double)16.0;
-
else L = (double)903.3 * y;
-
double sum = x + 15 * y + 3 * z;
-
if(sum != 0) u = 4 * x / sum, v = 9 * y / sum;
-
else u = 4.0, v = (double)9.0 / (double)15.0;
-
luv[0] = L;
-
luv[1] = 13 * L * (u - (double)0.19784977571475);
-
luv[2] = 13 * L * (v - (double)0.46834507665248);
-
}
1. 3 rgb轉grey
-
void RgbToLab(unsigned char *rgb, double *lab)
-
{
-
double R = ((double) rgb[0]) / (double)255.0;
-
double G = ((double) rgb[1]) / (double)255.0;
-
double B = ((double) rgb[2]) / (double)255.0;
-
double X = 0.412453 * R + 0.357580 * G + 0.180423 * B;
-
double Y = 0.212671 * R + 0.715160 * G + 0.072169 * B;
-
double Z = 0.019334 * R + 0.119193 * G + 0.950227 * B;
-
double xr = X / 0.950456, yr = Y / 1.000, zr = Z / 1.088854;
-
if(yr > 0.008856) lab[0] = 116.0 * pow(yr, 1.0 / 3.0) - 16.0;
-
else lab[0] = 903.3 * yr;
-
double fxr, fyr, fzr;
-
if(xr > 0.008856) fxr = pow(xr, 1.0 / 3.0);
-
else fxr = 7.787 * xr + 16.0 / 116.0;
-
if(yr > 0.008856) fyr = pow(yr, 1.0 / 3.0);
-
else fyr = 7.787 * yr + 16.0 / 116.0;
-
if(zr > 0.008856) fzr = pow(zr, 1.0 / 3.0);
-
else fzr = 7.787 * zr + 16.0 / 116.0;
-
lab[1] = 500.0 * (fxr - fyr);
-
lab[2] = 200.0 * (fyr - fzr);
-
}
2 grey轉rgb,grey轉luv,grey轉lab
2.1 grey轉rgb
-
void GreyToRgb(double *grey, unsigned char *rgb)
-
{
-
if(grey[0] < (double)0.1) rgb[0] = rgb[1] = rgb[2] = 0;
-
else
-
{
-
double y;
-
if(grey[0] <= (double)7.9996) y = 255 * grey[0] / (double)903.3;
-
else y = (grey[0] + (double)16.0) / (double)116.0, y = 255 * y * y * y;
-
rgb[0] = (unsigned char)((y < 0) ? 0 : ((y > 255) ? 255 : y));
-
rgb[2] = rgb[1] = rgb[0];
-
}
-
}
2.2 grey轉luv
-
void GreyToLuv(double *grey, double *luv)
-
{
-
luv[0] = grey[0], luv[1] = luv[2] = 0;
-
}
2.3 grey轉lab
-
void GreyToLab(double *grey, double *lab)
-
{
-
lab[0] = grey[0], lab[1] = lab[2] = 0;
-
}
3 luv轉rgb,luv轉grey
3.1 luv轉rgb
-
void LuvToRgb(double *luv, unsigned char *rgb)
-
{
-
if(luv[0] < 0.1) rgb[0] = rgb[1] = rgb[2] = 0;
-
else
-
{
-
double x, y, z, u, v;
-
if(luv[0] <= (double)7.9996) y = luv[0] / (double)903.3;
-
else y = (luv[0] + (double)16.0) / (double)116.0, y = y * y * y;
-
u = luv[1] / (13 * luv[0]) + (double)0.19784977571475;
-
v = luv[2] / (13 * luv[0]) + (double)0.46834507665248;
-
x = 9 * u * y / (4 * v);
-
z = (12 - 3 * u - 20 * v) * y / (4 * v);
-
double R = (double)3.240479 * x - (double)1.537150 * y - (double)0.498535 * z;
-
double G = (double)-0.969256 * x + (double)1.875992 * y + (double)0.041556 * z;
-
double B = (double)0.055648 * x - (double)0.204043 * y + (double)1.057311 * z;
-
R *= 255, G *= 255, B *= 255;
-
rgb[0] = (unsigned char)((R < 0) ? 0 : ((R > 255) ? 255 : R));
-
rgb[1] = (unsigned char)((G < 0) ? 0 : ((G > 255) ? 255 : G));
-
rgb[2] = (unsigned char)((B < 0) ? 0 : ((B > 255) ? 255 : B));
-
}
-
}
3.2 luv轉grey
-
void LuvToGrey(double *luv, double *grey)
-
{
-
grey[0] = luv[0];
-
}
4 lab轉rgb,lab轉grey
4.1 lab轉rgb
-
void LabToRgb(double *lab, unsigned char *rgb)
-
{
-
double X, Y, Z;
-
double P = (lab[0] + 16.0) / 116.0;
-
if(lab[0] > 7.9996) Y = 1.000 * P * P * P;
-
else Y = 1.000 * lab[0] / 903.3;
-
double yr = Y / 1.000, fy;
-
if(yr > 0.008856) fy = pow(yr, 1.0 / 3.0);
-
else fy = 7.787 * yr + 16.0 / 116.0;
-
double fx = lab[1] / 500.0 + fy, fz = fy - lab[2] / 200.0;
-
if(fx > 0.2069) X = 0.950456 * fx * fx * fx;
-
else X = 0.950456 / 7.787 * (fx - 16.0 / 116.0);
-
if(fz > 0.2069) Z = 1.088854 * fz * fz * fz;
-
else Z = 1.088854 / 7.787 * (fz - 16.0 / 116.0);
-
double R = 3.240479 * X - 1.537150 * Y - 0.498535 * Z;
-
double G = -0.969256 * X + 1.875992 * Y + 0.041556 * Z;
-
double B = 0.055648 * X - 0.204043 * Y + 1.057311 * Z;
-
R *= 255, G *= 255, B *= 255;
-
rgb[0] = (unsigned char)((R < 0) ? 0 : ((R > 255) ? 255 : R));
-
rgb[1] = (unsigned char)((G < 0) ? 0 : ((G > 255) ? 255 : G));
-
rgb[2] = (unsigned char)((B < 0) ? 0 : ((B > 255) ? 255 : B));
-
}
4.2 lab轉grey
-
void LabToGrey(double *lab, double *grey)
-
{
-
grey[0] = lab[0];
-
}
5 參考文獻
https://blog.csdn.net/google0802/article/details/17633111
[1]http://blog.sina.com.cn/s/blog_5309cefc0101cdeg.html 作者:RescuEe 題目:用c++實現顏色空間rgb,grey,luv和lab的互轉
[2]http://blog.sina.com.cn/s/blog_46e3af5b0100u7wt.html 作者:孫志海 題目:RGB與Lab顏色空間轉換的程式實現及效果(VC++6.0)