Runtime Error on OpenCV Core.Multiply Method (Java)

Hello,

Some advice here would be appreciated, been looking at this for a number of weeks and I am stumped. Posted to Forum @ OpenCV.org as well, but format was messed up when cutting an pasting.

Error given on Matrix Multiply OpenCV: Core.Multiply() Function Goal: After running SolvePnP doing simple Matrix Multiply with the following matrix dimensions : [3x3]*[3x1] = [3x1]
Error states that the array sizes do not match, but they do according to Matrix Multiplication Rules. Should I be using a different method, is Core.Multiply reservered for multi-channel matricies?

*** Log File Excerpt showing Mat Dumps and Error Message ***

LmVSM_k_RotInv : [-0.7248091196769779, -0.6670117571281554, 0.1724733483350253; -0.6771860665621979, 0.7358117890023557, -0.0002059591509784506;
-0.1267705458184306, -0.1169458294167176, -0.9850141631950949]
LmVSM_k_TransVectNeg : [-7.324298797523894; -2.210406560977737; 1.162266668817123]
VmVSM_k_ImgPlaneZeroWorld : [1.429164061258389e-101; 1.840923297268401e-80; 8.843939164890396e+247]

OpenCV(3.4.4) Error: Sizes of input arguments do not match (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 arithm_op, file /var/lib/jenkins/workspace/OpenCV/OpenCV - Release/arm/opencv/modules/core/src/arithm.cpp, line 660 Unhandled exception: CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.4) /var/lib/jenkins/workspace/OpenCV/OpenCV - Release/arm/opencv/modules/core/src/arithm.cpp:660: error: (-209:Sizes of input arguments do not match) 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 ‘arithm_op’]

*** Code Snippets ***

private Mat VmVSM_k_RotVect = new Mat(3,1,CvType.CV_64F);
private Mat VmVSM_k_TransVect = new Mat(3,1,CvType.CV_64F);
private Mat VmVSM_k_Rot = new Mat(3,3,CvType.CV_64F);
private Mat VmVSM_k_ImgPlaneZeroWorld = new Mat(3,1,CvType.CV_64F);
.
.
.
Mat LmVSM_k_RotInv = new Mat(3,3,CvType.CV_64F);
Mat LmVSM_k_TransVectNeg = new Mat(3,1,CvType.CV_64F);
Mat LmVSM_k_ZerosVect = new Mat(3,1,CvType.CV_64F);
.
.
. /* Initialize Local Matricies for prior to Angle2 calculation /
LmVSM_k_RotInv.zeros(3,3,CvType.CV_64F);
LmVSM_k_TransVectNeg.zeros(1,3,CvType.CV_64F);
LmVSM_k_ZerosVect.zeros(3,1,CvType.CV_64F);
.
.
.
/
Angle2: Calculate horiz angle between the target perpendicular and the robot-target line */
Calib3d.Rodrigues(VmVSM_k_RotVect, VmVSM_k_Rot);
Core.transpose(VmVSM_k_Rot,LmVSM_k_RotInv);
Core.scaleAdd(VmVSM_k_TransVect, -1.0, LmVSM_k_ZerosVect, LmVSM_k_TransVectNeg);
System.out.println("LmVSM_k_RotInv : " + LmVSM_k_RotInv.dump());
System.out.println("LmVSM_k_TransVectNeg : " + LmVSM_k_TransVectNeg.dump());
System.out.println("VmVSM_k_ImgPlaneZeroWorld : " + VmVSM_k_ImgPlaneZeroWorld.dump());
Core.multiply(LmVSM_k_RotInv, LmVSM_k_TransVectNeg, VmVSM_k_ImgPlaneZeroWorld); (Error in this last line here apparently)

Thanks

Wrong category my guy, try Technical

Thanks @tkdberger for calling that to my attention. I’ll post to the correct sub.

Core.Multiply() is not a matrix multiplication. Per the docs, it is a per-element multiplication, so both input Mats need to be the same size. If you want a matrix multiplication, use cv.gemm().

1 Like

@Peter_Johnson. This was exactly what I needed. I reviewed both the Library docs for Core.Multiply and Gemm again and it is clear. Previously, I looked at Gemm and thought it was more than I needed due to the alpha scalar, and addition of 3rd matrix and scalar. I then did a search for more on Matrix Multiply functions, and examples showed Core.Multiply only involving two matricies and thought that would work. The C+ and Python Library doc is much more useful than the Java Library doc, as I don’t recall that the Java docs defining the contextual details. Seems like that is a common theme that the Java library doc lacks elaboration on the functions.

Thank You so much for responding and pointing out the difference.