woo*_*365 7 java android opencv matrix multiplication
我可能在这里非常愚蠢但是我在使用OpenCV for Android做一些基本的Mat乘法时遇到了麻烦.
我有两个相同类型的Mat, CV_64F
mat1有大小:3行,3列
mat2有大小:3行,1列
我想将它们相乘以得到mat3大小为3行,1列的产品.
我尝试过使用:
Mat mat3 = new Mat(3, 1, CvType.CV_64F);
Core.multiply(mat1, mat2, mat3);
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
CvException [org.opencv.core.CvException:/home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/core/src/arithm.cpp:1253:错误:( - 209)该操作既不是'数组操作array'(其中数组具有相同的大小和相同的通道数),也没有'array op scalar',也没有'scalar 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*)
我究竟做错了什么?
在此先感谢您的帮助.
编辑:
 
如果有帮助,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);
Run Code Online (Sandbox Code Playgroud)
    您现在基本上尝试执行以下操作:
[ 0 ]   [ 0 1 2 ]
[ 1 ] * [ 3 4 5 ]
[ 2 ]   [ 6 7 8 ]
Run Code Online (Sandbox Code Playgroud)
在这里*是乘法.矩阵乘法不能以这种方式完成.在这里阅读矩阵乘法.
您要执行的操作是:
            [ 0 1 2 ]
[ 0 1 2 ] * [ 3 4 5 ]
            [ 6 7 8 ]
Run Code Online (Sandbox Code Playgroud)
要使代码正常工作,请进行以下更改:
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
Run Code Online (Sandbox Code Playgroud)
此外,您正在使用该方法Core.multiply.在OpenCv的文档中,它提到:函数multiply计算两个矩阵的每元素乘积.如果您正在寻找矩阵产品,而不是每个元素的产品,请参阅Core.gemm().
该函数gemm(src1, src2, alpha, src3, beta, dest, flags)根据以下函数执行乘法:
dest = alpha * src1 * src2 + beta * src3
Run Code Online (Sandbox Code Playgroud)
基本矩阵乘法(在您的情况下)通过以下方式完成:
Core.gemm(mat2, mat1, 1, NULL, 0, mat3, 0);
Run Code Online (Sandbox Code Playgroud)