OpenCV/WishList/IO/WriteMatrix

From ProgrammingExamples
< OpenCV
Revision as of 11:04, 21 January 2011 by Daviddoria (Talk | contribs)

Jump to: navigation, search

WriteMatrix.cxx

#include "cv.h"
#include "opencv2/highgui/highgui.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::imwrite("matrix.jpg", 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;
}

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
)