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

From ProgrammingExamples
Jump to: navigation, search
Line 5: Line 5:
 
#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 22: Line 23:
 
   std::cout << myMatrix << std::endl;
 
   std::cout << myMatrix << std::endl;
 
    
 
    
   cv::imwrite("matrix.jpg", myMatrix);
+
   cv::FileStorage fs("test.mat", cv::FileStorage::WRITE);
 
+
   fs << myMatrix;
  // Doesn't work - the matrix that is read back in has been rounded to integers!
+
 
  cv::Mat inputMatrix = cv::imread("matrix.jpg", 0);
+
 
+
   std::cout << inputMatrix << std::endl;
+
 
    
 
    
 
   return 0;
 
   return 0;
 
}
 
}
 
  
  

Revision as of 11:13, 21 January 2011

This works fine for integer valued matrices, but how do you write a float matrix to a file?

WriteMatrix.cxx

#include "cv.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
 
#include <iostream>
 
int main(int argc, char*argv[])
{
  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;
 
  cv::FileStorage fs("test.mat", cv::FileStorage::WRITE);
  fs << myMatrix;
 
 
  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
)