OpenCV學習筆記------基本數據結構

開發環境配置參考: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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章