Construct mat with Vec type variables (solve a problem)

Source: Internet
Author: User

Http://blog.csdn.net/zssureqh/article/details/7598750

The content of this article is as follows: Construct mat with Vec type variables

On the 25 pages of opencv2.3.1 English official manual, there is an example code:

STD: vector <point3f> VEC;
...

Mat pointmat = MAT (VEC). // convert vector to mat, O (1) Operation
Reshape (1). // make nx3 1-channel matrix out of Nx1 3-channel.
// Also, an O (1) Operation
T (); // Finally, transpose the nx3 matrix.
// This involves copying all the elements

First, when we construct a mat type using MAT (VEC), if we directly use mat in mat. when at <float> (I, j) is used to output elements, I find that it only outputs the first coordinate value of point3f, a three-dimensional space point. What is the problem?

Let's Insert the breakpoint in mat pointmat = MAT (VEC). Line by F9, and then press F11 to enter the program to view the truth:

The program enters the position in the mat. HPP file:

 

Then we carefully analyze the Code:

Template <typename _ TP> inline mat: MAT (const vector <_ TP> & VEC, bool copydata)
: Flags (magic_val | datatype <_ TP>: type | cv_mat_cont_flag), // initialize the list
Dims (2), rows (INT) Vec. Size (), cols (1), data (0), refcount (0), // initialize the list
Datastart (0), dataend (0), Allocator (0), size (& rows) // initialize the list

{
If (VEC. Empty ())
Return;
If (! Copydata)
{
Step [0] = step [1] = sizeof (_ TP );
Data = datastart = (uchar *) & VEC [0];
Datalimit = dataend = datastart + rows * step [0];
}
Else
MAT (INT) Vec. Size (), 1, datatype <_ TP>: type, (uchar *) & VEC [0]). copyto (* This );
}

 

As shown in red, the row of Mat data is equal to VEC. Size (). The element type of VEC is point3f. In fact, the number of rows of Mat generated is 1. naturally, when we use loops to display mat data, only one element is displayed. If we want to display three coordinates: so we can only use data (char * type pointer pointing to real data to operate)

As follows:

Cout <* (float *) pointmat. Data) <Endl;
Cout <* (float *) pointmat. Data + 1) <Endl;

Cout <* (float *) pointmat. Data + 2) <Endl;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.