Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix multiplication in OpenCV

Tags:

opencv

I have two Mat images in OpenCV:

Mat ft = Mat::zeros(src.rows,src.cols,CV_32FC1); Mat h = Mat::zeros(src.rows,src.cols,CV_32FC1); 

Both images are the same dimension and are calculated from a single source image.

I would like to multiply these two images but have tried using both

Mat multiply1 = h*ft;  Mat multiply2; gemm(h,ft,1,NULL,0,multiply2); 

but both result in the following assertion failure:

OpenCV Error: Assertion failed (a_size.width == len) in unknown function, file ...matmul.cpp Termination called after throwing 'cv::exception'

What am I doing wrong?

like image 340
bistaumanga Avatar asked Jun 07 '12 16:06

bistaumanga


People also ask

How do you multiply matrices in OpenCV?

In OpenCV, we can multiply two images using the asterisk operator. Images are stored in a matrix in OpenCV, so we can use the asterisk operator to multiply two matrices.

What is matrix multiplication used for?

Matrix multiplication is probably the most important matrix operation. It is used widely in such areas as network theory, solution of linear systems of equations, transformation of co-ordinate systems, and population modeling, to name but a very few.

What is matrix in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.


1 Answers

You say that the matrices are the same dimensions, and yet you are trying to perform matrix multiplication on them. Multiplication of matrices with the same dimension is only possible if they are square. In your case, you get an assertion error, because the dimensions are not square. You have to be careful when multiplying matrices, as there are two possible meanings of multiply.

Matrix multiplication is where two matrices are multiplied directly. This operation multiplies matrix A of size [a x b] with matrix B of size [b x c] to produce matrix C of size [a x c]. In OpenCV it is achieved using the simple * operator:

C = A * B 

Element-wise multiplication is where each pixel in the output matrix is formed by multiplying that pixel in matrix A by its corresponding entry in matrix B. The input matrices should be the same size, and the output will be the same size as well. This is achieved using the mul() function:

output = A.mul(B); 
like image 164
Chris Avatar answered Sep 19 '22 03:09

Chris