StuckInPhDNoMore
StuckInPhDNoMore

Reputation: 2679

Matrix Multiplication wrong in opencv?

I know that there are 3 ways to multiply matrices in opencv, one is:

cvGEMM(M1, M1T, 1, NULL, 0, Mult, 0);

the other:

cvMulTransposed(M1, Mult, 0);

and third:

cvMul(M1, M1T, Mult);

I am basically multiplying a matrix by its transpose. After multiplication the matrices should be symmetrical.. but each of the methods above is giving me an unsymmetrical and completely wrong output.. here is the original (M1) matrix:

rows: 5
   cols: 50
   dt: f
   data: [ 60., 89., 86., 102., 58., 51., 143., 187., 140., 64., 80.,
       169., 184., 172., 67., 90., 174., 191., 175., 41., 97., 86., 171.,
       104., 87., 145., 164., 176., 157., 119., 176., 177., 180., 179.,
       150., 44., 182., 148., 182., 38., 48., 197., 171., 171., 39., 48.,
       169., 163., 141., 39., 93., 149., 169., 184., 56., 120., 166.,
       182., 168., 144., 132., 172., 187., 173., 157., 164., 157., 152.,
       166., 172., 175., 157., 133., 170., 140., 180., 164., 173., 175.,
       152., 178., 176., 166., 143., 158., 177., 186., 172., 138., 141.,
       182., 191., 169., 159., 34., 185., 154., 155., 171., 32., 81.,
       154., 163., 112., 52., 126., 163., 183., 165., 53., 136., 176.,
       184., 174., 51., 148., 173., 178., 139., 160., 158., 147., 163.,
       154., 135., 146., 157., 181., 161., 79., 39., 172., 187., 174.,
       45., 44., 187., 153., 173., 39., 46., 187., 145., 160., 36., 38.,
       177., 155., 150., 37., 81., 154., 163., 112., 52., 126., 163.,
       183., 165., 53., 136., 176., 184., 174., 51., 148., 173., 178.,
       139., 160., 158., 147., 163., 154., 135., 146., 157., 181., 161.,
       79., 39., 172., 187., 174., 45., 44., 187., 153., 173., 39., 46.,
       187., 145., 160., 36., 38., 177., 155., 150., 37., 76., 165.,
       195., 110., 100., 131., 180., 188., 184., 73., 146., 179., 193.,
       183., 96., 117., 148., 146., 185., 97., 104., 147., 157., 174.,
       172., 129., 171., 177., 181., 173., 123., 126., 186., 194., 171.,
       91., 74., 109., 194., 145., 33., 117., 166., 180., 176., 35., 77.,
       155., 152., 177. ]

but multiplying this by its transpose gives this:

rows: 5
   cols: 5
   dt: f
   data: [ 927321., 1014163., 923303., 923303., 947641., 1014163.,
       1260101., 1062130., 1062130., 1102823., 923303., 1062130.,
       1004488., 1004488., 990651., 923303., 1062130., 1004488.,
       1004488., 990651., 947641., 1102823., 990651., 990651., 1116004. ]

the values are unsymmetrical, also how they got so large is what I dont understand?

cvMultransposed and cvGEMM are giving the same result as copied above.. cvMul is giving some runtime error and returning garbage values. any suggestions?

Upvotes: 0

Views: 3347

Answers (2)

etarion
etarion

Reputation: 17131

In addition to the expected large output values (that japreiss already told you about), the matrix that you get IS symmetrical.

I've aligned it for better readability:

reshape(a,5,5)

ans =

      927321     1014163      923303      923303      947641
     1014163     1260101     1062130     1062130     1102823
      923303     1062130     1004488     1004488      990651
      923303     1062130     1004488     1004488      990651
      947641     1102823      990651      990651     1116004

So, there is nothing wrong here, and you should double-check that X is indeed the case if you go and ask why X occurs in future ...

Upvotes: 2

japreiss
japreiss

Reputation: 11251

I can't explain the lack of symmetry, but the magnitude of the numbers is what I'd expect. When you're multiplying matrices you take inner products of a row from the first with a column from the second. Since your rows are 50 elements and your elements are in the 50-200 range, that's going to be around 50*100*100 = 500000 for an element in the output matrix.

Upvotes: 1

Related Questions