# Matrix multiplication assertion failed

I'm trying to perform a simple matrix multiplication using OpenCV from Java but keep getting the following assertion error message:

OpenCV Error: Assertion failed (C.type() == type && (((flags&GEMM_3_T) == 0 && C.rows == d_size.height && C.cols == d_size.width) || ((flags&GEMM_3_T) != 0 && C.rows == d_size.width && C.cols == d_size.height))) in gemm


The following code causes the above error message to be thrown. I'm trying to achieve a very simple matrix multiplication in the form A' x B x C = D.

    Mat A = new Mat(3, 1, CvType.CV_64F);
A.put(0, 0, new double[]{ 1, 2, 3 });

Mat B = new Mat(3, 3, CvType.CV_64F);
B.put(0, 0, new double[]{
1, 2, 3,
4, 5, 6,
7, 8, 9
});

Mat C = new Mat(3, 1, CvType.CV_64F);
C.put(0, 0, new double[]{1, 2, 3});

Mat D = new Mat();

Core.gemm(A.t(), B, 1, C, 1, D);


edit retag close merge delete

2

shouldn't it be : Core.gemm(A.t(), B, 1, C.t(), 1, D); ?

A and C are both (3x1) and B is (3x3). For the matrix multiplication to work, don't the number of columns in the first matrix have to equal the number of rows in the second? A' x B x C give us matrices of size (1x3) x (3x3) x (3x1) which I believe should work (WolframAlpha)?

I've also tried Core.gemm(A, B, 1, C, 1, D, Core.GEMM_1_T) to no avail.

Sort by » oldest newest most voted

Hello,

Here the function gemm() is not actually doing the operation A * B * C but the operation A*B + C. That's why it doesn't work and the answer of berak works.

more

Official site

GitHub

Wiki

Documentation