创建Mat

 1 #include<opencv2/opencv.hpp>  
 2 #include<iostream>  
 3 #include<cassert>  
 4 #include<vector>  
 5 #include<stdio.h>
 6 #include <sys/time.h>
 7 
 8 using namespace cv;  
 9 using namespace std;  
10 
11 double abtic() 
12 {  
13     double result = 0.0;  
14     struct timeval tv;  
15     gettimeofday(&tv, NULL);  
16     result = tv.tv_sec*1000*1000 + tv.tv_usec;  
17     return result;  
18 }
19 
20 int main(int argc, char** argv)
21 {
22     double time = 0.0;
23     Mat srcImage=imread(argv[1], CV_LOAD_IMAGE_COLOR);  
24     Mat B;
25     Mat C;
26 
27     B = Mat(srcImage);
28     printf("B-data address:%p\n", B.data);
29     printf("srcImage-data address:%p\n", srcImage.data);
30 
31     B.copyTo(C);//deep copy
32     printf("C-data address:%p\n", C.data);
33 
34     Mat D(B);
35     printf("D-data address:%p\n", D.data);
36 
37     Mat E;
38     E = B.clone();//deep copy
39     printf("E-data address:%p\n", E.data);
40 
41     Mat F = Mat_<uchar>(B);//same Mat(m)
42     printf("F-data address:%p\n", F.data);
43     //Mat G = Mat_<int>(B);//datatype is different,cat not work
44 
45     time = abtic();
46     Mat G; 
47     B.convertTo(G, CV_64FC3);//矩阵元素数据类型的转换
48     cout << "time(ms):" << (abtic()-time)/1000 << endl;
49     printf("G-data address:%p\n", G.data);
50     cout << "G.channels:" << G.channels() << endl;
51     cout << G.at<Vec3d>(1,1)[0] << endl;
52     cout << G.at<Vec3d>(1,1)[1] << endl;
53     cout << G.at<Vec3d>(1,1)[2] << endl;
54     cout << "G.size:" << G.total() << endl;
55     cout << "G.rows:" << G.rows << endl;
56     cout << "G.cols:" << G.cols << endl;
57 
58     Mat H = Mat_<double>(G);//多通道强转单通道,列数会增加,即高度不变,宽度变为原来的3倍
59     printf("H-data address:%p\n", H.data);
60     cout << "H.channels:" << H.channels() << endl;
61     cout << H.at<double>(1,1) << endl;
62     cout << "H.size:" << H.total() << endl;
63     cout << "H.rows:" << H.rows << endl;
64     cout << "H.cols:" << H.cols << endl;
65     
66     //result print:
67     //B-data address:0x7ff653b2c020
68     //srcImage-data address:0x7ff653b2c020
69     //C-data address:0x7ff653a6b020
70     //D-data address:0x7ff653b2c020
71     //E-data address:0x7ff645b6b020
72     //F-data address:0x7ff653b2c020
73     //G-data address:0x7ff64556a020
74     //H-data address:0x7ff64556a020
75     
76     return 0;
77 }

 

Mat::clone

Creates a full copy of the array and the underlying data.

C++: Mat Mat::clone() const

The method creates a full copy of the array. The original step[] is not taken into account. So, the array copy is a continuous array occupying total()*elemSize() bytes.

1 inline Mat Mat::clone() const
2 {
3     Mat m;
4     copyTo(m);
5     return m;
6 }

 

Mat::copyTo

Copies the matrix to another one.

C++: void Mat::copyTo(OutputArray m) const
C++: void Mat::copyTo(OutputArray m, InputArray mask) const
Parameters:
  • m – Destination matrix. If it does not have a proper size or type before the operation, it is reallocated.
  • mask – Operation mask. Its non-zero elements indicate which matrix elements need to be copied.

The method copies the matrix data to another matrix. Before copying the data, the method invokes

m.create(this->size(), this->type());

so that the destination matrix is reallocated if needed. While m.copyTo(m); works flawlessly, the function does not handle the case of a partial overlap between the source and the destination matrices.

When the operation mask is specified, if the Mat::create call shown above reallocates the matrix, the newly allocated matrix is initialized with all zeros before copying the data.

Mat::convertTo

Converts an array to another data type with optional scaling.

C++: void Mat::convertTo(OutputArray m, int rtype, double alpha=1, double beta=0 ) const
Parameters:
  • m – output matrix; if it does not have a proper size or type before the operation, it is reallocated.
  • rtype – desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.
  • alpha – optional scale factor.
  • beta – optional delta added to the scaled values.

The method converts source pixel values to the target data type. saturate_cast<> is applied at the end to avoid possible overflows:

        m(x,y)=saturate_case<rType>(a(*this)(x,y)+b)

 

posted @ 2016-10-11 09:12  suonikeyinsu  Views(300)  Comments(0Edit  收藏  举报