Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Image To Matrix In txt files

I am doing this to get each pixel value of Image and printing it on Console

include "stdafx.h"
include "opencv2/imgproc/imgproc.hpp"
include "opencv2/highgui/highgui.hpp"
include <stdlib.h>
include <stdio.h>

using namespace cv;

int main( int argc, char** argv )
{
    IplImage *img = cvLoadImage("lena.jpg");
    CvMat *mat = cvCreateMat(img->height,img->width,CV_32FC3 );
    cvConvert( img, mat );
    for(int i=0;i<10;i++)
       {
         for(int j=0;j<10;j++)
           {
              CvScalar scal = cvGet2D( mat,j,i);
              printf( "(%.f,%.f,%.f)  ",scal.val[0], scal.val[1], scal.val[2] );
           }
         printf("\n");
       }
    waitKey(1);
    return 0;
 }

Is there better way to get each pixel value along with there header and channel values in txt.files

like image 575
Mukesh Avatar asked Dec 10 '22 01:12

Mukesh


1 Answers

I would recommend against using the C API if you can help it. The C++ API is much easier to use. As for storage of a matrix in a file, have a look at the FileStorage class available in OpenCV.

It's as easy as:

Write

FileStorage fs("test.yml", FileStorage::WRITE);
Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
fs << "cameraMatrix" << cameraMatrix;
...
fs.release();

Read

FileStorage fs("test.yml", FileStorage::READ);

Mat cameraMatrixFromFile;
fs["cameraMatrix"] >> cameraMatrixFromFile;
...
fs.release();

Hope that helps!

like image 84
mevatron Avatar answered Mar 20 '23 22:03

mevatron