Ask Your Question
0

cv::multiply doesn't work

asked 2018-08-16 01:15:16 -0600

Aiyyaa gravatar image

I tried to multiply matrix, created from a picture, to transposed of this matrix. Transposed matrix created by OpenCv function cv::transpose(). But function cv::multiply doesn't work. I don't know why.

    Mat img = imread( "sample.jpeg", 0 );
    if( img.empty() ) return -1;
    Mat transposed;
    cv::transpose(img, transposed);
    Mat cov;
    cv::multiply(img, transposed, img);

Here is a mistake message: "OpenCV(3.4.1) 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 /opt/opencv-3.4.1/modules/core/src/arithm.cpp, line 659 terminate called after throwing an instance of 'cv::Exception' what(): OpenCV(3.4.1) /opt/opencv-3.4.1/modules/core/src/arithm.cpp:659: 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 arithm_op"

How to fix this error?

edit retag flag offensive close merge delete

Comments

what are you trying to achieve here ? what is the purpose of the multiplication ?

berak gravatar imageberak ( 2018-08-16 01:35:52 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2018-08-16 02:04:48 -0600

berak gravatar image

updated 2018-08-16 02:19:14 -0600

i don't see the reason for the size restriction (though it says so in the docs), but you'll have to work around it by either

// using the * operator, like:
Mat cov = img * transposed;

or:

// full blown gemm:
gemm(img, transposed, 1, noArray(), 0, cov);

however, a conversion to float or double (before the transpose()) is mandatory:

img.convertTo(img, CV_32F);
edit flag offensive delete link more
0

answered 2018-08-16 03:57:31 -0600

LBerger gravatar image

updated 2018-08-16 04:00:23 -0600

I run code with opencv/samples/data/baboon.jpg and there is no exeption with opencv 4.0.0-pre. multiply Calculates the per-element scaled product of two arrays. img size must be square
Are you sure that sample.jpeg size is square?

As @berak wrote you should convert img in float before multiply if you want to get a good result and matrix product is * or gemm

edit flag offensive delete link more

Comments

problem is with non-square Mat's. like:

Mat a(1,3,CV_32F);
multiply(a, a.t(), a);
berak gravatar imageberak ( 2018-08-16 04:01:46 -0600 )edit

I don't understand in a(1,3,CV_32F) and a.t() size are not equal then multiply send an exception ( per-element scaled product of two arrays)

LBerger gravatar imageLBerger ( 2018-08-16 04:42:36 -0600 )edit

A*A.t() is a common operation, no ? it just can't be done with multiply(), due to the size restriction.

berak gravatar imageberak ( 2018-08-16 04:48:04 -0600 )edit

My english is too bad :

Mat a(1,3,CV_32F);
multiply(a, a.t(), b);

What is b size?

"A*A.t() is a common operation" it is pseudo-inverse

I understand your comment as

Mat a(1,4,CV_32F);
Mat b( 2,2,CV_32F);
multiply(a, b, c);

is possible because number of elements in a is equal to number of elements in b

LBerger gravatar imageLBerger ( 2018-08-16 05:04:48 -0600 )edit

more like:

Mat a(1,4,CV_32F);
Mat b(4,1,CV_32F);
Mat outer_product = a * b;  // result is 4x4
berak gravatar imageberak ( 2018-08-16 05:10:19 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-08-16 01:15:16 -0600

Seen: 2,590 times

Last updated: Aug 16 '18