OpenCV学习笔记------基本数据结构
程序员文章站
2022-07-12 15:48:59
...
开发环境配置参考:vs2017+OpenCV~
//图像数据结构实例
#include"opencv2/core/core.hpp"
#include<iostream>
using namespace cv;
using namespace std;
int main(int, char**)
{
//用构造函数建立数据 2*2的矩阵 里面的元素为8位 U表示(unsigned)为无符号数 C表示(channe)通道数有3个
Mat M(2,2,CV_8UC3,Scalar(0, 0, 255));
cout << "M = " << endl << " " << M << endl;
//用create函数建立数据
M.create(2, 2, CV_8UC(2));
cout << "M = " << endl << " " << M << endl;
//建立多维矩阵 无法用<<输出
int sz[3] = { 2, 2, 2 };
Mat L(3, sz, CV_8UC(1), Scalar::all(0));
//用MATLAB风格的眼建立数据
Mat E = Mat::eye(3, 3, CV_64F);
cout << "E = " << endl << " " << E << endl;
//数据都是1
Mat O = Mat::ones(2, 2, CV_32F);
cout << "O = " << endl << " " << O << endl;
//数据都是0
Mat Z= Mat::zeros(2, 2, CV_8UC1);
cout << "Z = " << endl << " " << Z << endl;
//建立3*3双精度矩阵,值由<<输入
Mat C = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
cout << "C = " << endl << " " << C << endl;
//复制第一行数据
Mat RowClone = C.row(0).clone();
cout << "RowClone = " << endl << " " << RowClone << endl;
//以随机数值填入矩阵内
Mat R = Mat(3, 2, CV_8UC3);
randu(R, Scalar::all(0),Scalar::all(255));
//展示各种输出格式选项
cout << "R (default) = " << endl << R << endl;
cout << "R (python) = " << endl << format(R, "python") << endl;
cout << "R (numpy) = " << endl << format(R, "numpy") << endl;
cout << "R(csv) = " << endl << format(R, "csv") << endl;
cout << "R(C) = " << endl << format(R, "C") << endl;
//图像中二维的点
Point2f P(5,1);
cout << "Point (2D)" << P << endl;
//图像中三维的点
Point2f P3f(5,1);
cout << "Point(3D)" << P3f << endl;
vector<float> v;
v.push_back((float) CV_PI);
v.push_back(2);
v.push_back(3.01f);
cout << "浮点向量矩阵 = " << Mat(v) << endl << endl;
vector<Point2f> vPoints(5);
for (size_t i = 0; i < vPoints.size(); ++i)
vPoints[i] = Point2f((float)(i * 5), (float)(i % 7));
cout << "二维图点向量 = " << vPoints << endl;
getchar();
return 0;
}
上一篇: C++面试知识点汇总
下一篇: 知识点汇总