Multiplying a Mat against another Mat on a GPU

asked 2016-03-17 15:59:32 -0600

JasonHonduras gravatar image

updated 2016-03-18 00:55:19 -0600

berak gravatar image

I can multiply a 1080X1920 pixel CV_32FC1 mat against another 3X3 Mat when using CPU based OpenCV, but when I convert the code to be Gpu Based, I get an error. Here is my CPU code

 float matrix[3][3] = {{1.057311, -0.204043, 0.055648},
                                { 0.041556, 1.875992, -0.969256},
                                {-0.498535,-1.537150, 3.240479}};

 Mat matrixMat = Mat(3, 3, CV_32FC1, matrix).t();

 Mat orig_img_linear = linearMat.reshape(1, 1080*1920);
 Mat color_matrixed_linear = orig_img_linear * matrixMat; 
 Mat final_color_matrixed = color_matrixed_linear.reshape(3, 1080);

When I run the following Gpu code, I get an error:

cv::gpu::multiply(orig_img_linear, matrixMat, color_matrixed_linear);

 OpenCV Error: Assertion failed (src2.type() == src1.type() && src2.size() == src1.size()) 
 in multiply, file  /Users/user/Downloads/opencv-2.4.11/modules/gpu/src/element_operations.cpp,
 line 934 libc++abi.dylib: terminating with uncaught exception of type
 cv::Exception:
 /Users/user/Downloads/opencv-2.4.11/modules/gpu/src/element_operations.cpp:934:
 error: (-215) src2.type() == src1.type() && src2.size() == src1.size() in function multiply

I assume from the error that the problem is that my two arrays' sizes are different. Is there a way to accomplish the CPU code with the GPU?

edit retag flag offensive close merge delete

Comments

1
  • please show us, how your linearMatrix looks originally
  • a * b does matrix multiplication, while multiply(a,b) does per element multiplication. can it be, you're confusing this ?
berak gravatar imageberak ( 2016-03-18 00:58:48 -0600 )edit

Looking back on my code, gemm wants to multiply Mats which have the following types: CV_32FC1 , CV_64FC1 , CV_32FC2 , or CV_64FC2

My linear Mat is CV_32FC3, so I would think I need to need to split my RGB channels and multiply them individually. But that won't work. Any recommendations?

I wish to perform matrix multiplication similar to:

xformrgb(mat,r,g,b,tr,tg,tb)
    float mat[4][4];
    float r,g,b;
    float *tr,*tg,*tb;
  {        
        *tr = r*mat[0][0] + g*mat[1][0] + b*mat[2][0] + mat[3][0];
        *tg = r*mat[0][1] + g*mat[1][1] + b*mat[2][1] + mat[3][1];
        *tb = r*mat[0][2] + g*mat[1][2] + b*mat[2][2] + mat[3][2];
    }

Any advice on how to accomplish this?

JasonHonduras gravatar imageJasonHonduras ( 2016-03-20 22:36:07 -0600 )edit

on the cpu, there's a transform function for this.

berak gravatar imageberak ( 2016-03-21 02:00:24 -0600 )edit

Yes, but I am trying to use this on the GPU

JasonHonduras gravatar imageJasonHonduras ( 2016-03-23 00:36:00 -0600 )edit