转载于:https://blog.csdn.net/jeakon/article/details/41942197
参考文章:http://www.cnblogs.com/tornadomeet/archive/2012/07/19/2599376.html
实验说明: (引用)
本文主要讲一些opencv 2.0版本后出现的Mat矩形类,主要是参考opencv自带doc文件夹下的tutiol教材。通过这次实验觉得用Mat的话下面几点需要特别注意(在代码中可以体现出来):
-
#include
-
#include
-
#include
-
#include
-
-
using
namespace
std;
-
using
namespace cv;
-
-
int demo_Mat()
-
{
-
int next_num =
1;
-
char tmp;
-
while(next_num<
7)
-
{
-
cout<<
"-------- "<
" --------"<<
endl;
-
waitKey(
0);
-
-
switch (next_num)
-
{
-
case
1:
-
{
-
Mat M( 2, 2, CV_8UC3, Scalar(0,255,0) );
//其实是2*6的矩阵,因为每个元素有3个通道。
-
cout<<
"create by using the constructor..."<<
endl;
-
cout<<
"M = \n "<
endl;
-
-
M.create(
4,
4, CV_8UC(
2));
//括号里面的2表示2通道
-
cout<<
"create by using create function..."<<
endl;
-
cout<<
"M = \n "<
endl;
-
break;
-
}
//当case语句里面变量定义时,需要用括号括起来,否则会报错
-
case
2:
-
{
-
int sz[
3] = {
2,
2,
2};
-
Mat L( 3, sz, CV_8UC(1), Scalar::all(0) );
-
cout<<
"create multidimensional matrix..."<<
endl;
-
// cout<<"L = "<
-
break;
-
}
-
case
3:
-
{
-
Mat E = Mat::eye(
4,
4, CV_64F);
-
Mat O = Mat::ones(
2,
3, CV_32F);
-
Mat Z = Mat::zeros(
3,
3, CV_8UC1);
-
cout<<
"using matlab stytle..."<<
endl;
-
cout<<
"E = \n "<
endl;
-
cout<<
"O = \n "<
endl;
-
cout<<
"Z = \n "<
endl;
-
break;
-
}
-
case
4:
-
{
-
Mat C =(Mat_<
double>(
3,
3)<<
0,
-1,
0,
-1,
5,
-1,
0,
-1,
0);
//直接赋初始值的方法
-
Mat row_clone = C.row(
1).clone();
-
cout<<
"create 3*3 double-precision identity matrix..."<<
endl;
-
cout<<
"C = \n "<
endl;
-
cout<<
"row_clone = "<
endl;
-
break;
-
}
-
case
5:
-
{
-
Mat R = Mat(
3,
2, CV_8UC3 );
-
randu( R, Scalar::all(
0), Scalar::all(
255) );
-
cout<<
"fill a matrix with rand numbers..."<<
endl;
-
cout<<
"R (default) = "<
endl;
-
cout<<
"demonstrate the output formating options..."<<
endl;
-
cout<<
"R (python) = \n "<
"python")<<
endl;
-
cout<<
"R (numpy) = \n "<
"numpy")<<
endl;
//numpy是一个用python实现的科学计算包
-
cout<<
"R (csv) = \n "<
"csv")<<
endl;
//csv,逗号分隔符
-
cout<<
"R (c) = \n " <
"C")<<
endl;
-
break;
-
}
-
case
6:
-
{
-
cout<<
"the point format output..."<<
endl;
-
-
Point2f P1(5, 1);
-
cout<<
"Point (2D) = "<
endl;
-
-
Point3f P2(4, 5, 6);
-
cout<<
"Point (3D) = "<
endl;
-
-
vector<
float>v;
-
v.push_back( (
float)CV_PI);
-
v.push_back(
2 );
//push_back为在其尾部加入一个数据
-
v.push_back(
3.01f );
-
cout<<
"vector of float: = "<
endl;
//vector数据是没法单独输出的,因此可以借助mat输出
-
-
int N =
20;
-
vector
vPoints(N);
//vector可以用变量定义其长度,比数组好用
-
for(
size_t E =
0; E < vPoints.size(); ++E )
//size_t其实就是一个unsigned int类型
-
vPoints[E] = Point2f((
float)(E*
5), (
float)(E%
7));
-
cout<<
"vPoints[] = \n"<
endl;
//但是vector点确实可以直接输出的,因为这时候的vector本身就是
-
//一个多维(至少2维)Mat了
-
break;
-
}
-
default:
-
break;
-
}
-
next_num++;
-
}
-
return
0;
-
}
-
运行结果: