Matrix Multiplication Type Issue

asked 2018-01-30 12:00:08 -0500

raisa_ gravatar image

I'm trying to do color conversion from RGB into LMS and multiply the matrix using reshape.

void test(const Mat &ori, Mat &output, Mat &pic, int rows, int cols)
    Mat lms(3, 3, CV_32FC3);
    Mat rgb(3, 3, CV_32FC3);

    lms = (Mat_<float>(3, 3) << 1.4671, 0.1843, 0.0030,
                                3.8671, 27.1554, 3.4557,
                                4.1194, 45.5161 , 17.884 );
    /* switch the order of the matrix according to the BGR order of color on OpenCV */

    Mat transpose = (3, 3, CV_32FC3, lms).t();  // this will do transpose from matrix lms

    pic         = ori.reshape(1, rows*cols);
    rgb         = pic*transpose;
    output      = rgb.reshape(3, cols);

About reshape, because my original Mat object is from an image with 3 channel (RGB), and I need to multiply them with matrix of 1 channel, it seems like I have an issue with the matrix type.

This is how I define my original mat

Mat ori = imread("colorcubes.png", CV_LOAD_IMAGE_COLOR);
int rows = ori.rows;
int cols = ori.cols;
Mat pic(rows, cols, CV_32FC3);
Mat output(rows, cols, CV_32FC3);

Error is : OpenCV Error: Assertion failed (type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)) , so it's the type issue.

I tried to change all type into either 32FC3 of 32FC1, but doesn't seem to work. Any suggestion ?

edit retag flag offensive close merge delete


i'm pretty sure, you want a transform() here, not a (gemm) matrix multiplication, like:

transform(input, output, lms);

berak gravatar imageberak ( 2018-01-30 12:03:12 -0500 )edit

I tried using transform too, then I read reference here which is doing exactly the same with what I'm trying to do. So I'm trying to implement it.

raisa_ gravatar imageraisa_ ( 2018-01-30 12:05:40 -0500 )edit
  • don't pre-allocate output Mat's like pic, or output, they will be overwritten anyway. (it also does not, what you expect !)
  • you'll have to convert your input to float instead, like: ori.convertTo(ori, CV_32F);
berak gravatar imageberak ( 2018-01-31 04:37:15 -0500 )edit