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;