Basic matrix multiplication in OpenCV for Android

╄→гoц情女王★ 提交于 2019-11-30 20:23:29

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 with 1 row and 3 columns
mat1.put(0, 0, 2.0); // Set row 1 , column 1
mat1.put(0, 1, 0.5); // Set row 1 , column 2
mat1.put(0, 2, 1.0); // Set row 1 , column 3

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!