Ask Your Question
0

Matrix multiplication assertion failed

asked 2015-04-27 03:24:57 -0600

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);

Please help!

edit retag flag offensive close merge delete

Comments

2

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

berak gravatar imageberak ( 2015-04-27 03:42:50 -0600 )edit

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)?

nick_au gravatar imagenick_au ( 2015-04-27 18:33:56 -0600 )edit

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

nick_au gravatar imagenick_au ( 2015-04-27 19:09:24 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-06-02 03:47:31 -0600

moud gravatar image

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.

edit flag offensive delete link more

Comments

oh, you're right. i missed that entirely.

berak gravatar imageberak ( 2015-06-02 03:51:43 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-27 03:24:57 -0600

Seen: 1,785 times

Last updated: Jun 02 '15