Ask Your Question

# Product of two matrices

Hi,

I use OpenCV 2.4.4 with Java. I have problem with simple multiplication of two matrices. I define it in my code in following way:

Mat m1 = Mat.ones(3, 3, CvType.CV_16SC1);

Mat m2 = Mat.ones(3, 3, CvType.CV_16SC1);

In case if I multiply:

Mat m3 = m1 * m2;

I achieved error that operator * is undefined.

I know that OpenCv for Java provides function mul and i can use it:

Mat m3 = m1.mul(m2);

but in this way I receive only product of elementwise multiplication what is not my objective.

Is there some replacement function which allow me to multiply two matrices like * operator?

Thanks in advance :)

edit retag close merge delete

## Comments

1

i think you need float mat's for this

( 2013-04-05 01:28:25 -0500 )edit

Hi, When I define my matrix like: Mat m1 = Mat.ones(3, 3, CvType.CV_32FC1); I achieved error that I am not able to use operator * too. There have to be another way to solve it...

( 2013-04-05 02:31:40 -0500 )edit

Thank You berek for interest of my problem. The problem was with CvType and used gemm function. Tomorrow I will present final solution, today I am not able because of some rules of forum.

( 2013-04-05 04:27:57 -0500 )edit

## 2 answers

Sort by ยป oldest newest most voted

Ok, below I present solution of my problem:

Mat m1 = Mat.zeros(3, 3, CvType.CV_32F);

Mat m2 = Mat.ones(3, 3, CvType.CV_32F);

Mat m3 = Mat.ones(3, 3, CvType.CV_32F);

Core.gemm(m2, m3, 1, new Mat(), 0, m1, 0);


My problem was connected with wrong type used in declaration of matrices and to multiply them we can use gemm function as it is presented above.

Thanks for help:)

more

## Comments

Glad to hear that you resolved your issue.

( 2013-04-08 12:56:23 -0500 )edit

Thanks Dominik for the solution. Solved my issue as well.

( 2014-01-20 20:26:03 -0500 )edit
more

## Comments

Hi, Yes, I have to use gemm function like this:

Core.gemm(m2, m3, 1,null, 0, m1,0);

But after that I achieved error: Exception in thread "main" java.lang.NullPointerException at org.opencv.core.Core.gemm(Core.java:3647)

Any suggestion how can I solve that?

( 2013-04-05 02:28:55 -0500 )edit

oh, sorry, i can only speak of c++ . but i looked up gemm, and both m2 and m3 must be float types. also, you probably have to supply a valid result Mat there, too (the null pointer)

( 2013-04-05 02:46:29 -0500 )edit
2

Thank You Guanta for interest of my problem. The problem was with CvType and used gemm function. Tomorrow I will present final solution, today I am not able because of some rules of forum.

( 2013-04-05 04:28:38 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2013-04-04 17:04:56 -0500

Seen: 11,920 times

Last updated: Apr 08 '13