Difference between revisions of "OpenCV/WishList/IO/WriteMatrix"

From ProgrammingExamples
Jump to: navigation, search
Line 1: Line 1:
This works fine for integer valued matrices, but how do you write a float matrix to a file?
+
OpenCV Error: Parsing error (test.mat(0): Valid XML should start with '<?xml ...?>') in icvXMLParse, file /home/doriad/src/OpenCV/modules/core/src/persistence.cpp, line 2136
 +
terminate called after throwing an instance of 'cv::Exception'
 +
  what():  /home/doriad/src/OpenCV/modules/core/src/persistence.cpp:2136: error: (-212) test.mat(0): Valid XML should start with '<?xml ...?>' in function icvXMLParse
  
 
==WriteMatrix.cxx==
 
==WriteMatrix.cxx==
Line 11: Line 13:
 
int main(int argc, char*argv[])
 
int main(int argc, char*argv[])
 
{
 
{
 +
  // Create a matrix
 
   cv::Mat myMatrix(3,3,CV_32FC1);
 
   cv::Mat myMatrix(3,3,CV_32FC1);
 
   std::cout << "Input:" << std::endl;
 
   std::cout << "Input:" << std::endl;
Line 22: Line 25:
  
 
   std::cout << myMatrix << std::endl;
 
   std::cout << myMatrix << std::endl;
    
+
 
   cv::FileStorage fs("test.mat", cv::FileStorage::WRITE);
+
   // Write the matrix to a file
   fs << myMatrix;
+
   cv::FileStorage fsout("test.mat", cv::FileStorage::WRITE);
    
+
   fsout << "myMatrix" << myMatrix;
 +
 
 +
   // Read the file back in to test that it was written correctly
 +
  cv::Mat myMatrix2(3,3,CV_32FC1);
 +
  cv::FileStorage fsin("test.mat", cv::FileStorage::READ);
 +
  fsin["myMatrix"] >> myMatrix2;
 +
 
 +
  std::cout << myMatrix2 << std::endl;
 
    
 
    
 
   return 0;
 
   return 0;
 
}
 
}
 +
  
  

Revision as of 11:26, 21 January 2011

OpenCV Error: Parsing error (test.mat(0): Valid XML should start with '<?xml ...?>') in icvXMLParse, file /home/doriad/src/OpenCV/modules/core/src/persistence.cpp, line 2136 terminate called after throwing an instance of 'cv::Exception'

 what():  /home/doriad/src/OpenCV/modules/core/src/persistence.cpp:2136: error: (-212) test.mat(0): Valid XML should start with '<?xml ...?>' in function icvXMLParse

WriteMatrix.cxx

#include "cv.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
 
#include <iostream>
 
int main(int argc, char*argv[])
{
  // Create a matrix
  cv::Mat myMatrix(3,3,CV_32FC1);
  std::cout << "Input:" << std::endl;
  for(unsigned int i = 0; i < 3; i++)
    {
    for(unsigned int j = 0; j < 3; j++)
      {
      myMatrix.at<float>(i,j) = 2.1;
      }
    }
 
  std::cout << myMatrix << std::endl;
 
  // Write the matrix to a file
  cv::FileStorage fsout("test.mat", cv::FileStorage::WRITE);
  fsout << "myMatrix" << myMatrix;
 
  // Read the file back in to test that it was written correctly
  cv::Mat myMatrix2(3,3,CV_32FC1);
  cv::FileStorage fsin("test.mat", cv::FileStorage::READ);
  fsin["myMatrix"] >> myMatrix2;
 
  std::cout << myMatrix2 << std::endl;
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(WriteMatrix)
 
FIND_PACKAGE(OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OPENCV_INCLUDE_DIR} )
 
 
ADD_EXECUTABLE(WriteMatrix WriteMatrix.cxx)
TARGET_LINK_LIBRARIES(WriteMatrix opencv_core opencv_highgui
opencv_flann opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_objdetect
          opencv_features2d opencv_calib3d opencv_legacy opencv_contrib
)