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

 

Comments

Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

Optical Flow sample source code using OpenCV

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

SICK LMS511 sensor data acquisition interface (source code, C++/MFC)

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Image warping (using opencv findHomography, warpPerspective)