以下为从一机器人系统中提出出来的关于各个颜色空间的转换,这些转换都是基于通道值的,对于不使用OPENCV的程序还是有用的。
static void fromYCbCrToRGB(unsigned char Y,
unsigned char Cb,
unsigned char Cr,
unsigned char& R,
unsigned char& G,
unsigned char& B)
{
int r = Y + ((1436 * (Cr - 128)) >> 10),
g = Y - ((354 * (Cb - 128) + 732 * (Cr - 128)) >> 10),
b = Y + ((1814 * (Cb - 128)) >> 10);
if(r < 0) r = 0;
else if(r > 255) r = 255;
if(g < 0) g = 0;
else if(g > 255) g = 255;
if(b < 0) b = 0;
else if(b > 255) b = 255;
R = (unsigned char) r;
G = (unsigned char) g;
B = (unsigned char) b;
}
/** Converts an RGB pixel into an YCbCr pixel.
* @param R The R channel of the source pixel.
* @param G The G channel of the source pixel.
* @param B The B channel of the source pixel.
* @param Y The Y channel of the target pixel.
* @param Cb The Cb channel of the target pixel.
* @param Cr The Cr channel of the target pixel.
*/
static void fromRGBToYCbCr(unsigned char R,
unsigned char G,
unsigned char B,
unsigned char& Y,
unsigned char& Cb,
unsigned char& Cr)
{
int y = (int)(0.2990 * R + 0.5870 * G + 0.1140 * B),
cr = 127 + (int)(-0.1687 * R - 0.3313 * G + 0.5000 * B),
cb = 127 + (int)(0.5000 * R - 0.4187 * G - 0.0813 * B);
if(y < 0) y = 0;
else if(y > 255) y = 255;
if(cb < 0) cb = 0;
else if(cb > 255) cb = 255;
if(cr < 0) cr = 0;
else if(cr &