I am trying to perform an RGB color mixing operation in opencv. I have an image contained in an MxNx3 Mat. I would like it to be somewhat with a 3x3 matrix. In Matlab, I do the following: * Smooth the image from MxNx3 to MNx3 * multiply the MNx3 matrix by the 3x3 color mixing matrix * reformat it back to MxNx3
In Opencv, I would like to do the following:
void RGBMixing::mixColors(Mat &imData, Mat &rgbMixData) { float rgbmix[] = {1.4237, -0.12364, -0.30003, -0.65221, 2.1936, -0.54141, -0.38854, -0.47458, 1.8631}; Mat rgbMixMat(3, 3, CV_32F, rgbmix);
This compiles, but throws an exception:
OpenCV error: the sizes of the input arguments do not match (the operation is neither an “rray op array” (where the arrays are the same size and the same number of channels), neither an “op scalar array” nor a “scalar op array ') in the arithm_op file, file C: / slave / WinI nstallerMegaPack / src / opencv / modules / core / src / arithm.cpp, line 1253 terminate call after calling the instance 'cv :: Exception'
which (): C: /slave/WinInstallerMegaPack/src/opencv/modules/core/src/arithm.cpp: 1253: error: (-209) The operation is not an “op array” (where the arrays are the same size and the same number channels), as well as 'array op scalar', or 'sca lar op array' in the arithm_op function
This application requested Runtime to complete it in an Unusual way. Contact customer support information.
Update 1:
This is the code that works:
void RGBMixing::mixColors(Mat &imData, Mat&rgbMixData) { Size tempSize; uint32_t channels; float rgbmix[] = {1.4237, -0.12364, -0.30003, -0.65221, 2.1936, -0.54141, -0.38854, -0.47458, 1.8631}; Mat rgbMixMat(3, 3, CV_32F, rgbmix); Mat flatImage = imData.reshape(1, 3); tempSize = flatImage.size(); channels = flatImage.channels(); cout << "temp channels: " << channels << " Size: " << tempSize.width << " x " << tempSize.height << endl; Mat flatFloatImage; flatImage.convertTo(flatFloatImage, CV_32F); Mat mixedImage = flatFloatImage.t() * rgbMixMat; mixedImage = mixedImage.t(); rgbMixData = mixedImage.reshape(3, 1944); channels = rgbMixData.channels(); tempSize = rgbMixData.size(); cout << "temp channels: " << channels << " Size: " << tempSize.width << " x " << tempSize.height << endl; }
But the resulting image is distorted. If I skip the multiplication of two matrices and just assign
mixedImage = flatFloatImage
The resulting image looks great (just not mixed with color). So I have to do something wrong, but I'm getting closer.