日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

yamlcpp遍历_OpenCV文件输入和输出使用XML和YAML文件

發(fā)布時間:2025/3/15 asp.net 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 yamlcpp遍历_OpenCV文件输入和输出使用XML和YAML文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目標(biāo)

您會找到以下問題的答案:如何使用YAML或XML文件打印和讀取文本和OpenCV文本條目?

OpenCV數(shù)據(jù)結(jié)構(gòu)如何做同樣的操作?

如何為您的數(shù)據(jù)結(jié)構(gòu)做這個?

源代碼

您可以從這里下載,也可以在samples/cpp/tutorial_code/core/file_input_output/file_input_output.cppOpenCV源代碼庫中找到它。

以下是如何實現(xiàn)目標(biāo)列表中枚舉的所有內(nèi)容的示例代碼。#include

#include

#include

using namespace cv;

using namespace std;

static void help(char** av)

{

cout << endl

<< av[0] << " shows the usage of the OpenCV serialization functionality." << endl

<< "usage: " << endl

<< av[0] << " outputfile.yml.gz" << endl

<< "The output file may be either XML (xml) or YAML (yml/yaml). You can even compress it by "

<< "specifying this in its extension like xml.gz yaml.gz etc... " << endl

<< "With FileStorage you can serialize objects in OpenCV by using the << and >> operators" << endl

<< "For example: - create a class and have it serialized" << endl

<< " - use it to read and write matrices." << endl;

}

class MyData

{

public:

MyData() : A(0), X(0), id()

{}

explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") // explicit to avoid implicit conversion

{}

void write(FileStorage& fs) const //Write serialization for this class

{

fs << "{" << "A" << A << "X" << X << "id" << id << "}";

}

void read(const FileNode& node) //Read serialization for this class

{

A = (int)node["A"];

X = (double)node["X"];

id = (string)node["id"];

}

public: // Data Members

int A;

double X;

string id;

};

//These write and read functions must be defined for the serialization in FileStorage to work

static void write(FileStorage& fs, const std::string&, const MyData& x)

{

x.write(fs);

}

static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){

if(node.empty())

x = default_value;

else

x.read(node);

}

// This function will print our custom class to the console

static ostream& operator<

{

out << "{ id = " << m.id << ", ";

out << "X = " << m.X << ", ";

out << "A = " << m.A << "}";

return out;

}

int main(int ac, char** av)

{

if (ac != 2)

{

help(av);

return 1;

}

string filename = av[1];

{ //write

Mat R = Mat_::eye(3, 3),

T = Mat_::zeros(3, 1);

MyData m(1);

FileStorage fs(filename, FileStorage::WRITE);

fs << "iterationNr" << 100;

fs << "strings" << "["; // text - string sequence

fs << "image1.jpg" << "Awesomeness" << "../data/baboon.jpg";

fs << "]"; // close sequence

fs << "Mapping"; // text - mapping

fs << "{" << "One" << 1;

fs << "Two" << 2 << "}";

fs << "R" << R; // cv::Mat

fs << "T" << T;

fs << "MyData" << m; // your own data structures

fs.release(); // explicit close

cout << "Write Done." << endl;

}

{//read

cout << endl << "Reading: " << endl;

FileStorage fs;

fs.open(filename, FileStorage::READ);

int itNr;

//fs["iterationNr"] >> itNr;

itNr = (int) fs["iterationNr"];

cout << itNr;

if (!fs.isOpened())

{

cerr << "Failed to open " << filename << endl;

help(av);

return 1;

}

FileNode n = fs["strings"]; // Read string sequence - Get node

if (n.type() != FileNode::SEQ)

{

cerr << "strings is not a sequence! FAIL" << endl;

return 1;

}

FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node

for (; it != it_end; ++it)

cout << (string)*it << endl;

n = fs["Mapping"]; // Read mappings from a sequence

cout << "Two " << (int)(n["Two"]) << "; ";

cout << "One " << (int)(n["One"]) << endl << endl;

MyData m;

Mat R, T;

fs["R"] >> R; // Read cv::Mat

fs["T"] >> T;

fs["MyData"] >> m; // Read your own structure_

cout << endl

<< "R = " << R << endl;

cout << "T = " << T << endl << endl;

cout << "MyData = " << endl << m << endl << endl;

//Show default behavior for non existing nodes

cout << "Attempt to read NonExisting (should initialize the data structure with its default).";

fs["NonExisting"] >> m;

cout << endl << "NonExisting = " << endl << m << endl;

}

cout << endl

<< "Tip: Open up " << filename << " with a text editor to see the serialized data." << endl;

return 0;

}

說明

這里我們僅談?wù)揦ML和YAML文件輸入。您的輸出(及其相應(yīng)的輸入)文件可能只有這些擴展中的一個和結(jié)構(gòu)來自此。它們是可以序列化的兩種數(shù)據(jù)結(jié)構(gòu):映射(如STL映射)和元素序列(如STL向量)。這些之間的區(qū)別是,在地圖中,每個元素都有一個唯一的名稱,通過您可以訪問它。對于序列,您需要通過它們查詢特定項目。XML / YAML文件打開和關(guān)閉。在將任何內(nèi)容寫入此類文件之前,您需要將其打開并結(jié)束關(guān)閉。OpenCV中的XML / YAML數(shù)據(jù)結(jié)構(gòu)是cv :: FileStorage。要指定文件在硬盤驅(qū)動器上綁定的結(jié)構(gòu),您可以使用其構(gòu)造函數(shù)或open()函數(shù):string filename = "I.xml";

FileStorage fs(filename, FileStorage::WRITE);

//...

fs.open(filename, FileStorage::READ);

您使用第二個參數(shù)中的任何一個是一個常數(shù),指定您可以在其上執(zhí)行的操作類型:WRITE,READ或APPEND。文件名中指定的擴展名也可以確定要使用的輸出格式。如果指定擴展名,例如* .xml.gz *,輸出可能會被壓縮。

當(dāng)cv :: FileStorage對象被銷毀時,文件會自動關(guān)閉。但是,您可以使用發(fā)布功能顯式地調(diào)用此功能:fs.release(); // explicit close文本和數(shù)字的輸入和輸出。數(shù)據(jù)結(jié)構(gòu)使用與STL庫相同的<

讀入是一個簡單的尋址(通過[]操作符)和轉(zhuǎn)換操作或通過>>操作符讀取:int itNr;

fs["iterationNr"] >> itNr;

itNr = (int) fs["iterationNr"];

OpenCV數(shù)據(jù)結(jié)構(gòu)的輸入/輸出。那么這些行為就像基本的C ++類型一樣:Mat R = Mat_::eye (3, 3),

T = Mat_::zeros(3, 1);

fs << "R" << R; // Write cv::Mat

fs << "T" << T;

fs["R"] >> R; // Read cv::Mat

fs["T"] >> T;

向量(數(shù)組)和關(guān)聯(lián)圖的輸入/輸出。如前所述,我們可以輸出地圖和序列(數(shù)組,向量)。再次,我們首先打印變量的名稱,然后我們必須指定我們的輸出是序列還是地圖。對于第一個元素之前的序列打印“[”字符,最后一個“]”字符后:fs << "strings" << "["; // text - string sequence

fs << "image1.jpg" << "Awesomeness" << "baboon.jpg";

fs << "]"; // close sequence

對于地圖,鉆頭是一樣的,現(xiàn)在我們使用“{”和“}”分隔符:fs << "Mapping"; // text - mapping

fs << "{" << "One" << 1;

fs << "Two" << 2 << "}";FileNode n = fs["strings"]; // Read string sequence - Get node

if (n.type() != FileNode::SEQ)

{

cerr << "strings is not a sequence! FAIL" << endl;

return 1;

}

FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node

for (; it != it_end; ++it)

cout << (string)*it << endl;

對于地圖,您可以再次使用[]運算符來訪問給定項目(或>>運算符):n = fs["Mapping"]; // Read mappings from a sequence

cout << "Two " << (int)(n["Two"]) << "; ";

cout << "One " << (int)(n["One"]) << endl << endl;

讀寫自己的數(shù)據(jù)結(jié)構(gòu)。假設(shè)你有一個數(shù)據(jù)結(jié)構(gòu),如:class MyData

{

public:

MyData() : A(0), X(0), id() {}

public: // Data Members

int A;

double X;

string id;

};

通過OpenCV I / O XML / YAML接口(就像OpenCV數(shù)據(jù)結(jié)構(gòu)一樣),可以通過在類中添加一個讀取和寫入函數(shù)來對其進(jìn)行序列化。對于內(nèi)部:void write(FileStorage& fs) const //Write serialization for this class

{

fs << "{" << "A" << A << "X" << X << "id" << id << "}";

}

void read(const FileNode& node) //Read serialization for this class

{

A = (int)node["A"];

X = (double)node["X"];

id = (string)node["id"];

}

那么你需要在類之外添加以下函數(shù)定義:void write(FileStorage& fs, const std::string&, const MyData& x)

{

x.write(fs);

}

void read(const FileNode& node, MyData& x, const MyData& default_value = MyData())

{

if(node.empty())

x = default_value;

else

x.read(node);

}

在這里您可以看到,在閱讀部分中,我們定義了如果用戶嘗試讀取不存在的節(jié)點會發(fā)生什么。在這種情況下,我們只返回默認(rèn)的初始化值,但是更詳細(xì)的解決方案是返回一個對象ID的減號值。

一旦添加了這四個函數(shù),就可以使用>>操作符進(jìn)行寫操作,而<

fs << "MyData" << m; // your own data structures

fs["MyData"] >> m; // Read your own structure_

或嘗試閱讀一個不存在的閱讀:fs["NonExisting"] >> m; // Do not add a fs << "NonExisting" << m command for this to work

cout << endl << "NonExisting = " << endl << m << endl;

結(jié)果

主要是我們打印出定義的數(shù)字。您可以在控制臺的屏幕上看到:Write Done.

Reading:

100image1.jpg

Awesomeness

baboon.jpg

Two 2; One 1

R = [1, 0, 0;

0, 1, 0;

0, 0, 1]

T = [0; 0; 0]

MyData =

{ id = mydata1234, X = 3.14159, A = 97}

Attempt to read NonExisting (should initialize the data structure with its default).

NonExisting =

{ id = , X = 0, A = 0}

Tip: Open up output.xml with a text editor to see the serialized data.

不過,輸出xml文件中可能會看到的更有趣:<?xml version="1.0"?>

100

image1.jpg Awesomeness baboon.jpg

1

2

3

3

u

1 0 0 0 1 0 0 0 1

3

1

d

0. 0. 0.

97

3.1415926535897931e+000

mydata1234

或YAML文件:%YAML:1.0

iterationNr: 100

strings:

- "image1.jpg"

- Awesomeness

- "baboon.jpg"

Mapping:

One: 1

Two: 2

R: !!opencv-matrix

rows: 3

cols: 3

dt: u

data: [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ]

T: !!opencv-matrix

rows: 3

cols: 1

dt: d

data: [ 0., 0., 0. ]

MyData:

A: 97

X: 3.1415926535897931e+000

id: mydata1234

總結(jié)

以上是生活随笔為你收集整理的yamlcpp遍历_OpenCV文件输入和输出使用XML和YAML文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。