2011年7月30日土曜日

cvWrite, cvRead

プロジェクトに昔のファイルが入ってるから、使いまわしで分かるように書いておく。

int write_data(char *filename, Mat a_mat)
{     
        // ファイルを開く

CvFileStorage *fs;
        fs = cvOpenFileStorage (filename, 0, CV_STORAGE_WRITE);
if(! fs ){
cout<<"can not open "<<filename<<endl;
return 0;
}

        // Mat -> CvMat
CvMat a = a_mat;
     
        // 書き込む
cvWrite (fs, "a", &a);

        // ファイルを閉じる
cvReleaseFileStorage (&fs);
}


int read_data(char *filename)
{
        CvFileStorage *fs;
CvFileNode *param;
// (2)パラメータファイルの読み込み
fs = cvOpenFileStorage (filename, 0, CV_STORAGE_READ);
if(!fs){
cout <<"can not open "<< filename << endl;
return 0;
}

CvMat *a;

param = cvGetFileNodeByName (fs, NULL, "a"); 
        a = (CvMat *) cvRead (fs, param);
cvReleaseFileStorage (&fs);

        // CvMat → Mat
Mat a_mat;
a_mat = Mat(a);

        // あとでdoubleでメモリにアクセスする場合のために,要素の型変換を行う
        a.convertTo(a, CV_64F);

        return 1;
}

0 件のコメント:

コメントを投稿