java OpenCV for Android 中的基本矩阵乘法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10168058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:53:59  来源:igfitidea点击:

Basic matrix multiplication in OpenCV for Android

javaandroidopencvmatrixmultiplication

提问by woodstock365

I'm probably being incredibly stupid here but I'm having trouble doing some basicaly Mat multiplication using OpenCV for Android.

我在这里可能非常愚蠢,但是我在使用 OpenCV for Android 进行一些基本的 Mat 乘法时遇到了麻烦。

I have two Mat's both of the same type, CV_64F

我有两个相同类型的垫子, CV_64F

mat1has size: 3 rows, 3 cols
mat2has size: 3 rows, 1 cols

mat1有大小:3 行,3 列
mat2有大小:3 行,1 列

I want to multiply them to give the product mat3of size 3 rows, 1 cols.

我想将它们相乘以得到mat3大小为 3 行、1 列的乘积。

I've tried using:

我试过使用:

Mat mat3 = new Mat(3, 1, CvType.CV_64F);
Core.multiply(mat1, mat2, mat3);


But I get an error:


但我收到一个错误:

CvException [org.opencv.core.CvException:/home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/core/src/arithm.cpp:1253: error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function void cv::arithm_op(const cv::_InputArray&, const cv::_InputArray&, const cv::_OutputArray&, const cv::_InputArray&, int, void (*)(const uchar, size_t, const uchar*, size_t, uchar*, size_t, cv::Size, void*), bool, void*)

CvException [org.opencv.core.CvException:/home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/core/src/arithm.cpp:1253: error: (-209) The operation is not 'array op array'(其中数组具有相同的大小和相同的通道数),也不是函数 void cv::arithm_op(const cv::_InputArray&, const cv::_InputArray&, const cv::_OutputArray&, const cv::_InputArray&, int, void (* )(const uchar, size_t, const uchar*, size_t, uchar*, size_t, cv::Size, void*), bool, void*)


What am I doing wrong?

Thanks for any help in advance.


我究竟做错了什么?

提前感谢您的任何帮助。

EDIT:
If it helps, the 3x3 matrix mat2is the result of Imgproc.getPerspectiveTransformand the rest of the code is as follows:

编辑:
如果有帮助,3x3 矩阵mat2是结果,Imgproc.getPerspectiveTransform其余代码如下:

Mat mat1 = new Mat(3, 1, CvType.CV_64F);
mat1.put(0, 0, 2.0);
mat1.put(1, 0, 0.5);
mat1.put(2, 0, 1.0);

Mat mat3 = new Mat(3, 1, CvType.CV_64F);
Core.multiply(mat2, mat1, mat3);

回答by dennisg

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().

此外,您正在使用方法Core.multiply。在 OpenCv 的文档中它提到:函数乘以计算两个矩阵的每个元素的乘积。如果您正在寻找矩阵乘积,而不是每个元素的乘积,请参阅Core.gemm()

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

该函数gemm(src1, src2, alpha, src3, beta, dest, flags)根据以下函数执行乘法:

dest = alpha * src1 * src2 + beta * src3

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

基本矩阵乘法(在您的情况下)通过以下方式完成:

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

回答by DarkRift

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

反转你的 2 个相乘矩阵: Core.multiply(mat2, mat1, mat3);