OpenCV/WishList/IO/WriteMatrix

From ProgrammingExamples
Jump to: navigation, search

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.yml", 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.yml", 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_LIBS})