如果您确定矩阵中的值存储在 RGB 中(默认情况下 OpenCV 在 BGR 模式下读取它们),您可以继续进行简单的矩阵乘法运算。您只需创建包含您指定的 RGB 值的正确矩阵,即 1,R,G,B 1,R,G,B, ...,原始图像中每个像素 1 行
这是你可以做的(在 C++ 中)
// say img is your original RGB matrix, transform is your transformation matrix
std::vector<cv::Mat> channels;
// this extracts the R,G and B channels in single matrices
cv::split(img, channels);
// create a matrix on ones in floating point
cv::Mat oneMat = cv::Mat::ones(img.size(), CV_32F);
oneMat.push_front(channels);
// now channels is a vector containing 4 matrices of the same dimension as img
// you want to group those matrix into 1 single matrix of same dimension and 4 channels
// 1, R, G, B
cv::Mat rgbOne;
cv::merge(channels, rgbOne);
// Transform the row by col, 4 channel matrix rgbOne into a row*col, 4, single channel matrix prior to multiplication
cv::Mat reshaped = rgbOne.reshape(1, rgbOne.rows*rgbOne.cols);
// reshape is very fast as no allocation is required, check documentation
// now simply do the matrix multiplication
cv::Mat colorCorrectedImage = reshaped*transform;
// get back colorCorrectedImage to it's original dimensions
cv::Mat colorCoorectedReshaped = colorCorrectedImage.reshape(4, rgbOne.rows);
// note that you still have the extra channel of ones
// you can use split and merge functions as above to get rid of it and get your proper RGB image