您应该使用 OpenCV 中的 Vec3f 类型(实际上是一个 3x1 矩阵):
// I assume you have RGB values as unsigned char in [0-255] interval
// here using a dummy color
unsigned char R = 255;
unsigned char G = 127;
unsigned char B = 64;
// construct a Vec3f from those, divide by 255 to get them in [0-1] interval
Vec3f colorRGB(R/255.0f, G/255.0f, B/255.0f);
// matrix for RGB -> YIQ conversion
Matx33f matYIQ( 0.299f, 0.587f, 0.114f,
0.596f, -0.274f, -0.322f,
0.211f, -0.523f, 0.312f);
// do the conversion
// a warning ... I & Q can be negative
// Y => [0,1]
// I => [-1,1]
// Q => [-1,1]
Vec3f colorYIQ = matYIQ * colorRGB;
--- 编辑---
这是一个更好的版本,仅使用 OpenCV 功能转换整个图像
// let's define the matrix for RGB -> YIQ conversion
Matx33f matYIQ( 0.299f, 0.587f, 0.114f,
0.596f, -0.274f, -0.322f,
0.211f, -0.523f, 0.312f);
// I assume you have a source image of type CV_8UC3
// CV_8UC3: 3 channels, each on unsigned char, so [0,255]
// here is a dummy one, black by default, 256x256
Mat ImgRGB_8UC3(256, 256, CV_8UC3);
// We need to convert this to a new image of type CV_32FC3
// CV_32FC3: 3 channels each on 32bit float [-inf, +inf]
// we need to do this because YIQ result will be in [-1.0, 1.0] (I & Q)
// so this obviously cannot be stored in CV_8UC3
// At the same time, we will also divide by 255.0 to put values in [0.0, 1.0]
Mat ImgYIQ_32FC3;
ImgRGB_8UC3.convertTo(ImgYIQ_32FC3, CV_32FC3, 1.0/255.0);
// at this point ImgYIQ_32FC3 contains pixels made of 3 RGB float components in [0-1]
// so let's convert to YIQ
// (cv::transform will apply the matrix to each 3 component pixel of ImgYIQ_32FC3)
cv::transform(ImgYIQ_32FC3, ImgYIQ_32FC3, matYIQ);