-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.cpp
562 lines (541 loc) · 28.4 KB
/
training.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#include <QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/ml.h>
#include <opencv/cxcore.h>
#include <fstream>
//#include <string >
#include <sstream>
#include <iostream>
using namespace cv;
using namespace std;
void main()//int argc, int *argv[]
{
// step 1:
//训练数据的分类标记,即4类
// float labels[16] = {1.0, 1.0,1.0,1.0,2.0,2.0,2.0,2.0,3.0,3.0,3.0,3.0,4.0,4.0,4.0,4.0};
float labels1[300] ={ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 };
float labels[120] ={
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 ,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 ,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 ,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
};
Mat labelsMat (120, 1, CV_32FC1, labels);
//训练数据矩阵
// float trainingData[16][2] = { {0, 0}, {4, 1}, {4, 5}, {-1, 6},{3,11},{-2,10},{4,30},{0,25},{10,13},{15,12},{25,40},{11,35},{8,1},{9,6},{15,5},{20,-1} };
/* float trainingData[300][10] = {
{ 1, 71, 321, 71, 0, 191, 2207, 23, 18, 12 },
{ 1, 37, 81, 37, 0, 172, 2969, 13, 7, 7 },
{ 1, 50, 173, 50, 0, 214, 2539, 17, 12, 9 },
{ 3, 70, 288, 43, 386, 223, 2072, 27, 13, 14 },
{ 1, 58, 221, 58, 0, 220, 2374, 19, 15, 10 },
{ 1, 67, 279, 67, 0, 225, 1976, 22, 18, 11 },
{ 1, 49, 167, 49, 0, 222, 2260, 15, 13, 8 },
{ 1, 53, 165, 53, 0, 209, 3178, 20, 10, 10 },
{ 1, 50, 173, 50, 0, 224, 2016, 15, 14, 8 },
{ 1, 121, 955, 121, 0, 200, 1388, 35, 34, 20 },
{ 2, 86, 514, 63, 551, 234, 1632, 26, 23, 15 },
{ 2, 83, 442, 53, 870, 223, 2877, 24, 23, 14 },
{ 1, 68, 247, 68, 0, 214, 2554, 25, 12, 13 },
{ 4, 95, 584, 40, 989, 229, 2195, 30, 23, 16 },
{ 1, 113, 789, 113, 0, 231, 1265, 36, 29, 21 },
{ 2, 123, 836, 77, 2158, 234, 1508, 35, 30, 19 },
{ 1, 87, 448, 87, 0, 219, 2924, 30, 21, 15 },
{ 1, 87, 422, 87, 0, 225, 2623, 29, 20, 15 },
{ 2, 168, 1370, 96, 5252, 229, 2152, 54, 35, 28 },
{ 1, 100, 612, 100, 0, 230, 2026, 36, 22, 18 },
{ 2, 73, 344, 49, 582, 228, 2058, 24, 18, 12 },
{ 3, 70, 284, 42, 405, 219, 2516, 27, 13, 14 },
{ 1, 63, 262, 63, 0, 220, 2797, 19, 17, 11 },
{ 2, 99, 595, 61, 1432, 237, 1124, 32, 24, 17 },
{ 1, 66, 207, 66, 0, 194, 3781, 25, 12, 13 },
{ 2, 91, 496, 61, 849, 229, 2069, 34, 19, 17 },
{ 1, 68, 303, 68, 0, 227, 2009, 20, 20, 12 },
{ 2, 102, 583, 65, 1388, 237, 1177, 33, 25, 19 },
{ 2, 96, 545, 64, 1037, 230, 1852, 30, 26, 16 },
{ 2, 91, 496, 61, 849, 229, 2069, 34, 19, 17 },
{ 1, 85, 427, 85, 0, 226, 2547, 27, 21, 15 },
{ 1, 88, 455, 88, 0, 225, 2168, 31, 21, 16 },
{ 2, 108, 618, 65, 1846, 228, 2439, 37, 23, 19 },
{ 1, 98, 550, 98, 0, 230, 2009, 33, 24, 17 },
{ 5, 131, 1132, 48, 1698, 237, 1736, 40, 37, 22 },
{ 1, 106, 728, 106, 0, 232, 1904, 36, 22, 19 },
{ 1, 88, 293, 88, 0, 202, 3192, 33, 13, 17 },
{ 5, 52, 168, 36, 149, 194, 2155, 17, 12, 9 },
{ 3, 74, 338, 42, 530, 211, 3453, 22, 21, 13 },
{ 4, 54, 177, 31, 186, 183, 2365, 18, 13, 9 },
{ 1, 121, 314, 121, 0, 190, 2886, 31, 19, 16 },
{ 2, 111, 573, 70, 1664, 228, 2341, 34, 25, 18 },
{ 4, 117, 860, 46, 1701, 232, 2247, 38, 29, 22 },
{ 1, 104, 578, 104, 0, 233, 1462, 30, 29, 18 },
{ 6, 117, 860, 35, 1350, 232, 2247, 38, 29, 22 },
{ 6, 132, 1114, 45, 1565, 238, 1623, 38, 38, 22 },
{ 3, 109, 591, 58, 1283, 221, 3126, 34, 26, 18 },
{ 2, 48, 153, 35, 157, 211, 2374, 15, 12, 8 },
{ 1, 52, 177, 52, 0, 223, 2083, 15, 14, 8 },
{ 2, 54, 138, 34, 402, 173, 4632, 19, 11, 9 },
{ 4, 537, 2165, 166, 46525, 147, 1879, 134, 53, 72 },
{ 7, 283, 851, 73, 7715, 168, 4020, 128, 12, 66 },
{ 5, 299, 787, 98, 11503, 123, 2163, 88, 42, 45 },
{ 2, 286, 752, 184, 10507, 149, 2609, 106, 25, 54 },
{ 6, 385, 1160, 87, 17720, 155, 3853, 155, 29, 79 },
{ 1, 302, 832, 302, 0, 136, 561, 133, 14, 68 },
{ 4, 406, 1111, 115, 28278, 101, 467, 171, 16, 88 },
{ 3, 480, 1287, 172, 47582, 110, 640, 210, 14, 108 },
{ 7, 561, 1835, 133, 35862, 159, 3449, 227, 30, 117 },
{ 4, 230, 593, 80, 7569, 92, 267, 102, 11, 52 },
{ 2, 162, 404, 89, 5274, 139, 1904, 68, 19, 35 },
{ 12, 602, 1921, 86, 24885, 165, 3296, 204, 39, 104 },
{ 10, 466, 1449, 77, 17089, 156, 3934, 192, 30, 99 },
{ 4, 315, 832, 122, 13876, 147, 2548, 112, 29, 58 },
{ 1, 126, 337, 126, 0, 97, 213, 58, 7, 30 },
{ 9, 1204, 4188, 222, 129325, 103, 1367, 373, 111, 191 },
{ 1, 393, 1174, 393, 0, 116, 391, 170, 17, 87 },
{ 4, 218, 540, 76, 6897, 98, 228, 97, 9, 49 },
{ 5, 386, 897, 106, 19749, 102, 813, 83, 75, 54 },
{ 1, 387, 1029, 387, 0, 117, 401, 144, 18, 74 },
{ 3, 104, 250, 48, 1625, 156, 1608, 46, 7, 23 },
{ 3, 449, 1293, 165, 40417, 136, 2386, 205, 15, 105 },
{ 4, 318, 854, 95, 16480, 124, 1716, 134, 19, 69 },
{ 1, 185, 474, 185, 0, 107, 537, 83, 10, 42 },
{ 1, 528, 2048, 528, 0, 99, 578, 160, 45, 83 },
{ 4, 176, 432, 60, 4529, 109, 1494, 80, 9, 41 },
{ 3, 50, 109, 31, 185, 86, 583, 17, 10, 9 },
{ 4, 244, 548, 77, 9366, 87, 340, 107, 10, 55 },
{ 6, 364, 975, 82, 15913, 108, 1738, 163, 11, 83 },
{ 5, 372, 1061, 95, 19133, 100, 1118, 87, 72, 53 },
{ 3, 292, 774, 110, 16492, 106, 945, 132, 13, 68 },
{ 4, 1062, 3732, 295, 196537, 100, 1333, 260, 78, 132 },
{ 18, 1034, 2851, 79, 53714, 102, 1330, 406, 64, 210 },
{ 4, 393, 1071, 133, 22950, 106, 976, 172, 18, 88 },
{ 3, 315, 980, 126, 18031, 103, 946, 134, 18, 69 },
{ 8, 404, 1153, 69, 16023, 102, 514, 161, 25, 82 },
{ 4, 386, 1015, 166, 24396, 105, 1677, 118, 24, 61 },
{ 4, 466, 1796, 144, 34886, 97, 1351, 135, 33, 70 },
{ 4, 263, 850, 81, 11015, 165, 3352, 111, 16, 57 },
{ 2, 201, 529, 110, 8207, 126, 1773, 79, 19, 41 },
{ 2, 247, 590, 137, 12131, 111, 364, 112, 12, 57 },
{ 1, 378, 1042, 378, 0, 95, 312, 171, 16, 88 },
{ 3, 453, 1038, 163, 42110, 108, 1209, 202, 19, 104 },
{ 4, 372, 649, 107, 23403, 93, 165, 118, 41, 60 },
{ 1, 382, 1046, 382, 0, 94, 287, 173, 15, 89 },
{ 7, 577, 2018, 134, 34646, 105, 745, 222, 35, 114 },
{ 12, 506, 1610, 70, 17557, 155, 3915, 207, 31, 107 },
{ 1, 285, 751, 285, 0, 93, 206, 128, 10, 66 },
{ 1, 132, 336, 132, 0, 97, 540, 53, 15, 27 },
{ 3, 347, 927, 139, 21663, 108, 1350, 160, 13, 82 },
{ 8, 318, 854, 99, 11563, 124, 1716, 134, 19, 69 },
{ 115, 147, 173, 34, 803, 158, 2525, 47, 16, 24 },
{ 93, 182, 310, 32, 718, 225, 1742, 37, 24, 21 },
{ 24, 163, 479, 35, 1035, 209, 2606, 35, 27, 19 },
{ 36, 267, 393, 55, 2615, 169, 1021, 55, 29, 30 },
{ 28, 325, 608, 60, 3899, 177, 1768, 51, 47, 31 },
{ 41, 273, 391, 40, 2000, 197, 2187, 65, 27, 33 },
{ 20, 145, 80, 49, 1622, 178, 394, 29, 15, 15 },
{ 95, 325, 608, 45, 2280, 177, 1768, 51, 47, 31 },
{ 83, 426, 463, 38, 2936, 170, 1446, 131, 21, 67 },
{ 126, 214, 188, 32, 1073, 128, 350, 85, 10, 43 },
{ 39, 312, 108, 50, 4093, 188, 1994, 98, 16, 50 },
{ 31, 91, 77, 27, 264, 189, 3267, 21, 14, 11 },
{ 39, 249, 423, 33, 1337, 219, 1477, 45, 24, 23 },
{ 38, 252, 90, 36, 1633, 133, 192, 55, 21, 28 },
{ 16, 134, 196, 27, 801, 172, 1895, 37, 16, 20 },
{ 19, 230, 523, 33, 2248, 177, 2696, 49, 23, 26 },
{ 37, 76, 64, 29, 200, 147, 319, 26, 7, 14 },
{ 57, 251, 404, 39, 2744, 163, 2247, 48, 23, 26 },
{ 29, 230, 523, 34, 1893, 177, 2696, 49, 23, 26 },
{ 67, 181, 226, 30, 607, 189, 502, 40, 21, 21 },
{ 28, 48, 70, 24, 66, 228, 895, 20, 7, 10 },
{ 12, 121, 77, 37, 845, 212, 1471, 30, 10, 15 },
{ 20, 162, 336, 37, 1343, 208, 1614, 35, 27, 21 },
{ 32, 141, 30, 42, 1064, 137, 110, 34, 9, 17 },
{ 51, 162, 336, 32, 689, 208, 1614, 35, 27, 21 },
{ 28, 75, 86, 31, 330, 158, 477, 26, 11, 13 },
{ 37, 75, 113, 31, 272, 194, 2418, 31, 8, 16 },
{ 35, 75, 113, 31, 278, 194, 2418, 31, 8, 16 },
{ 34, 110, 89, 31, 389, 158, 477, 41, 11, 21 },
{ 60, 110, 89, 32, 372, 158, 477, 41, 11, 21 },
{ 40, 110, 89, 28, 304, 158, 477, 41, 11, 21 },
{ 31, 88, 46, 31, 241, 176, 663, 24, 6, 12 },
{ 17, 99, 160, 33, 461, 201, 1999, 31, 16, 16 },
{ 19, 105, 42, 29, 407, 158, 1152, 46, 6, 24 },
{ 65, 105, 42, 32, 404, 158, 1152, 46, 6, 24 },
{ 39, 103, 79, 29, 284, 231, 1131, 24, 10, 12 },
{ 12, 74, 94, 33, 296, 183, 1930, 23, 9, 12 },
{ 26, 48, 33, 23, 54, 241, 570, 15, 6, 8 },
{ 17, 65, 44, 29, 152, 213, 719, 24, 7, 12 },
{ 21, 187, 110, 36, 1344, 254, 7, 29, 24, 16 },
{ 51, 74, 94, 27, 158, 183, 1930, 23, 9, 12 },
{ 45, 62, 108, 26, 94, 251, 111, 21, 8, 10 },
{ 14, 115, 182, 35, 586, 234, 1174, 33, 16, 17 },
{ 26, 64, 61, 32, 186, 164, 776, 20, 8, 10 },
{ 20, 141, 142, 36, 729, 165, 749, 33, 13, 17 },
{ 27, 116, 217, 34, 596, 208, 2116, 37, 18, 19 },
{ 11, 97, 113, 41, 448, 242, 627, 23, 12, 12 },
{ 61, 141, 142, 35, 584, 165, 749, 33, 13, 17 },
{ 27, 181, 287, 37, 998, 230, 1215, 48, 24, 26 },
{ 8, 1011, 4311, 170, 102480, 110, 362, 181, 114, 97 },
{ 7, 425, 3557, 147, 26522, 116, 571, 132, 52, 67 },
{ 16, 332, 767, 43, 5660, 94, 233, 70, 35, 36 },
{ 2, 436, 3515, 228, 43336, 106, 335, 82, 69, 46 },
{ 6, 430, 1752, 106, 22170, 129, 371, 107, 51, 54 },
{ 3, 425, 3557, 169, 32894, 116, 571, 132, 52, 67 },
{ 2, 332, 1342, 179, 23524, 111, 444, 81, 50, 41 },
{ 2, 796, 5675, 416, 144151, 125, 796, 167, 71, 85 },
{ 5, 615, 1913, 162, 51553, 101, 264, 104, 68, 55 },
{ 2, 384, 1108, 198, 34345, 98, 221, 88, 55, 46 },
{ 3, 539, 3223, 210, 54267, 118, 537, 127, 52, 65 },
{ 1, 528, 2059, 528, 0, 99, 317, 78, 67, 42 },
{ 3, 91, 245, 42, 1230, 157, 1132, 38, 9, 19 },
{ 1, 382, 2875, 382, 0, 107, 333, 105, 63, 54 },
{ 2, 171, 763, 96, 5623, 107, 247, 46, 30, 25 },
{ 3, 436, 3516, 161, 37700, 106, 335, 82, 69, 46 },
{ 7, 416, 1007, 84, 18570, 96, 225, 90, 45, 46 },
{ 4, 435, 1808, 187, 23966, 110, 416, 76, 58, 43 },
{ 1, 143, 334, 143, 0, 126, 357, 26, 25, 17 },
{ 4, 414, 4938, 129, 27033, 150, 659, 90, 74, 52 },
{ 4, 430, 1938, 127, 30688, 128, 360, 107, 51, 54 },
{ 5, 579, 5767, 142, 47913, 126, 602, 114, 76, 65 },
{ 7, 457, 1536, 120, 25045, 122, 568, 106, 43, 56 },
{ 3, 385, 3729, 143, 29205, 144, 757, 81, 64, 47 },
{ 7, 1051, 8497, 178, 127237, 127, 634, 208, 76, 111 },
{ 6, 944, 3656, 204, 109871, 125, 319, 171, 62, 90 },
{ 3, 314, 1354, 130, 16955, 124, 665, 91, 44, 47 },
{ 2, 190, 969, 114, 5711, 107, 424, 55, 35, 30 },
{ 5, 395, 980, 134, 22815, 117, 258, 112, 27, 58 },
{ 1, 230, 1204, 230, 0, 118, 600, 49, 42, 30 },
{ 3, 248, 431, 105, 10480, 99, 123, 66, 16, 34 },
{ 4, 156, 425, 86, 2744, 98, 278, 48, 19, 24 },
{ 2, 347, 1410, 186, 25798, 110, 436, 87, 51, 45 },
{ 3, 411, 3528, 163, 31052, 117, 567, 126, 52, 64 },
{ 2, 146, 424, 108, 1430, 116, 253, 38, 20, 20 },
{ 5, 968, 4204, 237, 134777, 110, 353, 163, 110, 87 },
{ 1, 489, 2492, 489, 0, 117, 301, 89, 66, 50 },
{ 1, 589, 3196, 589, 0, 115, 308, 123, 69, 63 },
{ 2, 436, 3515, 228, 43336, 106, 335, 82, 69, 46 },
{ 11, 332, 767, 54, 7857, 94, 233, 70, 35, 36 },
{ 3, 429, 1748, 162, 35671, 129, 372, 106, 51, 54 },
{ 3, 320, 1372, 133, 17617, 124, 665, 91, 45, 47 },
{ 5, 156, 546, 93, 2905, 113, 436, 43, 26, 22 },
{ 3, 122, 543, 77, 1784, 106, 277, 34, 26, 19 },
{ 2, 190, 623, 155, 1231, 112, 785, 74, 19, 38 },
{ 4, 200, 689, 65, 6071, 96, 424, 70, 17, 36 },
{ 3, 131, 231, 84, 1134, 101, 131, 43, 14, 23 },
{ 4, 489, 3201, 152, 37838, 118, 590, 119, 54, 60 },
{ 3, 347, 3411, 134, 22720, 152, 754, 70, 62, 44 },
{ 2, 360, 1389, 193, 27940, 110, 442, 87, 51, 45 },
{ 1, 298, 2617, 298, 0, 171, 6639, 69, 65, 41 },
{ 2, 187, 1939, 107, 6390, 153, 8177, 51, 48, 31 },
{ 3, 145, 924, 74, 2512, 171, 5119, 43, 34, 22 },
{ 2, 112, 579, 67, 2002, 186, 4538, 36, 26, 19 },
{ 1, 107, 690, 107, 0, 195, 3494, 31, 31, 19 },
{ 1, 95, 566, 95, 0, 197, 3654, 28, 25, 17 },
{ 7, 302, 2675, 97, 8619, 185, 6207, 69, 66, 40 },
{ 4, 203, 958, 78, 5360, 139, 5662, 62, 35, 32 },
{ 1, 194, 639, 194, 0, 179, 4740, 42, 40, 28 },
{ 1, 165, 857, 165, 0, 186, 4087, 49, 28, 26 },
{ 3, 203, 958, 94, 6068, 139, 5662, 62, 35, 32 },
{ 1, 110, 550, 110, 0, 181, 4972, 36, 21, 18 },
{ 1, 95, 577, 95, 0, 180, 5373, 28, 26, 16 },
{ 2, 132, 764, 84, 2319, 146, 6265, 39, 29, 20 },
{ 1, 73, 378, 73, 0, 164, 5985, 22, 21, 12 },
{ 3, 261, 1038, 112, 11234, 140, 4967, 79, 37, 41 },
{ 4, 277, 1743, 110, 9564, 154, 6388, 84, 42, 45 },
{ 1, 94, 516, 94, 0, 172, 5008, 31, 22, 16 },
{ 1, 151, 563, 151, 0, 180, 4920, 41, 23, 21 },
{ 1, 74, 373, 74, 0, 152, 5441, 22, 21, 12 },
{ 4, 209, 1560, 80, 5579, 149, 6806, 51, 43, 30 },
{ 2, 220, 1900, 145, 5678, 188, 6081, 59, 47, 34 },
{ 2, 157, 1542, 100, 3301, 171, 7769, 51, 39, 27 },
{ 3, 158, 1534, 110, 1557, 177, 7330, 49, 43, 26 },
{ 4, 214, 1624, 104, 4558, 155, 6797, 58, 46, 33 },
{ 2, 166, 1422, 102, 4094, 157, 7568, 52, 38, 27 },
{ 3, 172, 1758, 75, 4729, 175, 7085, 54, 45, 30 },
{ 2, 162, 504, 109, 2774, 176, 4867, 50, 24, 26 },
{ 1, 75, 391, 75, 0, 168, 5408, 23, 21, 13 },
{ 6, 138, 758, 65, 1631, 197, 3730, 34, 33, 21 },
{ 3, 166, 1422, 122, 3455, 157, 7568, 52, 38, 27 },
{ 5, 145, 898, 53, 2163, 144, 5791, 47, 29, 25 },
{ 1, 134, 670, 134, 0, 185, 4310, 47, 22, 24 },
{ 3, 159, 870, 86, 3000, 159, 5869, 41, 31, 22 },
{ 1, 72, 368, 72, 0, 156, 5738, 21, 20, 12 },
{ 3, 395, 2207, 169, 25956, 163, 5217, 96, 64, 50 },
{ 3, 436, 2343, 183, 32390, 163, 5499, 75, 63, 46 },
{ 1, 106, 755, 106, 0, 151, 5870, 31, 30, 18 },
{ 2, 130, 721, 105, 644, 177, 4241, 42, 24, 21 },
{ 1, 338, 1552, 338, 0, 174, 4600, 69, 52, 36 },
{ 3, 128, 806, 67, 1912, 152, 5989, 40, 29, 21 },
{ 2, 98, 607, 65, 1109, 207, 3941, 31, 24, 17 },
{ 7, 246, 2012, 63, 5706, 169, 6022, 55, 55, 32 },
{ 6, 147, 877, 53, 1798, 211, 3304, 39, 34, 22 },
{ 2, 89, 554, 78, 129, 189, 4923, 29, 23, 16 },
{ 4, 128, 804, 57, 1808, 156, 6169, 38, 28, 21 },
{ 3, 239, 1076, 108, 8769, 160, 5843, 64, 33, 35 },
{ 4, 396, 1038, 141, 23147, 145, 4071, 55, 52, 33 },
{ 2, 166, 915, 98, 4622, 155, 6217, 37, 36, 21 },
{ 3, 156, 771, 92, 2380, 152, 6021, 53, 28, 27 },
{ 1, 196, 1352, 196, 0, 228, 2379, 74, 33, 38 },
{ 1, 222, 1734, 222, 0, 235, 1798, 80, 32, 41 },
{ 2, 165, 230, 112, 2735, 177, 3007, 39, 26, 21 },
{ 1, 125, 1014, 125, 0, 218, 2002, 36, 32, 21 },
{ 1, 129, 923, 129, 0, 219, 2625, 41, 31, 22 },
{ 1, 162, 1466, 162, 0, 145, 1693, 47, 43, 28 },
{ 1, 105, 631, 105, 0, 215, 3122, 35, 25, 19 },
{ 1, 158, 1044, 158, 0, 226, 2055, 57, 29, 30 },
{ 2, 162, 1050, 94, 4658, 222, 3334, 61, 29, 31 },
{ 1, 143, 835, 143, 0, 227, 2331, 55, 25, 28 },
{ 1, 143, 883, 143, 0, 222, 3214, 50, 27, 26 },
{ 1, 110, 665, 110, 0, 232, 2063, 34, 27, 20 },
{ 1, 197, 291, 197, 0, 173, 4284, 38, 26, 20 },
{ 1, 153, 875, 153, 0, 216, 3634, 40, 37, 24 },
{ 1, 153, 875, 153, 0, 216, 3634, 40, 37, 24 },
{ 1, 136, 635, 136, 0, 219, 2776, 56, 19, 29 },
{ 1, 194, 1093, 194, 0, 227, 2332, 56, 35, 30 },
{ 1, 151, 1081, 151, 0, 234, 1618, 45, 36, 26 },
{ 3, 159, 1008, 80, 3174, 224, 3084, 59, 28, 30 },
{ 1, 119, 856, 119, 0, 235, 1411, 39, 27, 20 },
{ 1, 135, 675, 135, 0, 205, 4620, 56, 19, 28 },
{ 2, 118, 154, 100, 341, 108, 282, 38, 20, 20 },
{ 1, 227, 305, 227, 0, 168, 3512, 50, 25, 26 },
{ 1, 175, 265, 175, 0, 170, 4286, 31, 26, 19 },
{ 1, 101, 625, 101, 0, 209, 2760, 30, 26, 18 },
{ 2, 108, 618, 58, 2440, 176, 1981, 42, 17, 21 },
{ 1, 123, 1005, 123, 0, 235, 1706, 35, 32, 21 },
{ 1, 92, 523, 92, 0, 213, 2773, 31, 22, 16 },
{ 1, 203, 312, 203, 0, 172, 3942, 39, 27, 21 },
{ 1, 162, 793, 162, 0, 214, 3572, 56, 26, 28 },
{ 1, 226, 1905, 226, 0, 229, 2394, 72, 50, 38 },
{ 1, 128, 561, 128, 0, 198, 4758, 52, 20, 26 },
{ 1, 180, 1312, 180, 0, 227, 2709, 67, 37, 34 },
{ 1, 156, 1443, 156, 0, 237, 1795, 46, 45, 27 },
{ 1, 134, 773, 134, 0, 215, 3849, 39, 31, 22 },
{ 1, 129, 176, 129, 0, 180, 3312, 24, 24, 15 },
{ 1, 147, 550, 147, 0, 207, 3568, 48, 25, 25 },
{ 1, 121, 147, 121, 0, 188, 3778, 36, 18, 19 },
{ 1, 160, 786, 160, 0, 213, 3734, 55, 26, 28 },
{ 1, 135, 776, 135, 0, 215, 3801, 37, 33, 22 },
{ 1, 155, 1011, 155, 0, 227, 2337, 51, 35, 27 },
{ 2, 196, 1558, 123, 5320, 235, 1943, 55, 46, 31 },
{ 1, 312, 418, 312, 0, 168, 3346, 52, 44, 29 },
{ 1, 122, 842, 122, 0, 224, 2722, 39, 28, 23 },
{ 1, 146, 946, 146, 0, 221, 3227, 52, 29, 26 },
{ 1, 139, 561, 139, 0, 209, 3533, 52, 24, 27 },
{ 1, 127, 823, 127, 0, 228, 2688, 48, 27, 25 },
{ 1, 131, 798, 131, 0, 207, 4543, 38, 28, 21 },
{ 1, 119, 584, 119, 0, 224, 2537, 48, 20, 25 },
{ 1, 194, 302, 194, 0, 155, 3906, 40, 26, 21 }
};
*/
float trainingData[120][10] = {
{ 5, 69, 338, 21, 585, 187, 2363, 23, 18, 12 },
{ 6, 39, 92, 15, 121, 163, 3055, 14, 8, 7 },
{ 3, 72, 224, 36, 675, 191, 3964, 23, 15, 12 },
{ 16, 77, 403, 16, 259, 227, 3069, 24, 21, 13 },
{ 5, 52, 183, 23, 279, 230, 1890, 16, 14, 9 },
{ 7, 34, 83, 16, 82, 193, 3625, 10, 10, 6 },
{ 10, 63, 194, 25, 362, 212, 2927, 20, 14, 11 },
{ 9, 56, 178, 17, 220, 205, 3061, 20, 12, 10 },
{ 4, 51, 175, 19, 343, 225, 1855, 15, 14, 8 },
{ 4, 63, 200, 27, 467, 213, 2801, 19, 14, 10 },
{ 11, 110, 577, 33, 782, 224, 2461, 28, 27, 18 },
{ 3, 49, 158, 19, 454, 203, 3071, 15, 13, 8 },
{ 3, 68, 221, 31, 692, 202, 3300, 21, 15, 11 },
{ 8, 108, 368, 23, 1023, 178, 3235, 31, 20, 17 },
{ 12, 54, 169, 21, 148, 189, 1856, 18, 13, 9 },
{ 26, 57, 181, 17, 111, 207, 2746, 16, 15, 9 },
{ 7, 91, 454, 20, 847, 220, 2562, 31, 21, 16 },
{ 3, 82, 301, 35, 1131, 233, 1262, 22, 21, 14 },
{ 6, 83, 220, 23, 710, 196, 3167, 29, 15, 15 },
{ 3, 62, 253, 29, 527, 225, 2157, 19, 17, 11 },
{ 10, 297, 585, 39, 7387, 205, 1265, 136, 6, 70 },
{ 6, 286, 725, 58, 10483, 186, 2779, 128, 12, 66 },
{ 13, 350, 620, 38, 8166, 129, 1062, 160, 12, 82 },
{ 8, 278, 429, 42, 7956, 104, 149, 127, 8, 65 },
{ 10, 352, 870, 42, 10679, 179, 2944, 155, 23, 79 },
{ 21, 481, 1239, 34, 10073, 178, 2979, 208, 27, 107 },
{ 14, 382, 627, 38, 9124, 105, 268, 173, 13, 89 },
{ 12, 403, 636, 52, 11463, 131, 489, 185, 7, 95 },
{ 17, 407, 1020, 33, 8775, 186, 2596, 177, 15, 90 },
{ 16, 378, 629, 37, 7858, 106, 309, 170, 13, 88 },
{ 3, 155, 196, 60, 4555, 183, 865, 68, 13, 35 },
{ 8, 453, 1061, 65, 21518, 209, 1729, 205, 19, 105 },
{ 16, 430, 1122, 38, 10319, 179, 2997, 187, 23, 95 },
{ 6, 257, 458, 53, 8322, 122, 234, 109, 12, 56 },
{ 1, 243, 489, 243, 0, 212, 1258, 111, 8, 57 },
{ 12, 254, 417, 35, 4462, 135, 190, 112, 9, 57 },
{ 7, 286, 532, 49, 9331, 148, 1561, 128, 11, 66 },
{ 16, 305, 619, 28, 5101, 118, 1029, 134, 13, 69 },
{ 12, 175, 312, 28, 2066, 128, 1039, 80, 8, 41 },
{ 3, 101, 200, 43, 1699, 170, 1331, 45, 6, 23 },
{ 40, 107, 265, 27, 434, 214, 1683, 22, 21, 14 },
{ 66, 82, 163, 25, 289, 194, 2124, 30, 9, 15 },
{ 65, 168, 647, 27, 563, 155, 195, 37, 29, 20 },
{ 24, 80, 131, 23, 263, 163, 1506, 23, 12, 12 },
{ 19, 78, 157, 31, 526, 185, 2035, 20, 15, 11 },
{ 73, 144, 405, 29, 604, 153, 321, 54, 17, 28 },
{ 52, 87, 189, 23, 278, 167, 2107, 33, 11, 17 },
{ 35, 71, 121, 32, 269, 172, 684, 23, 10, 12 },
{ 47, 65, 141, 26, 231, 184, 2716, 15, 15, 9 },
{ 35, 64, 127, 26, 230, 191, 2549, 20, 10, 11 },
{ 88, 68, 146, 23, 204, 167, 2287, 24, 9, 12 },
{ 44, 64, 127, 25, 231, 191, 2549, 20, 10, 11 },
{ 41, 70, 125, 23, 173, 177, 2321, 23, 9, 12 },
{ 61, 70, 129, 23, 200, 175, 2320, 23, 9, 12 },
{ 26, 94, 236, 30, 382, 177, 2274, 34, 12, 17 },
{ 68, 263, 714, 28, 1475, 173, 2098, 63, 37, 35 },
{ 34, 64, 127, 26, 252, 191, 2549, 20, 10, 11 },
{ 49, 88, 199, 24, 232, 197, 1823, 21, 16, 12 },
{ 35, 96, 253, 27, 383, 173, 2264, 34, 14, 17 },
{ 33, 94, 193, 25, 339, 180, 2210, 33, 10, 17 },
{ 2, 294, 1821, 158, 18334, 131, 394, 82, 37, 42 },
{ 3, 167, 835, 64, 5349, 138, 468, 54, 26, 28 },
{ 5, 298, 1686, 68, 13233, 134, 258, 57, 55, 33 },
{ 10, 343, 2789, 45, 9854, 112, 262, 83, 59, 43 },
{ 11, 298, 1686, 44, 6568, 134, 258, 57, 55, 33 },
{ 4, 298, 1900, 84, 15296, 130, 388, 84, 37, 43 },
{ 7, 298, 1686, 53, 10090, 134, 258, 57, 55, 32 },
{ 6, 313, 2400, 69, 12007, 146, 638, 65, 58, 38 },
{ 5, 343, 2435, 81, 17213, 114, 260, 68, 61, 41 },
{ 8, 222, 858, 43, 4713, 106, 174, 61, 32, 32 },
{ 2, 328, 1967, 169, 25362, 131, 338, 89, 39, 46 },
{ 5, 308, 1760, 71, 14020, 109, 257, 58, 57, 35 },
{ 3, 136, 613, 52, 3591, 120, 181, 32, 31, 18 },
{ 2, 284, 1712, 154, 17095, 131, 402, 81, 37, 42 },
{ 8, 199, 783, 34, 3905, 122, 388, 55, 27, 29 },
{ 7, 343, 2435, 61, 13233, 114, 260, 68, 61, 40 },
{ 11, 373, 1585, 49, 10540, 103, 154, 67, 56, 38 },
{ 4, 118, 586, 43, 1937, 131, 526, 36, 22, 19 },
{ 5, 178, 858, 48, 4266, 110, 381, 56, 27, 29 },
{ 4, 155, 682, 45, 4027, 143, 451, 51, 23, 26 },
{ 8, 245, 2316, 44, 5915, 175, 7284, 64, 54, 38 },
{ 4, 174, 1851, 51, 5078, 154, 8486, 50, 48, 29 },
{ 15, 171, 862, 23, 1600, 176, 5437, 43, 35, 25 },
{ 4, 87, 490, 30, 1090, 196, 4657, 25, 24, 14 },
{ 16, 186, 869, 31, 1994, 153, 5332, 43, 36, 27 },
{ 12, 150, 716, 29, 1523, 162, 6993, 38, 29, 21 },
{ 5, 123, 620, 35, 2057, 188, 4551, 38, 24, 22 },
{ 10, 161, 739, 29, 2021, 146, 6677, 42, 35, 24 },
{ 17, 126, 768, 33, 1233, 215, 3548, 41, 27, 21 },
{ 15, 126, 648, 20, 848, 157, 7422, 36, 26, 18 },
{ 8, 156, 718, 32, 2322, 146, 6860, 40, 35, 24 },
{ 18, 89, 363, 20, 353, 161, 5600, 27, 20, 15 },
{ 16, 130, 742, 27, 1026, 162, 6823, 38, 31, 21 },
{ 8, 103, 607, 24, 933, 150, 7672, 31, 27, 18 },
{ 12, 79, 372, 19, 374, 169, 5815, 25, 19, 14 },
{ 9, 176, 849, 30, 2673, 152, 6111, 48, 34, 26 },
{ 14, 82, 368, 22, 458, 174, 5715, 25, 19, 13 },
{ 4, 95, 487, 35, 1229, 178, 4931, 31, 22, 16 },
{ 5, 107, 491, 57, 1326, 195, 5042, 36, 22, 19 },
{ 18, 86, 385, 19, 336, 156, 5078, 28, 19, 15 } ,
{ 5, 208, 1258, 50, 6194, 238, 1182, 73, 33, 38 },
{ 2, 221, 1582, 114, 11430, 246, 572, 79, 32, 40 },
{ 3, 127, 610, 51, 2933, 220, 2812, 48, 20, 26 },
{ 6, 153, 827, 33, 2886, 207, 4153, 41, 35, 22 },
{ 5, 129, 904, 33, 2332, 224, 1955, 40, 31, 23 },
{ 5, 128, 820, 36, 2151, 231, 2213, 48, 27, 24 },
{ 2, 157, 932, 84, 5270, 226, 2444, 53, 29, 28 },
{ 4, 300, 1585, 96, 14155, 199, 2993, 84, 36, 44 },
{ 3, 173, 1035, 67, 5683, 226, 2660, 62, 30, 32 },
{ 4, 128, 834, 40, 2605, 228, 2161, 42, 27, 23 },
{ 7, 123, 787, 27, 1558, 213, 3881, 36, 33, 21 },
{ 2, 115, 667, 65, 2521, 234, 1666, 35, 29, 21 },
{ 2, 120, 758, 65, 2965, 232, 1883, 39, 26, 21 },
{ 6, 163, 785, 47, 3260, 215, 3285, 55, 26, 28 },
{ 3, 97, 391, 38, 1699, 225, 2439, 26, 25, 16 },
{ 14, 128, 810, 19, 921, 211, 4085, 41, 30, 21 },
{ 3, 182, 1240, 67, 6594, 236, 1496, 65, 37, 33 },
{ 8, 166, 775, 32, 2619, 219, 2907, 56, 26, 28 },
{ 1, 124, 765, 124, 0, 230, 1877, 39, 27, 21 },
{ 2, 134, 642, 72, 3851, 214, 3497, 56, 19, 29 }/**/
};
Mat trainingDataMat (120, 10, CV_32FC1, trainingData);
// step 2:
//训练参数设定
CvSVMParams params;
params.svm_type = CvSVM::C_SVC; //SVM类型
params.kernel_type = CvSVM::LINEAR ; //核函数的类型
//SVM训练过程的终止条件, max_iter:最大迭代次数 epsilon:结果的精确性
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, FLT_EPSILON );
// step 3:
//启动训练过程
CvSVM SVM;
SVM.train( trainingDataMat, labelsMat, Mat(),Mat(), params);
SVM.save("C:\\Users\\william\\Desktop\\钢板缺陷\\svm0204_5.xml");
// step 4:
//使用训练所得模型对新样本进行分类测试
/*
for (int i=-5; i<15; i++)
{
for (int j=-5; j<15; j++)
{
float a[] = {i,j};
CvMat sampleMat;
cvInitMatHeader(&sampleMat,1,2,CV_32FC1,a);
cvmSet(&sampleMat,0,0,i); // Set M(i,j)
cvmSet(&sampleMat,0,1,j); // Set M(i,j)
float response = SVM.predict(&sampleMat);
cout<<response<<" ";
}
cout<<endl;
}
*/
for (int i=0; i<120; i++)
{
// float a[] = trainingData[i];
// Mat sampleMat = trainingData[i];
// CvMat sampleMat;
Mat sampleMat = (Mat_<float>(1,10) << trainingData[i][0],trainingData[i][1],trainingData[i][2],trainingData[i][3],trainingData[i][4],
trainingData[i][5],trainingData[i][6],trainingData[i][7],trainingData[i][8],trainingData[i][9]);
// cvInitMatHeader(&sampleMat,1,10,CV_32FC1,trainingData[i]);
float response = SVM.predict(sampleMat);
if(i%20==0)
cout<<endl<<endl;
cout<<response<<", ";
}
// { 2, 190, 623, 155, 1231, 112, 785, 74, 19, 38 };
float a[10] = { 19, 105, 42, 29, 407, 158, 1152, 46, 6, 24 } ;//{ 7, 686, 1973, 137, 50846, 115, 344, 203, 62, 106 };
CvMat sampleMat1;
cvInitMatHeader(&sampleMat1,1,10,CV_32FC1,a);
float response1 = SVM.predict(&sampleMat1);
cout<<"\n\nresponse1: "<<response1<<"\n\n";
/* cvmSet(&sampleMat,0,0,60); // Set M(i,j)
cvmSet(&sampleMat,0,1,110);
cvmSet(&sampleMat,0,2,89);
cvmSet(&sampleMat,0,3,32);
cvmSet(&sampleMat,0,4,372);
cvmSet(&sampleMat,0,5,158);
cvmSet(&sampleMat,0,6,477);
cvmSet(&sampleMat,0,7,41);
cvmSet(&sampleMat,0,8,11);
cvmSet(&sampleMat,0,9,21); // Set M(i,j)
// step 5:
//获取支持向量
int c = SVM.get_support_vector_count();
cout<<endl;
for (int i=0; i<c; i++)
{
const float* v = SVM.get_support_vector(i);
cout<<*v<<", ";
}
cout<<endl;
*/
system("pause");
// return 0;
waitKey();
// return a.exec();
}