OpenCV - 色彩校正

问题描述 投票:0回答:2

使用OpenCV。

我有一个RGB图像,每个值都是一个浮点数。我还有一个标准的色彩校正矩阵3x4。

在图像上“应用”此矩阵的最快方法是什么?

如果您不知道色彩校正......这是一个简单的矩阵运算。

如果图像看起来像这样(每个像素是3个浮点数):

R G B
R G B
R G B
R G B
R G B
.
.
.

然后我想执行以下操作:

1 R G B     [ A1, A2, A3 ] 
1 R G B     [ R1, R2, R3 ]
1 R G B  *  [ G1, G2, G3 ]
1 R G B     [ B1, B2, B3 ]
1 R G B
.
.
.

3x4矩阵中的所有值都是常量。

谢谢。

opencv emgucv
2个回答
1
投票

乘以颜色校正矩阵的rgb部分:

transform(imageIn,imageOut,M3x3);

然后添加A通道校正:

add(imageOut,Scalar(A1,A2,A3),imageOut);

阅读有关opencv2refman变换的信息,它意味着你可以使用它

transfrom(imageIn,imageOut,M4X3);

在一步中获得相同的结果(它确实dst(I)= mtx·[src(I); 1],这很有用),避免必须添加A组件。请原谅,如果那应该是M3X4。当涉及到矩阵数学,行与cols以及首先出现时,我非常沮丧。


0
投票

如果您确定矩阵中的值存储在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
© www.soinside.com 2019 - 2024. All rights reserved.