Ask Your Question

MatrixXf eigen and Mat opencv

asked 2014-06-29 07:16:43 -0500

sara gravatar image

Hi ,

what is the difference between using MatrixXf eigen and Mat opencv ? I know that each one offer different class and functionality . But my main question is : if it is possible to use both structure in the code which of them is prefered for increasing the matrix computation speed ?

Thansk in advance Sarah

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-08-16 15:31:22 -0500

BloodAxe gravatar image

updated 2020-05-30 10:42:14 -0500

Well, Eigen differs from OpenCV matrices in two things:

  • Eigen use column-major ordering by default.
  • Eigen use aligned memory allocators by default.

How this affect your code

Column-major ordering will lead to incorrect result if you pass Eigen data directly to OpenCV functions. Your data will be treated as 'transposed'. Depending on your application you can either declare your Eigen types with Eigen::RowMajor option or transpose OpenCV matrix that you create from Eigen data.

Aligned memory allocators allows faster memory access during vector load/stores. This reduce memory access latency and improve performance. So if your code is math-heavy using Eigen is a really good idea. However if you map OpenCV types to Eigen using Eigen::Map remember that it assumes there is no alignment for external data.

How to map Eigen to OpenCV and back

I have made a tutorial post on mapping Eigen plain matrices and expressions to OpenCV types. In my project I switched from OpenCV to Eigen for matrix operations, so I needed a handy wrapper to pass my Eigen data to OpenCV: I hope this post will help you.

edit flag offensive delete link more


The link doesn't work

AnkilP gravatar imageAnkilP ( 2018-07-24 19:02:38 -0500 )edit

Question Tools


Asked: 2014-06-29 07:16:43 -0500

Seen: 2,148 times

Last updated: May 30 '20