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

From ProgrammingExamples
Jump to: navigation, search
(Created page with '==WriteMatrix.cxx== <source lang="cpp"> #include "cv.h" #include "opencv2/highgui/highgui.hpp" #include <iostream> int main(int argc, char*argv[]) { cv::Mat myMatrix(3,3,CV_3…')
 
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
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==
 
<source lang="cpp">
 
<source lang="cpp">
 
#include "cv.h"
 
#include "cv.h"
 
#include "opencv2/highgui/highgui.hpp"
 
#include "opencv2/highgui/highgui.hpp"
 +
#include "opencv2/core/core.hpp"
  
 
#include <iostream>
 
#include <iostream>
Line 8: Line 13:
 
int main(int argc, char*argv[])
 
int main(int argc, char*argv[])
 
{
 
{
   cv::Mat myMatrix(3,3,CV_32FC3);
+
  // Create a matrix
 +
   cv::Mat myMatrix(3,3,CV_32FC1);
 
   std::cout << "Input:" << std::endl;
 
   std::cout << "Input:" << std::endl;
 
   for(unsigned int i = 0; i < 3; i++)
 
   for(unsigned int i = 0; i < 3; i++)
Line 14: Line 20:
 
     for(unsigned int j = 0; j < 3; j++)
 
     for(unsigned int j = 0; j < 3; j++)
 
       {
 
       {
      cv::Point3f myPoint;
+
       myMatrix.at<float>(i,j) = 2.1;
      myPoint.x = 1;
+
      myPoint.y = 2;
+
      myPoint.z = 3;
+
       myMatrix.at<cv::Point3f>(i,j) = myPoint;
+
 
       }
 
       }
 
     }
 
     }
  
 
   std::cout << myMatrix << std::endl;
 
   std::cout << myMatrix << std::endl;
 
 
  cv::imwrite("matrix.jpg", myMatrix);
 
 
 
  cv::Mat inputMatrix = cv::imread("matrix.jpg", 1);
 
  
   std::cout << inputMatrix << 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;
 
   return 0;
 
}
 
}
 +
  
  
Line 47: Line 55:
  
 
ADD_EXECUTABLE(WriteMatrix WriteMatrix.cxx)
 
ADD_EXECUTABLE(WriteMatrix WriteMatrix.cxx)
TARGET_LINK_LIBRARIES(WriteMatrix opencv_core opencv_highgui
+
TARGET_LINK_LIBRARIES(WriteMatrix ${OpenCV_LIBS})
opencv_flann opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_objdetect
+
          opencv_features2d opencv_calib3d opencv_legacy opencv_contrib
+
)
+
  
 
</source>
 
</source>

Latest revision as of 11:25, 20 September 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.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})