1. 程式人生 > >opencv3學習筆記——XML或YAML檔案的寫入

opencv3學習筆記——XML或YAML檔案的寫入

本程式適合vs2017,其他版本需要修改標頭檔案。

Mat類矩陣初始化:1、直接初始化。2、利用陣列初始化。

1、    Mat cameraMatrix = (Mat_<double>(3, 3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);//矩陣直接初始化

2、    double a[5][1] = { 1.0,0.01,-0.001,0,0 };//矩陣通過陣列進行初始化
    Mat distCoeffs = Mat(5, 1, CV_64F,a);

cout中空格可用 cout<<"a"<<a<<""<<"b"<<b<<endl;其中endl相當於換行;

printf的用法:printf("a=%d,b=%d/n",a,b);

#include "pch.h"
#include "iostream"
#include "opencv2/opencv.hpp"
#include "time.h"
using namespace cv;
using namespace std;
int main()
{
	FileStorage fs("test.yaml", FileStorage::WRITE);
	//開始檔案寫入
	fs << "frameCount" << 5;
	time_t rawtime; time(&rawtime);
	Mat cameraMatrix = (Mat_<double>(3, 3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);//矩陣直接初始化
	double a[5][1] = { 1.0,0.01,-0.001,0,0 };//矩陣通過陣列進行初始化
	Mat distCoeffs = Mat(5, 1, CV_64F, a);
	fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
	fs << "feature" << "[";
	for (int i = 0; i < 3; i++)
	{
		int x = rand() % 640;
		int y = rand() % 480;
		uchar lbp = rand() % 256;
		fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
		for (int j = 0; j < 8; j++)
			fs << ((lbp >> j) & 1);
		fs << "]" << "}";
	}
	fs << "]";
	fs.release();
	printf("檔案讀寫完畢,請在工程目錄下檢視生成的檔案");
	getchar();
	return 0;
}