Skip to content Skip to sidebar Skip to footer

Basic Matrix Multiplication In Opencv For Android

I'm probably being incredibly stupid here but I'm having trouble doing some basicaly Mat multiplication using OpenCV for Android. I have two Mat's both of the same type, CV_64F mat

Solution 1:

You are basicly trying to perform the following operation now:

[ 0 ][ 0 1 2 ][ 1 ] * [ 3 4 5 ][ 2 ][ 6 7 8 ]

In here * is multiplication. A matrix multiplication cannot be done this way. Read on matrix multiplications here.

The operation you would like to perform is :

[ 0 1 2 ][ 0 1 2 ] * [ 3 4 5 ][ 6 7 8 ]

To get your code working make the following changes:

Mat mat1 =new Mat(1, 3, CvType.CV_64F); // A matrix with1rowand3 columns
mat1.put(0, 0, 2.0); //Setrow1 , column1
mat1.put(0, 1, 0.5); //Setrow1 , column2
mat1.put(0, 2, 1.0); //Setrow1 , column3

EDIT

Also, you're using the method Core.multiply. In the documentation of OpenCv it mentions: The function multiply calculates the per-element product of two matrices. If you are looking for a matrix product, not per-element product, see Core.gemm().

The function gemm(src1, src2, alpha, src3, beta, dest, flags) performs the multiplication according to the following function:

dest = alpha * src1 * src2 + beta * src3

Basic matrix multiplcation (in your case) is done by:

Core.gemm(mat2, mat1, 1, NULL, 0, mat3, 0);

Solution 2:

Reverse your 2 multiplied matrices : Core.multiply(mat2, mat1, mat3);

Post a Comment for "Basic Matrix Multiplication In Opencv For Android"