问题
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
mat1
has size: 3 rows, 3 colsmat2
has size: 3 rows, 1 cols
I want to multiply them to give the product mat3
of size 3 rows, 1 cols.
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*)
What am I doing wrong?
Thanks for any help in advance.
EDIT:
If it helps, the 3x3 matrix mat2
is the result of Imgproc.getPerspectiveTransform
and the rest of the code is as follows:
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);
回答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 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);
回答2:
Reverse your 2 multiplied matrices : Core.multiply(mat2, mat1, mat3);
来源:https://stackoverflow.com/questions/10168058/basic-matrix-multiplication-in-opencv-for-android