1
#include <highgui.h>
2
#include <cv.h>
3
#include <stdio.h>
4
5
#pragma
comment (lib,"opencv_calib3d231d.lib")
6
#pragma
comment (lib,"opencv_contrib231d.lib")
7
#pragma
comment (lib,"opencv_core231d.lib")
8
#pragma
comment (lib,"opencv_features2d231d.lib")
9
#pragma
comment (lib,"opencv_flann231d.lib")
10
#pragma
comment (lib,"opencv_gpu231d.lib")
11
#pragma
comment (lib,"opencv_haartraining_engined.lib")
12
#pragma
comment (lib,"opencv_highgui231d.lib")
13
#pragma
comment (lib,"opencv_imgproc231d.lib")
14
#pragma
comment (lib,"opencv_legacy231d.lib")
15
#pragma
comment (lib,"opencv_ml231d.lib")
16
#pragma
comment (lib,"opencv_objdetect231d.lib")
17
#pragma
comment (lib,"opencv_ts231d.lib")
18
#pragma
comment (lib,"opencv_video231d.lib")
19
20
/*
21
*《學(xué)習(xí)OpenCV》第三章第八題b
22
* 完成時間:19:46 4/4 星期四 2013
23
*/
24
25
#define
ARRAY_LENGTH 10
//
數(shù)組長度
26
27
typedef
struct
my_struct
28
{
29
int
i;
30
CvPoint point;
31
CvRect rect;
32
} MyStruct;
33
34
void
write_my_struct(CvFileStorage * fs,
const
char
* name, my_struct*
ms)
35
{
36
//
開始寫數(shù)據(jù)
37
cvStartWriteStruct(fs, name,
6
);
38
39
//
寫入一個 整數(shù)
40
cvStartWriteStruct(fs,
"
integer
"
,CV_NODE_SEQ);
41
cvWriteInt(fs,NULL,ms->
i);
42
cvEndWriteStruct(fs);
43
44
//
寫入cvpoint結(jié)構(gòu)
45
cvStartWriteStruct(fs,
"
CvPoint
"
,CV_NODE_SEQ);
46
cvWriteInt(fs,NULL,ms->
point.x);
47
cvWriteInt(fs,NULL,ms->
point.y);
48
cvEndWriteStruct(fs);
49
50
//
寫入rect結(jié)構(gòu)體
51
cvStartWriteStruct(fs,
"
CvRect
"
,CV_NODE_SEQ);
52
cvWriteInt(fs,NULL,ms->
rect.x);
53
cvWriteInt(fs,NULL,ms->
rect.y);
54
cvWriteInt(fs,NULL,ms->
rect.height);
55
cvWriteInt(fs,NULL,ms->
rect.width);
56
cvEndWriteStruct(fs);
57
58
//
結(jié)束寫數(shù)據(jù)
59
cvEndWriteStruct(fs);
60
}
61
62
void
read_my_struct(CvFileStorage* fs, CvFileNode* ms_node, my_struct*
ms)
63
{
64
//
讀第一個整數(shù)
65
//
注意:這里應(yīng)使用node->data.i的value來讀取Integer
66
int
i = cvGetFileNodeByName(fs, ms_node,
"
integer
"
)->
data.i;
67
ms->i =
i;
68
69
//
讀CvPoint結(jié)構(gòu)
70
CvSeq *s1 = cvGetFileNodeByName(fs, ms_node,
"
CvPoint
"
)->
data.seq;
71
CvPoint point;
72
point.x= cvReadInt((CvFileNode*)cvGetSeqElem(s1,
0
));
73
point.y= cvReadInt((CvFileNode*)cvGetSeqElem(s1,
1
));
74
ms->point =
point;
75
76
//
讀取CvRect結(jié)構(gòu)
77
CvSeq *s2 = cvGetFileNodeByName(fs, ms_node,
"
CvRect
"
)->
data.seq;
78
CvRect rect;
79
rect.x=cvReadInt((CvFileNode*)cvGetSeqElem(s2,
0
));
80
rect.y=cvReadInt((CvFileNode*)cvGetSeqElem(s2,
1
));
81
rect.width=cvReadInt((CvFileNode*)cvGetSeqElem(s2,
3
));
82
rect.height=cvReadInt((CvFileNode*)cvGetSeqElem(s2,
2
));
83
ms->rect =
rect;
84
}
85
86
//
將MyStruct的值顯示出來
87
void
ShowStructValue(MyStruct*
pvalue)
88
{
89
printf(
"
integer:%d\n
"
, pvalue->
i);
90
printf(
"
CvPoint: (%d, %d)\n
"
, pvalue->point.x, pvalue->
point.y );
91
printf(
"
CvRect: h-->%d\tw-->%d\t(%d, %d)\n
"
, pvalue->
rect.height,
92
pvalue->rect.width, pvalue->rect.x, pvalue->
rect.y);
93
}
94
95
//
檢查兩個MyStruct是否一致
96
bool
check(MyStruct* msValue1, MyStruct*
msValue2)
97
{
98
if
( (msValue1->i == msValue2->i) &&
99
(msValue1->point.x == msValue2->point.x) &&
100
(msValue1->point.y == msValue2->point.y) &&
101
(msValue1->rect.height == msValue2->rect.height) &&
102
(msValue1->rect.width == msValue2->rect.width) &&
103
(msValue1->rect.x == msValue2->rect.x) &&
104
(msValue1->rect.y == msValue2->
rect.y) )
105
return
true
;
106
else
107
return
false
;
108
}
109
110
int
main()
111
{
112
/*
寫數(shù)據(jù)部分
*/
113
MyStruct msArray[ARRAY_LENGTH];
114
115
CvFileStorage* fs = cvOpenFileStorage(
"
My_struct.xml
"
,
0
, CV_STORAGE_WRITE);
116
char
pchTag[
12
];
117
//
隨機生成數(shù)據(jù)
118
for
(
int
i =
0
; i < ARRAY_LENGTH; i++
)
119
{
120
CvRNG rng =
cvRNG(cvGetTickCount());
121
122
msArray[i].i = cvRandInt(&rng) %
256
;
123
msArray[i].point = cvPoint( cvRandInt(&rng) %
1000
, cvRandInt(&rng) %
1000
);
124
msArray[i].rect = cvRect( cvRandInt(&rng) %
1000
, cvRandInt(&rng) %
1000
,
125
cvRandInt(&rng) %
600
, cvRandInt(&rng) %
600
);
126
127
//
最后一個整數(shù)計數(shù)
128
sprintf( pchTag,
"
my_struct_%d
"
, i );
129
write_my_struct(fs, pchTag, &
msArray[i]);
130
}
131
132
cvReleaseFileStorage(&
fs);
133
134
/*
讀數(shù)據(jù)部分
*/
135
fs = cvOpenFileStorage(
"
My_struct.xml
"
, NULL, CV_STORAGE_READ );
136
MyStruct msArrayRead[ARRAY_LENGTH];
137
CvFileNode *
pnode;
138
139
for
(
int
i =
0
; i < ARRAY_LENGTH; i++
)
140
{
141
sprintf( pchTag,
"
my_struct_%d
"
, i );
142
pnode =
cvGetFileNodeByName(fs, NULL, pchTag);
143
read_my_struct( fs, pnode, &
msArrayRead[i] );
144
145
//
顯示
146
printf(
"
---------------------- %d: Write -------------------------\n
"
, i);
147
ShowStructValue( &
msArray[i]);
148
printf(
"
---------------------- %d: Read --------------------------\n
"
, i);
149
ShowStructValue( &
msArrayRead[i]);
150
//
檢查讀寫是否一致
151
if
(check(&msArray[i], &
msArrayRead[i] ))
152
{
153
printf(
"
Consistent?:\tAnswer: True\n
"
);
154
}
155
else
156
{
157
printf(
"
Consistent?:\tAnswer: False\n
"
);
158
}
159
getchar();
160
}
161
162
cvReleaseFileStorage(&
fs);
163
164
return
0
;
165
}
對應(yīng)的XML文件:
?
運行結(jié)果:
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

