当前位置:   article > 正文

cv::Mat一些基本初始化_cv::mat 初始化

cv::mat 初始化

转自http://blog.sina.com.cn/s/blog_79bb01d00101ao58.html

本文讲解Mat 的一些基本的初始化


// m为3*5的矩阵,float型的单通道,把每个点都初始化为1

Mat m(3, 5, CV_32FC1, 1);

或者 Mat m(3, 5, CV_32FC1, Scalar(1));

cout<<m;

输出为:

[1, 1, 1, 1, 1;
  1, 1, 1, 1, 1;
  1, 1, 1, 1, 1]

 

// m为3*5的矩阵,float型的2通道,把每个点都初始化为1 2

 Mat m(3, 5, CV_32FC2, Scalar(1, 2));

cout<<m;

输出为

[1, 2, 1, 2, 1, 2, 1, 2, 1, 2;
  1, 2, 1, 2, 1, 2, 1, 2, 1, 2;
  1, 2, 1, 2, 1, 2, 1, 2, 1, 2]

 

// m为3*5的矩阵,float型的3通道,把每个点都初始化为1 2 3

Mat m(3, 5, CV_32FC3, Scalar(1, 2, 3));

cout << m;

输出为

[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3;
  1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3;
  1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

 

// 从已有的数据源初始化

double *data = new double[15];

for (int i = 0; i < 15; i++)

{

   data[i] = 1.2;

}

Mat m(3, 5, CV_32FC1, data);

cout << m;

输出为:

[1.2, 1.2, 1.2, 1.2, 1.2;
  1.2, 1.2, 1.2, 1.2, 1.2;
  1.2, 1.2, 1.2, 1.2, 1.2]

 

如果接着

delete [] data;

cout << m;

输出为:

[-1.456815990147463e+144, -1.456815990147463e+144, -1.456815990147463e+144, -1.456815990147463e+144, -1.456815990147463e+144;
  -1.456815990147463e+144, -1.456815990147463e+144, -1.456815990147463e+144, -1.456815990147463e+144, -1.456815990147463e+144;
  -1.456815990147463e+144, -1.456815990147463e+144, -1.456815990147463e+144, -1.456815990147463e+144, -1.456815990147463e+144]

可见,这里只是进行了浅拷贝,当数据源不在的时候,Mat里的数据也就是乱码了。

 

// 从图像初始化 

 Mat m = imread("1.jpg", CV_LOAD_IMAGE_GRAYSCALE);

 cout<< "channels ="<<m.channels()<<endl;

 cout << "cols ="<<m.cols<<endl;

 cout << "rows ="<<m.rows<<endl;

 cout << m;

输出为:

channels =1
cols =13
rows =12
[179, 173, 175, 189, 173, 163, 148, 190, 68, 14, 19, 31, 22;
  172, 172, 172, 180, 172, 177, 162, 190, 64, 13, 19, 30, 17;
  177, 180, 176, 175, 169, 184, 165, 181, 58, 12, 23, 38, 25;
  181, 183, 178, 178, 170, 181, 163, 182, 52, 8, 23, 37, 23;
  176, 173, 173, 184, 175, 178, 164, 195, 60, 14, 24, 35, 16;
  179, 175, 176, 187, 176, 175, 158, 191, 70, 21, 28, 37, 20;
  182, 183, 180, 184, 174, 179, 155, 174, 54, 1, 5, 15, 2;
  173, 182, 178, 176, 173, 191, 165, 169, 157, 101, 100, 107, 93;
  181, 182, 180, 177, 177, 177, 171, 162, 183, 185, 186, 185, 182;
  178, 180, 179, 177, 178, 179, 174, 167, 172, 174, 175, 174, 172;
  175, 178, 179, 178, 180, 182, 179, 173, 172, 174, 175, 175, 174;
  175, 179, 181, 180, 181, 183, 181, 177, 178, 180, 182, 183, 182]

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/886829
推荐阅读
相关标签
  

闽ICP备14008679号