日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

目標

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

OpenCV數據結構如何做同樣的操作?

如何為您的數據結構做這個?

源代碼

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

以下是如何實現目標列表中枚舉的所有內容的示例代碼。#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;

}

說明

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

FileStorage fs(filename, FileStorage::WRITE);

//...

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

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

當cv :: FileStorage對象被銷毀時,文件會自動關閉。但是,您可以使用發布功能顯式地調用此功能:fs.release(); // explicit close文本和數字的輸入和輸出。數據結構使用與STL庫相同的<

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

fs["iterationNr"] >> itNr;

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

OpenCV數據結構的輸入/輸出。那么這些行為就像基本的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;

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

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

fs << "]"; // close sequence

對于地圖,鉆頭是一樣的,現在我們使用“{”和“}”分隔符: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;

讀寫自己的數據結構。假設你有一個數據結構,如:class MyData

{

public:

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

public: // Data Members

int A;

double X;

string id;

};

通過OpenCV I / O XML / YAML接口(就像OpenCV數據結構一樣),可以通過在類中添加一個讀取和寫入函數來對其進行序列化。對于內部: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"];

}

那么你需要在類之外添加以下函數定義: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);

}

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

一旦添加了這四個函數,就可以使用>>操作符進行寫操作,而<

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;

結果

主要是我們打印出定義的數字。您可以在控制臺的屏幕上看到: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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。