OpenCV Image To Matrix在txt文件中

时间:2012-04-02 06:36:02

标签: opencv computer-vision

我这样做是为了获取Image的每个像素值并在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;
 }

是否有更好的方法来获取每个像素值以及txt.files中的标题和通道值

1 个答案:

答案 0 :(得分:14)

如果您能提供帮助,我建议不要使用C API。 C ++ API更容易使用。至于在文件中存储矩阵,请查看OpenCV中可用的FileStorage类。

这很简单:

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();

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

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

希望有所帮助!