Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of cv::Matx

Tags:

opencv

matrix

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?

like image 901
Jav_Rock Avatar asked Jun 08 '12 07:06

Jav_Rock


People also ask

What is CV_64F?

CV_64F is the same as CV_64FC1 . So if you need just 2D matrix (i.e. single channeled) you can just use CV_64F. EDIT. More generally, type name of a Mat object consists of several parts.

What is CV_8U?

CV_8U is unsigned 8bit/pixel - ie a pixel can have values 0-255, this is the normal range for most image and video formats.

What is Mat CPP?

Mat is basically a class with two data parts : the matrix header (containing information such as the size of the matrix, the method used for storing, at which address is the matrix stored, and so on) and a pointer to the matrix containing the pixel values (taking any dimensionality depending on the method chosen for ...


2 Answers

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.

like image 63
dom Avatar answered Oct 07 '22 13:10

dom


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.

like image 27
user1460044 Avatar answered Oct 07 '22 13:10

user1460044