パラメータ(カメラパラメータや学習したパラメータ)をOpenCVを通じて保存する方法
大体こちらに&note{XML-opencvjp:ファイルへのXML/YAML形式でのデータ保存/読み込み | OpenCV.jp};に書いてある内容で充分

void write(FileStorage &fs, variable v);

variable v の型に応じて多重定義されてる

引数

返り値

void cvWrite( CvFileStorage *fs, const char* name, const void* ptr, CvAttrList attributes)

Cインタフェース版

引数

返り値

解説

サンプルコード1

#geshi(c++,number){{

#include "opencv2/core/core.hpp"

int main(){

	cv::FileStorage fs;
	fs.open("test.xml", cv::FileStorage::WRITE);
	// READ と WRITE と APPEND は FileStorage 内にて enum で定義されてる
	// indexという名前で値を保存
	int index = 1;
	cv::write(fs, "index", index);
	// 複数のデータを書き出す場合
	cv::Rect aRect(200, 300, 100, 100);
	cv::WriteStructContext ws(fs, "Rectangles", CV_NODE_SEQ + CV_NODE_FLOW);
	for(int i = 0;i < 10;i++){
		cv::write(fs, aRect);
	}
	return 0;

} }}

実行結果1

<?xml version="1.0"?>
<opencv_storage>
<index>1</index>
<Rectangles>
  200 300 100 100 200 300 100 100 200 300 100 100 200 300 100 100 200
  300 100 100 200 300 100 100 200 300 100 100 200 300 100 100 200 300
  100 100 200 300 100 100</Rectangles>
</opencv_storage>

サンプルコード2

オブジェクトで書き出そうとしたらハマった

#geshi(c++,number){{

#include "opencv2/core/core.hpp"

int main(){

	cv::FileStorage fs;
	fs.open("test.xml", cv::FileStorage::WRITE);
	// READ と WRITE と APPEND は FileStorage 内にて enum で定義されてる
	cv::WriteStructContext ws(fs, "Region1", CV_NODE_SEQ + CV_NODE_FLOW);
	cv::Rect aRect(200, 300, 100, 100);
	cv::write(fs, "region1", aRect);
	return -1;

} }};

実行結果2

サンプルコード3

名前を降らなくても,配列を複数書き出そうとしたらこける

#geshi(c++,number){{

#include "opencv2/core/core.hpp"

int main(){

	cv::FileStorage fs;
	fs.open("test.xml", cv::FileStorage::WRITE);
	cv::Rect aRect(200, 300, 100, 100);
	cv::WriteStructContext ws(fs, "Faces1", CV_NODE_SEQ);
	for(int i = 0;i < 10;i++){
		cv::write(fs, aRect);
	}
	cv::WriteStructContext ws2(fs, "Faces2", CV_NODE_SEQ); // <- ここで死ぬ
	for(int i = 0;i < 10;i++){
		cv::write(fs, aRect);
	}
	return -1;

} }}

実行結果3

サンプルコード4

配列を複数書きだす場合はこう書く

#geshi(c++,number){{

#include "opencv2/core/core.hpp"

int main(){

	cv::FileStorage fs;
	fs.open("test.xml", cv::FileStorage::WRITE);
	cv::Rect aRect(200, 300, 100, 100);
	{
		cv::WriteStructContext ws(fs, "Faces1", CV_NODE_SEQ);
		for(int i = 0;i < 10;i++){
			cv::write(fs, aRect);
		}
	} // <- ****ここで無駄にブラケットで区切ることにより ws がここで消滅してデストラクタが呼ばれる
	{
		cv::WriteStructContext wsq(fs, "Faces2", CV_NODE_SEQ);
		for(int i = 0;i < 10;i++){
			cv::write(fs, aRect);
		}
	}
	return -1;

}}

実行結果4

<?xml version="1.0"?>
<opencv_storage>
<Faces1>
  200 300 100 100 200 300 100 100 200 300 100 100 200 300 100 100 200
  300 100 100 200 300 100 100 200 300 100 100 200 300 100 100 200 300
  100 100 200 300 100 100</Faces1>
<Faces2>
  200 300 100 100 200 300 100 100 200 300 100 100 200 300 100 100 200
  300 100 100 200 300 100 100 200 300 100 100 200 300 100 100 200 300
  100 100 200 300 100 100</Faces2>
</opencv_storage>

実体ファイル

ジャンル:OpenCV 2.3:OpenCV準拠


トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2012-09-28 (金) 16:15:50