1
votes

I'm multiplying two matrices with OpenCV, A in NxM and B is MxP.

According to the documentation:

All the arrays must have the same type and the same size (or ROI size). For types that have limited range this operation is saturating.

However, by the theory of matrix multiplication:

Assume two matrices are to be multiplied (the generalization to any number is discussed below). If A is an n×m matrix and B is an m×p matrix, the result would be AB of their multiplication is an n×p matrix defined only if the number of columns m in A is equal to the number of rows m in B.

shouldn't this code be working?

- (CvMat *) multMatrix:(CvMat *)AMatrix BMatrix:(CvMat *)BMatrix 
{
  CvMat *result = cvCreateMat(AMatrix->rows, BMatrix->cols, kMatrixType);
  cvMul(AMatrix, BMatrix, result, 1.0);
  return result;
}

I get the following exception:

OpenCV Error: Assertion failed (src1.size == dst.size && src1.channels() == dst.channels()) in cvMul, file /Users/Aziz/Documents/Projects/opencv_sources/trunk/modules/core/src/arithm.cpp, line 2728

kMatrixType is CV_32F, A is 6x234, B is 234x5 and result is 6x5...

Am I doing something wrong? Or is this an OpenCV restriction to matrix multiplication ?

1
I don't wanna be rude, but dammit! Documentation clearly states, that the matrices has to be same size, so don't ask why isn't yout code working.jnovacho

1 Answers

3
votes

You are doing element-wise multiplication with cvMul.

You should look at cvMatMul for doing proper matrix multiplication.

http://opencv.willowgarage.com/wiki/Matrix_operations