struct my_struct { int value; CvPoint point; CvRect rect; }; void write_my_struct(CvFileStorage* fs, const char* name, my_struct* ms) { cvWriteString(fs, "name", name); cvStartWriteStruct(fs, "Value", CV_NODE_SEQ); cvWriteInt(fs, 0, ms->value); cvEndWriteStruct(fs); cvStartWriteStruct(fs, "Point", CV_NODE_SEQ); cvWriteInt(fs, 0, ms->point.x); cvWriteInt(fs, 0, ms->point.y); cvEndWriteStruct(fs); cvStartWriteStruct(fs, "Rect", CV_NODE_SEQ); cvWriteInt(fs, 0, ms->rect.x); cvWriteInt(fs, 0, ms->rect.y); cvWriteInt(fs, 0, ms->rect.width); cvWriteInt(fs, 0, ms->rect.height); cvEndWriteStruct(fs); cvReleaseFileStorage(&fs); } void read_my_struct(CvFileStorage* fs, CvFileNode* ms_node, my_struct* ms) { ms->value = cvReadIntByName(fs, 0, "Value"); CvSeq* seq = cvGetFileNodeByName(fs, 0, "Point")->data.seq; ms->point = cvPoint(cvReadInt((CvFileNode*)cvGetSeqElem(seq, 0)), cvReadInt((CvFileNode*)cvGetSeqElem(seq, 1))); seq = cvGetFileNodeByName(fs, 0, "Rect")->data.seq; ms->rect = cvRect( cvReadInt((CvFileNode*)cvGetSeqElem(seq, 0)), cvReadInt((CvFileNode*)cvGetSeqElem(seq, 1)), cvReadInt((CvFileNode*)cvGetSeqElem(seq, 2)), cvReadInt((CvFileNode*)cvGetSeqElem(seq, 3)) ); cvReleaseFileStorage(&fs); } void myType8() { my_struct ms = {100, cvPoint(200, 200), cvRect(50, 50, 100, 100)}; CvFileStorage* fs = cvOpenFileStorage("my_struct.xml", 0, CV_STORAGE_WRITE); write_my_struct(fs, "my_struct", &ms); my_struct ms_read; fs = cvOpenFileStorage("my_struct.xml", 0, CV_STORAGE_READ); read_my_struct(fs, 0, &ms_read); std::cout<<ms_read.value<<std::endl; }
《学习OpenCV(中午版)》第3章 练习8
最新推荐文章于 2024-11-01 16:26:24 发布