Announce

PukiWiki contents have been moved into SONOTS Plugin (20070703)

OpenCV XML I/O

Example1

// somewhere deep in your code…you get 5x5
// matrix that you’d want to save
{
CvMat A = cvMat( 5, 5, CV_32F, the_matrix_data);
cvSave( “my_matrix.xml”, &A );
// cvSave( “my_matrix.xml”, &A, "my_matrix", "comment" );
}
// to load it then in some other program use …
CvMat* A1 = (CvMat*)cvLoad( “my_matrix.xml”);
<?xml version="1.0"?>
<opencv_storage>
  <my_matrix type_id="opencv-matrix">
  <rows>5</rows>
    <cols>5</cols>
    <dt>f</dt>
    <data>1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.0. 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.0. 1.</data>
  </my_matrix>
</opencv_storage>

Example2

CvFileStorage* fs=cvOpenFileStorage(“cfg.xml”, 0,CV_STORAGE_WRITE);
cvWriteInt( fs, “frame_count”, 10 );
cvWriteStartWriteStruct( fs, “frame_size”, CV_NODE_SEQ);
cvWriteInt( fs, 0, 320 );
cvWriteint( fs, 0, 200 );
cvEndWriteStruct(fs);
cvWrite( fs, “color_cvt_matrix”, cmatrix);
cvReleaseFileStorage( &fs);
<?xml version="1.0"?>
<opencv_storage>
  <frame_count>10</frame_count>
  <frame_size>320 200</frame_size>
  <color_cvt_matrix type_id="opencv-matrix">
    <rows>3</rows>
    <cols>3</cols>
    <dt>f</dt>
    <data>...</data>
  </color_cvt_matrix>
</opencv_storage>
CvFileStorage* fs= cvOpenFileStorage(“cfg.xml”, 0, CV_STORAGE_READ);
int frame_count=cvReadIntByName( fs, 0, “frame_count”, 10 /* default value */ );
CvSeq* s =cvGetFileNodeByName(fs,0,”frame_size”)->data.seq;
int frame_width= cvReadInt( (CvFileNode*)cvGetSeqElem(s,0) );
int frame_height= cvReadInt( (CvFileNode*)cvGetSeqElem(s,1) );
CvMat* color_cvt_matrix= (CvMat*)cvRead(fs,0,”color_cvt_matrix”);
cvReleaseFileStorage( &fs);