Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

4/17/2014

The example source code of 2d vector write and read to the XML file, using OpenCV ( and also introcuded 2D vector conver to Mat and Mat to 2d Vector converting example source code)

1d vector convert to Mat
Mat convert to 1d vector
refert to this post http://feelmare.blogspot.kr/2014/01/opencv-vector-to-mat-mat-to-vector.html


This post is about 2d vector write to the XML file.
And read the XML file and data assigned to 2d vector again.

To complete this process, I use 2d vector convert to Mat and Mat convert to 2D vector.
The method to converting 2D vector, Mat is like that..

...
//2D vector to Mat
//create Mat   
Mat M(row,col,CV_32F);
//copy 2d vector to mat  
for(int i=0; i< row; ++i)  
memcpy( &(M.data[col * i * sizeof(float) ]) ,vv_Test[i].data(),col*sizeof(float)); 



//Mat to 2D vector
//copy from Mat to 2d Vector
for(int i=0; i< row; ++i)
{
 vector< float > temp;
 int start=col * i * sizeof(float);
 int end=start + col*sizeof(float)-1;
 temp.assign( (float*)(&(M2.data[start])), (float*)(&(M2.data[end])) );
 vv_Test2.push_back(temp);
}
---





This example source is save 2d vector to XML and read xml and copy to 2D vector variable.
...
#include < stdio.h>
#include < opencv2\opencv.hpp>
//#include < opencv2\gpu\gpu.hpp>

using namespace cv;
using namespace std;


#ifdef _DEBUG        
#pragma comment(lib, "opencv_core247d.lib")         
#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing        
#pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor
//#pragma comment(lib, "opencv_gpu247d.lib")        
//#pragma comment(lib, "opencv_features2d247d.lib")        
#pragma comment(lib, "opencv_highgui247d.lib")        
//#pragma comment(lib, "opencv_ml247d.lib")      
//#pragma comment(lib, "opencv_stitching247d.lib");      
//#pragma comment(lib, "opencv_nonfree247d.lib");      
  
#else        
#pragma comment(lib, "opencv_core247.lib")        
#pragma comment(lib, "opencv_imgproc247.lib")        
#pragma comment(lib, "opencv_objdetect247.lib")        
//#pragma comment(lib, "opencv_gpu247.lib")        
//#pragma comment(lib, "opencv_features2d247.lib")        
#pragma comment(lib, "opencv_highgui247.lib")        
//#pragma comment(lib, "opencv_ml247.lib")        
//#pragma comment(lib, "opencv_stitching247.lib");      
//#pragma comment(lib, "opencv_nonfree247.lib");      
#endif 


void main()
{
 /////////////////////////////////////////////////////////////
 ////Write xml example

 //variables
 vector< vector < float > > vv_Test;
 int row = 5, col = 10;
 //make vector values
 for(int i=0; i< 5; ++i)
 {
  vector< float > vTest;
  for(int j=0; j< 10; ++j)
   vTest.push_back(i*j);

  vv_Test.push_back( vTest );
 }

 //create xml to write
 FileStorage write_hogXml("V_writeTest.xml", FileStorage::WRITE); //FileStorage::READ
 //create Mat   
 Mat M(row,col,CV_32F);
 //copy 2d vector to mat  
 for(int i=0; i< row; ++i)  
  memcpy( &(M.data[col * i * sizeof(float) ]) ,vv_Test[i].data(),col*sizeof(float)); 
 //write xml
 write(write_hogXml, "vectorTest",  M);
 //release
 write_hogXml.release();


 ///////////////////////////////////////////////////////////////////////////
 //read xml example 
 //create xml to read
 FileStorage read_hogXml("V_writeTest.xml", FileStorage::READ); //FileStorage::READ
 //Create Mat
 int row2,col2;
 //create Mat, 2d vector
 Mat M2; 
 vector< vector < float > > vv_Test2;
 //read data into Mat
 read( read_hogXml["vectorTest"], M2);
 row2 = M2.rows;
 col2 = M2.cols;
 printf("%d %d\n", row2, col2);
 //read_hogXml["vectorTest"] >> M2; //same 
 //copy from Mat to 2d Vector
 for(int i=0; i< row2; ++i)
 {
  vector< float > temp;
  int start=col2 * i * sizeof(float);
  int end=start + col2*sizeof(float)-1;
  temp.assign( (float*)(&(M2.data[start])), (float*)(&(M2.data[end])) );
  vv_Test2.push_back(temp);
 }
 //release
 read_hogXml.release();
 

 ///////////////////////////////////////////////////////////////////////////////////
 printf("read data confirm!! \n");
 for(int i=0; i< vv_Test2.size(); ++i)
 {
  vector< float > vTest;
  for(int j=0; j< vv_Test2[i].size(); ++j)
   printf("%.0f ", vv_Test2[i][j] );

  printf("\n");
 }

}

---

result of save XML file.
 
result of read XML and print 2D vector

 

3/05/2014

(C, C++) TinyXML , xml read & write

Firstly, download TinyXML source code on here -> http://sourceforge.net/projects/tinyxml/

After unzip, you can see many files and folders like that.

But, we only need 6 files.  
Copy these files to appropriate folder.

Make your project and add 6 files in your project.


This is example of xml file. Save file name to "ex4.xml"

---
< ? xml version="1.0" ?>
< LibraryIndex>
    < !-- Settings for MyApp -->
    < Messages>
        < Welcome>Welcome to MyApp< /Welcome>
        < Farewell>Thank you for using MyApp< /Farewell>
    < /Messages>
    < Windows>
        < Window name="MainFrame" x="5" y="15" w="400" h="250" />
    < /Windows>
    < Connection ip="192.168.0.1" timeout="123.456000" />
< /LibraryIndex>

----

Make code like this and run.
In my case, 6 TinyXML files are located "./tinyXML/"

---
#include < stdio.h>
#include "./tinyXML/tinyxml.h"


void main()
{
 TiXmlDocument doc;
 bool loadOkay = doc.LoadFile("ex4.xml");

 if (loadOkay)
 {
  printf("open success\n");
 }
 else
 {
  printf("Failed to load file \n");
 } 
}
---

If you see the "open success" massage, xml file is opened nomally.


-----------------------------
XML read & parsing example source code.
---
#include < stdio.h>
#include "./tinyXML/tinyxml.h"


void main()
{
 TiXmlDocument doc;
 bool loadOkay = doc.LoadFile("ex4.xml");

 if (loadOkay)
 {
  printf("open success\n");
 }
 else
 {
  printf("Failed to load file \n");
 } 


 //root node access
 TiXmlElement* pRoot = doc.FirstChildElement("LibraryIndex"); 
 if (!pRoot) return;

 //access "welcom" element to get value
 TiXmlElement* pElem = pRoot->FirstChildElement("Messages")->FirstChildElement("Welcome");
 if (!pElem) return;

 //get element value (Welcome).
 char* pszNode = (char*)pElem->Value();
 if (pszNode)
  printf("Node: %s\n", pszNode);

 //get element text(Welcome to MyApp)
 char* pszText = (char*)pElem->GetText();
 if (pszText)
  printf("Text: %s\n", pszText);


}

---
The result is like that.

--------------------------
modify example.
***
#include < stdio.h>
#include "./tinyXML/tinyxml.h"


void main()
{
 TiXmlDocument doc;
 bool loadOkay = doc.LoadFile("ex4.xml");

 if (loadOkay)
 {
  printf("open success\n");
 }
 else
 {
  printf("Failed to load file \n");
 } 

 // access root node
 TiXmlElement* pRoot = doc.FirstChildElement("LibraryIndex");   
 if (!pRoot) return;  

 // access welcome element to modify value  
 TiXmlElement* pElem = pRoot->FirstChildElement("Messages")->FirstChildElement("Welcome");  
 if (!pElem) return;

 // element clear and new value adding 
 pElem->Clear();
 pElem->LinkEndChild( new TiXmlText("Edit My App"));

 doc.SaveFile("m_ex4.xml");
}
***
The result is like the figure.


-------------
Example source code to make New xml file.
In the source code, new and delete pair is do not need because created variable by new keyword will be delete automatically after block.
***
#include < stdio.h>
#include "./tinyXML/tinyxml.h"


void main()
{
 
 // Declaration XML format
 TiXmlDocument doc;
 TiXmlDeclaration* pDecl = new TiXmlDeclaration("1.0", "euc-kr", "");  
 doc.LinkEndChild(pDecl);

 //Add Root node
 TiXmlElement* pRoot = new TiXmlElement("LibraryIndex2");
 doc.LinkEndChild(pRoot);

 // Add Comment sentence
 TiXmlComment * pComment = new TiXmlComment();
 pComment->SetValue("Settings for MyApp");
 pRoot->LinkEndChild(pComment);

 // Add child node and data
 TiXmlElement* pElem = new TiXmlElement("Messages");
 pRoot->LinkEndChild(pElem);

 TiXmlElement* pSubElem = new TiXmlElement("Welcome");
 pSubElem->LinkEndChild( new TiXmlText("Welcome to MyApp"));
 pElem->LinkEndChild(pSubElem);

 pSubElem = new TiXmlElement("Farewell");
 pSubElem->LinkEndChild( new TiXmlText("Thank you for using MyApp"));
 pElem->LinkEndChild(pSubElem);

 // Add child node and set attribute 
 pElem = new TiXmlElement("Windows");
 pRoot->LinkEndChild(pElem);

 pSubElem = new TiXmlElement("Window");
 pElem->LinkEndChild(pSubElem);
 pSubElem->SetAttribute("name", "MainFrame");
 pSubElem->SetAttribute("x", 5);
 pSubElem->SetAttribute("y", 15);
 pSubElem->SetAttribute("w", 400);
 pSubElem->SetAttribute("h", 250);

 pElem = new TiXmlElement("Connection");
 pRoot->LinkEndChild(pElem);
 pElem->SetAttribute("ip", "192.168.0.1");
 pElem->SetDoubleAttribute("timeout", 123.456);

 //Save xml file format
 doc.SaveFile("new_ex4.xml");

}
***
The result is like the figure.

-------
Finally, This source code is searching all node and text by using recursive routine.

#include < stdio.h>
#include "./tinyXML/tinyxml.h"

void searchXMLData(TiXmlElement* pElem)
{
 TiXmlHandle hRoot(0);
 TiXmlElement* pSubElem = pElem;
 TiXmlAttribute* pAttrib = NULL;

 
 //Set current node to root node and determine childe node is exist or not
 hRoot = TiXmlHandle(pSubElem);
 pSubElem = hRoot.FirstChildElement().Element();
 if (!pSubElem) return;

 char* pszNode = NULL;
 char* pszAttrib = NULL;
 char* pszText = NULL;

 while (pSubElem)
 {
  //node
  pszNode = (char*)pSubElem->Value();
  if (pszNode)
   printf("Node: %s\n", pszNode);

  //Attribute
  pAttrib = pSubElem->FirstAttribute();
  while (pAttrib)
  {
   char* pszAttrib = (char*)pAttrib->Name();
   char* pszText = (char*)pAttrib->Value();

   printf("------------Attribute: %s, Data: %s\n", pszAttrib, pszText);

   pAttrib = pAttrib->Next();
  }
  
  //Data
  pszText = (char*)pSubElem->GetText();
  if (pszText)
   printf("Text: %s\n", pszText);

  // Recursive call for searching child node based current node
  searchXMLData(pSubElem);
  pSubElem = pSubElem->NextSiblingElement();
 }
}

int main()
{
 TiXmlDocument doc;
 doc.LoadFile("ex4.xml");
 TiXmlHandle hDoc(&doc);

 //access root node
 TiXmlElement* pRoot = hDoc.FirstChildElement().Element();
 if (!pRoot) return 0;

 
 //search childe node step by step from starting root node
 searchXMLData(pRoot);
 
 return 0;
}

The result is like the figure.


Thank you.
Good luck!.

3/04/2014

(OpenCV Study) xml write & read, example source code.

This is very simple example source code for write and read xml file.
OpenCV version will be no matter.
I hope helpful to you.
Thank you.



//Write example
FileStorage xmlW("A.xml", FileStorage::WRITE);
Mat A(3,3, CV_32F);
setIdentity(A);
xmlW << "A" << A;
xmlW.release();

//read example
FileStorage xmlR;
xmlR.open("A.xml", FileStorage::READ);
Mat B;
xmlR["A"] >> B;
cout << B << endl;
xmlR.release();