天天看點

OpenCV中Mat資料結構的用法

本文參考了博文http://blog.csdn.net/zssureqh/article/details/7599508和http://blog.sina.com.cn/s/blog_79bb01d00101ao58.html以及http://blog.csdn.net/yang_xian521/article/details/7107786

1.介紹兩種初始化的方法:

1)利用循環結構逐個指派

首先配置設定記憶體:Mat m(a,b,CV_64F);//定義Mat型變量m, int型變量a表示行數, int型變量b表示列數, 64表示double型, 32表示float型

for(int i=0;i<m.rows;i++)
	for(int j=0;j<m.cols;j++)
		m.at<double>(i,j)=1.0/(i+j+1);
           

2)利用已知的一維數組指派

double *data = new double[15];
for(int i=0;i<15;i++)
	data[i]=1.0/(i+1);
Mat m(2,3,CV_64F, data);//64:double, 32:float
           

2.對于單個元素的操作,比如輸出第i行第j列的元素,下面給出兩種方式:

cout<<m.at<double>(i,j)<<endl;
cout<<m.ptr<double>(i)[j]<<endl;
           

3.資料類型

CV_8U:8位無符号int

CV_8S:8位有符号int

CV_16U:16位無符号int

CV_16S:16位有符号int

CV_32S:32位有符号int

CV_32F:float

CV_64F:double

繼續閱讀