图像处理之输入输出XML和YAML文件2

131 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第12天

3 示例程序:XML和YAML文件的写入

让我们先看一个关于XML或YAML文件的写入实例,示例代码如下:

//---------------------------------【头文件、命名空间包含部分】-------------------------------
//		描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include "opencv2/opencv.hpp"  
#include <time.h>  
using namespace cv;  


//-----------------------------------【ShowHelpText( )函数】----------------------------------
//		 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
void ShowHelpText()
{
	//输出欢迎信息和OpenCV版本
	printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
	printf("\n\n\t\t\t此为本书OpenCV2版的第29个配套示例程序\n");
	printf("\n\n\t\t\t   当前使用的OpenCV版本为:" CV_VERSION );
	printf("\n\n  ----------------------------------------------------------------------------\n");
}


//-----------------------------------【main( )函数】--------------------------------------------
//	描述:控制台应用程序的入口函数,我们的程序从这里开始
//-----------------------------------------------------------------------------------------------
int main( )  
{  
	//改变console字体颜色
	system("color 5F"); 

	ShowHelpText();

	//初始化
	FileStorage fs("test.yaml", FileStorage::WRITE);  

	//开始文件写入
	fs << "frameCount" << 5;  
	time_t rawtime; time(&rawtime);  
	fs << "calibrationDate" << asctime(localtime(&rawtime));  
	Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);  
	Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);  
	fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;  
	fs << "features" << "[";  
	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("\n文件读写完毕,请在工程目录下查看生成的文件~");
	getchar();

	return 0;  
}  

运行此程序,会在工程目录下程序一个名为“test.yaml”的文件,用记事本或者其他文本编辑器比如notepad++打开它,可以发现写入的YAML文档内容如下。

2.png

%YAML:1.0

frameCount: 5

calibrationDate: "Sat Nov 29 15:34:46 2014\n"

cameraMatrix: !!opencv-matrix

rows: 3

cols: 3

dt: d

data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]

distCoeffs: !!opencv-matrix

rows: 5

cols: 1

dt: d

data: [ 1.0000000000000001e-001, 1.0000000000000000e-002, -1.0000000000000000e-003, 0., 0. ]

features:

  • { x:41, y:227, lbp:[ 0, 1, 1, 1, 1, 1, 0, 1 ] }

  • { x:260, y:449, lbp:[ 0, 0, 1, 1, 0, 1, 1, 0 ] }

  • { x:598, y:78, lbp:[ 0, 1, 0, 0, 1, 0, 1, 0 ] }

如果我们修改上面代码的 FileStorage fs(“test.yaml”,FileStorage::WRITE) 这一句代码,将其中的“test.yaml”的后缀换为xml、yml、txt甚至doc,都是可 以得到运行结果的,大家不妨尝试一下。

4 示例程序:XML和YAML文件的读取

接着,我们一起看读操作应该如何进行,完整的源代码如下。

//---------------------------------【头文件、命名空间包含部分】-------------------------------
//		描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------       
#include "opencv2/opencv.hpp"  
#include <time.h>  
using namespace cv;  
using namespace std;  


//-----------------------------------【ShowHelpText( )函数】----------------------------------
//		 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
void ShowHelpText()
{
   //输出欢迎信息和OpenCV版本
   printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
   printf("\n\n\t\t\t此为本书OpenCV2版的第30个配套示例程序\n");
   printf("\n\n\t\t\t   当前使用的OpenCV版本为:" CV_VERSION );
   printf("\n\n  ----------------------------------------------------------------------------\n\n");
}

int main( )  
{  
   //改变console字体颜色
   system("color 6F"); 

   ShowHelpText();

   //初始化
   FileStorage fs2("test.yaml", FileStorage::READ);  

   // 第一种方法,对FileNode操作
   int frameCount = (int)fs2["frameCount"];  

   std::string date;  
   // 第二种方法,使用FileNode运算符> > 
   fs2["calibrationDate"] >> date;  

   Mat cameraMatrix2, distCoeffs2;  
   fs2["cameraMatrix"] >> cameraMatrix2;  
   fs2["distCoeffs"] >> distCoeffs2;  

   cout << "frameCount: " << frameCount << endl  
   	<< "calibration date: " << date << endl  
   	<< "camera matrix: " << cameraMatrix2 << endl  
   	<< "distortion coeffs: " << distCoeffs2 << endl;  

   FileNode features = fs2["features"];  
   FileNodeIterator it = features.begin(), it_end = features.end();  
   int idx = 0;  
   std::vector<uchar> lbpval;  

   //使用FileNodeIterator遍历序列
   for( ; it != it_end; ++it, idx++ )  
   {  
   	cout << "feature #" << idx << ": ";  
   	cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";  
   	// 我们也可以使用使用filenode > > std::vector操作符很容易的读数值阵列
   	(*it)["lbp"] >> lbpval;  
   	for( int i = 0; i < (int)lbpval.size(); i++ )  
   		cout << " " << (int)lbpval[i];  
   	cout << ")" << endl;  
   }  
   fs2.release();  

   //程序结束,输出一些帮助文字
   printf("\n文件读取完毕,请输入任意键结束程序~");
   getchar();

   return 0;  
}  

此程序需要工程目录下有指定的文件存在,如我们将前文刚刚生成的test.yaml 复制到工程目录下,运行此程序,便可以得出正确的结果。运行截图如下图所示。

3.PNG

5小结

我们学习了core模块的一些进阶知识点—一操作图像中的像素、图像混合、分离颜色通道、调节图像的对比度和亮度、进行离散傅里叶变换,以及输入输出XML和YAML文件。

涉及到的函数

函数名称说明
addWeighted计算两个数组(图像阵列)的加权和
split将一个多通道数组分离成几个单通道数组
merge将多个数组组合合并成一个多通道的数组
dft对一维或二维浮点数数组进行正向或反向离散傅里叶变换
getOptimalDFTSize返回给定向量尺寸的傅里叶最优尺寸大小
copyMakeBorder扩充图像边界
magnitude计算二维矢量的幅值
log计算每个数组元素绝对值的自然对数
normalize进行矩阵归一化
FileStorage类进行文件操作的类