Jav_Rock
Jav_Rock

Reputation: 22245

Advantages of cv::Matx

I noticed that a new data structure cv::Matx was added to the new OpenCV version, intended for small matrices of known size at compilation time, for example

cv::Matx31f  // matrix 3x1 of float type

Checking the documentation I saw that most of matrix operations are available, but still I don't see the advantages of using this new type instead of the old cv::Mat.

When should I use Matx instead of Mat?

Upvotes: 32

Views: 11179

Answers (4)

Daniel Danciu
Daniel Danciu

Reputation: 143

There are 2 other reasons why I prefer Matx to Mat:

  1. Readability: people reading the code can immediately see the size of the matrices, for example:

    cv::Matx34d transform = ...;
    

    It's clear that this is a 3x4 matrix, so it contains a 3D transformation of type (R,t), where R is a rotation matrix (as opposed to say, axis-angle). Similarly, accessing an element is more natural with transform(i,j) vs transform.at<double>(i,j).

  2. Easy debugging. Since the elements for Matx are allocated on the stack in an array of known length, IDEs or debuggers can display the entire contents nicely when stepping through the code.

Upvotes: 6

Яois
Яois

Reputation: 3858

This is a late late answer, but it is still an interesting question!

dom's answer is quite accurate, and the heap/stack reference in user1460044's is also interesting.

From a practical point of view, I wouldn't use Matx (or Vec), except if it were completely necessary. The major advantages of Matx are

  1. Using the stack (efficient![1])
  2. Initialization.

The problem is, at the end you will have to move your Matx data to a Mat to do most of stuff, and so, you will be back at the heap again. On the other hand, the "cool initialization" of a Matx can be done in a normal Mat:

// Matx initialization:
Matx31f A(1.f,2.f,3.f);
// Mat initialization:
Mat B = (Mat_<float>(3,1) << 1.f, 2.f, 3.f);

Also, there is a difference in initialization (beyond the heap/stack) stuff. If you try to put 5 values into the Matx31, it will crash (runtime exception), while calling the Mat_::operator<< with 5 values will only store the first three.

[1] Efficient if your program has to create lots of matrices of less than ~10 elements. In that case use Matx matrices.

Upvotes: 8

user1460044
user1460044

Reputation: 369

Short answer: cv::Mat uses the heap to store its data, while cv::Matx uses the stack.

A cv::Mat uses dynamic memory allocation (on the heap). This is appropriate for big matrices (like images) and lets you do things like shallow copies of a matrix, which is the default behavior of cv::Mat.

However, for the small matrices that cv::Matx is designed for, heap allocation would be very expensive compared to doing the same thing on the stack. I have seen a block of math reduce processing time by over 75% by switching to using stack-allocated types (e.g. cv::Point and cv::Matx) instead of cv::Mat.

Upvotes: 13

dom
dom

Reputation: 11972

It's about memory management and not wasting (in some cases important) memory or just reservation of memory for an object you'll use later.

That's how I understand it – may be someone else can give a better explanation.

Upvotes: 11

Related Questions