Please help me to convert opencv matrix multiplication into matlab

Dear concern, I am facing a problem converting an opencv matrix multiplication into matlab which seemed very easy but it is giving me different output. I have two matrices:
transformationMatrix =
-7.04442501068115 0 2239.44677734375
0 1.53200602531433 -2508.06420898438
0 6.14454364776611 -1048.95043945313
0 -0.00281885638833046 0.481214046478271
matrixmultiplier =
317.903421890249 460 100 317.903421890249
220 220 220 350
1 1 1 1
While using opencv the code and output becomes: CvMat *result = cvCreateMat( resultRows, resultCols, CV_32FC1 ); cvMatMul(&transformationMatrix, &matrixmultiplier, result) =
-3.88010430469876e-05 -1000.98872756958 1535.00427627563 -3.88010430469876e-05
-2171.02288341522 -2171.02288341522 -2171.02288341522 -1971.86210012436
302.84916305542 302.84916305542 302.84916305542 1101.63983726501
-0.13893435895443 -0.13893435895443 -0.13893435895443 -0.505385689437389
But while using matlab the code and output becomes: transformedMatrix*matrixmultiplier =
3.18194215651602e-05 -1000.98870849609 1535.00427246094 3.18194215651602e-05
-2171.02294921875 -2171.02294921875 -2171.02294921875 -1971.86206054688
302.849151611328 302.849151611328 302.849151611328 1101.63989257813
-0.13893435895443 -0.13893435895443 -0.13893435895443 -0.50538569688797
I need urgent help to find the error in my coding to understand the discrepancy between two outputs.
Thanks in advance

 Risposta accettata

Your code is fine, it's just rounding error. Note that the differences are smaller, by many orders of magnitude, than the largest elements of the matrix. It's likely that OpenCV and MATLAB do the computation in a different order and that results in these tiny discrepancies.
For more information, see this FAQ, or put "rounding error" into the search box on this page.

3 Commenti

Thanks a lot David. I have found it lately that this error is occurring because of different data types of matlab and opencv. Actually those matrices were floats in opencv but became double in matlab. Solved it via mex but still there is a need to use actual float in matlab. Couldn't find a way to use it except single precision.
You can convert between single and double precision floats in MATLAB using the single and double commands. It's safest and clearest to ensure that all arguments to a function are the same type if it's important to carry out the operation using that type (though in fact MATLAB has rules about implicit conversions).
Thank you again David. Your answer is helping a lot.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by