forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_ml_inner_functions.cpp
1688 lines (1347 loc) · 51.1 KB
/
old_ml_inner_functions.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "old_ml_precomp.hpp"
CvStatModel::CvStatModel()
{
default_model_name = "my_stat_model";
}
CvStatModel::~CvStatModel()
{
clear();
}
void CvStatModel::clear()
{
}
void CvStatModel::save( const char* filename, const char* name ) const
{
CvFileStorage* fs = 0;
CV_FUNCNAME( "CvStatModel::save" );
__BEGIN__;
CV_CALL( fs = cvOpenFileStorage( filename, 0, CV_STORAGE_WRITE ));
if( !fs )
CV_ERROR( CV_StsError, "Could not open the file storage. Check the path and permissions" );
write( fs, name ? name : default_model_name );
__END__;
cvReleaseFileStorage( &fs );
}
void CvStatModel::load( const char* filename, const char* name )
{
CvFileStorage* fs = 0;
CV_FUNCNAME( "CvAlgorithm::load" );
__BEGIN__;
CvFileNode* model_node = 0;
CV_CALL( fs = cvOpenFileStorage( filename, 0, CV_STORAGE_READ ));
if( !fs )
EXIT;
if( name )
model_node = cvGetFileNodeByName( fs, 0, name );
else
{
CvFileNode* root = cvGetRootFileNode( fs );
if( root->data.seq->total > 0 )
model_node = (CvFileNode*)cvGetSeqElem( root->data.seq, 0 );
}
read( fs, model_node );
__END__;
cvReleaseFileStorage( &fs );
}
void CvStatModel::write( CvFileStorage*, const char* ) const
{
OPENCV_ERROR( CV_StsNotImplemented, "CvStatModel::write", "" );
}
void CvStatModel::read( CvFileStorage*, CvFileNode* )
{
OPENCV_ERROR( CV_StsNotImplemented, "CvStatModel::read", "" );
}
CvMat* icvGenerateRandomClusterCenters ( int seed, const CvMat* data,
int num_of_clusters, CvMat* _centers )
{
CvMat* centers = _centers;
CV_FUNCNAME("icvGenerateRandomClusterCenters");
__BEGIN__;
CvRNG rng;
CvMat data_comp, centers_comp;
CvPoint minLoc, maxLoc; // Not used, just for function "cvMinMaxLoc"
double minVal, maxVal;
int i;
int dim = data ? data->cols : 0;
if( ICV_IS_MAT_OF_TYPE(data, CV_32FC1) )
{
if( _centers && !ICV_IS_MAT_OF_TYPE (_centers, CV_32FC1) )
{
CV_ERROR(CV_StsBadArg,"");
}
else if( !_centers )
CV_CALL(centers = cvCreateMat (num_of_clusters, dim, CV_32FC1));
}
else if( ICV_IS_MAT_OF_TYPE(data, CV_64FC1) )
{
if( _centers && !ICV_IS_MAT_OF_TYPE (_centers, CV_64FC1) )
{
CV_ERROR(CV_StsBadArg,"");
}
else if( !_centers )
CV_CALL(centers = cvCreateMat (num_of_clusters, dim, CV_64FC1));
}
else
CV_ERROR (CV_StsBadArg,"");
if( num_of_clusters < 1 )
CV_ERROR (CV_StsBadArg,"");
rng = cvRNG(seed);
for (i = 0; i < dim; i++)
{
CV_CALL(cvGetCol (data, &data_comp, i));
CV_CALL(cvMinMaxLoc (&data_comp, &minVal, &maxVal, &minLoc, &maxLoc));
CV_CALL(cvGetCol (centers, ¢ers_comp, i));
CV_CALL(cvRandArr (&rng, ¢ers_comp, CV_RAND_UNI, cvScalarAll(minVal), cvScalarAll(maxVal)));
}
__END__;
if( (cvGetErrStatus () < 0) || (centers != _centers) )
cvReleaseMat (¢ers);
return _centers ? _centers : centers;
} // end of icvGenerateRandomClusterCenters
static int CV_CDECL
icvCmpIntegers( const void* a, const void* b )
{
return *(const int*)a - *(const int*)b;
}
static int CV_CDECL
icvCmpIntegersPtr( const void* _a, const void* _b )
{
int a = **(const int**)_a;
int b = **(const int**)_b;
return (a < b ? -1 : 0)|(a > b);
}
static int icvCmpSparseVecElems( const void* a, const void* b )
{
return ((CvSparseVecElem32f*)a)->idx - ((CvSparseVecElem32f*)b)->idx;
}
CvMat*
cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, bool check_for_duplicates )
{
CvMat* idx = 0;
CV_FUNCNAME( "cvPreprocessIndexArray" );
__BEGIN__;
int i, idx_total, idx_selected = 0, step, type, prev = INT_MIN, is_sorted = 1;
uchar* srcb = 0;
int* srci = 0;
int* dsti;
if( !CV_IS_MAT(idx_arr) )
CV_ERROR( CV_StsBadArg, "Invalid index array" );
if( idx_arr->rows != 1 && idx_arr->cols != 1 )
CV_ERROR( CV_StsBadSize, "the index array must be 1-dimensional" );
idx_total = idx_arr->rows + idx_arr->cols - 1;
srcb = idx_arr->data.ptr;
srci = idx_arr->data.i;
type = CV_MAT_TYPE(idx_arr->type);
step = CV_IS_MAT_CONT(idx_arr->type) ? 1 : idx_arr->step/CV_ELEM_SIZE(type);
switch( type )
{
case CV_8UC1:
case CV_8SC1:
// idx_arr is array of 1's and 0's -
// i.e. it is a mask of the selected components
if( idx_total != data_arr_size )
CV_ERROR( CV_StsUnmatchedSizes,
"Component mask should contain as many elements as the total number of input variables" );
for( i = 0; i < idx_total; i++ )
idx_selected += srcb[i*step] != 0;
if( idx_selected == 0 )
CV_ERROR( CV_StsOutOfRange, "No components/input_variables is selected!" );
break;
case CV_32SC1:
// idx_arr is array of integer indices of selected components
if( idx_total > data_arr_size )
CV_ERROR( CV_StsOutOfRange,
"index array may not contain more elements than the total number of input variables" );
idx_selected = idx_total;
// check if sorted already
for( i = 0; i < idx_total; i++ )
{
int val = srci[i*step];
if( val >= prev )
{
is_sorted = 0;
break;
}
prev = val;
}
break;
default:
CV_ERROR( CV_StsUnsupportedFormat, "Unsupported index array data type "
"(it should be 8uC1, 8sC1 or 32sC1)" );
}
CV_CALL( idx = cvCreateMat( 1, idx_selected, CV_32SC1 ));
dsti = idx->data.i;
if( type < CV_32SC1 )
{
for( i = 0; i < idx_total; i++ )
if( srcb[i*step] )
*dsti++ = i;
}
else
{
for( i = 0; i < idx_total; i++ )
dsti[i] = srci[i*step];
if( !is_sorted )
qsort( dsti, idx_total, sizeof(dsti[0]), icvCmpIntegers );
if( dsti[0] < 0 || dsti[idx_total-1] >= data_arr_size )
CV_ERROR( CV_StsOutOfRange, "the index array elements are out of range" );
if( check_for_duplicates )
{
for( i = 1; i < idx_total; i++ )
if( dsti[i] <= dsti[i-1] )
CV_ERROR( CV_StsBadArg, "There are duplicated index array elements" );
}
}
__END__;
if( cvGetErrStatus() < 0 )
cvReleaseMat( &idx );
return idx;
}
CvMat*
cvPreprocessVarType( const CvMat* var_type, const CvMat* var_idx,
int var_count, int* response_type )
{
CvMat* out_var_type = 0;
CV_FUNCNAME( "cvPreprocessVarType" );
if( response_type )
*response_type = -1;
__BEGIN__;
int i, tm_size, tm_step;
//int* map = 0;
const uchar* src;
uchar* dst;
if( !CV_IS_MAT(var_type) )
CV_ERROR( var_type ? CV_StsBadArg : CV_StsNullPtr, "Invalid or absent var_type array" );
if( var_type->rows != 1 && var_type->cols != 1 )
CV_ERROR( CV_StsBadSize, "var_type array must be 1-dimensional" );
if( !CV_IS_MASK_ARR(var_type))
CV_ERROR( CV_StsUnsupportedFormat, "type mask must be 8uC1 or 8sC1 array" );
tm_size = var_type->rows + var_type->cols - 1;
tm_step = var_type->rows == 1 ? 1 : var_type->step/CV_ELEM_SIZE(var_type->type);
if( /*tm_size != var_count &&*/ tm_size != var_count + 1 )
CV_ERROR( CV_StsBadArg,
"type mask must be of <input var count> + 1 size" );
if( response_type && tm_size > var_count )
*response_type = var_type->data.ptr[var_count*tm_step] != 0;
if( var_idx )
{
if( !CV_IS_MAT(var_idx) || CV_MAT_TYPE(var_idx->type) != CV_32SC1 ||
(var_idx->rows != 1 && var_idx->cols != 1) || !CV_IS_MAT_CONT(var_idx->type) )
CV_ERROR( CV_StsBadArg, "var index array should be continuous 1-dimensional integer vector" );
if( var_idx->rows + var_idx->cols - 1 > var_count )
CV_ERROR( CV_StsBadSize, "var index array is too large" );
//map = var_idx->data.i;
var_count = var_idx->rows + var_idx->cols - 1;
}
CV_CALL( out_var_type = cvCreateMat( 1, var_count, CV_8UC1 ));
src = var_type->data.ptr;
dst = out_var_type->data.ptr;
for( i = 0; i < var_count; i++ )
{
//int idx = map ? map[i] : i;
assert( (unsigned)/*idx*/i < (unsigned)tm_size );
dst[i] = (uchar)(src[/*idx*/i*tm_step] != 0);
}
__END__;
return out_var_type;
}
CvMat*
cvPreprocessOrderedResponses( const CvMat* responses, const CvMat* sample_idx, int sample_all )
{
CvMat* out_responses = 0;
CV_FUNCNAME( "cvPreprocessOrderedResponses" );
__BEGIN__;
int i, r_type, r_step;
const int* map = 0;
float* dst;
int sample_count = sample_all;
if( !CV_IS_MAT(responses) )
CV_ERROR( CV_StsBadArg, "Invalid response array" );
if( responses->rows != 1 && responses->cols != 1 )
CV_ERROR( CV_StsBadSize, "Response array must be 1-dimensional" );
if( responses->rows + responses->cols - 1 != sample_count )
CV_ERROR( CV_StsUnmatchedSizes,
"Response array must contain as many elements as the total number of samples" );
r_type = CV_MAT_TYPE(responses->type);
if( r_type != CV_32FC1 && r_type != CV_32SC1 )
CV_ERROR( CV_StsUnsupportedFormat, "Unsupported response type" );
r_step = responses->step ? responses->step / CV_ELEM_SIZE(responses->type) : 1;
if( r_type == CV_32FC1 && CV_IS_MAT_CONT(responses->type) && !sample_idx )
{
out_responses = cvCloneMat( responses );
EXIT;
}
if( sample_idx )
{
if( !CV_IS_MAT(sample_idx) || CV_MAT_TYPE(sample_idx->type) != CV_32SC1 ||
(sample_idx->rows != 1 && sample_idx->cols != 1) || !CV_IS_MAT_CONT(sample_idx->type) )
CV_ERROR( CV_StsBadArg, "sample index array should be continuous 1-dimensional integer vector" );
if( sample_idx->rows + sample_idx->cols - 1 > sample_count )
CV_ERROR( CV_StsBadSize, "sample index array is too large" );
map = sample_idx->data.i;
sample_count = sample_idx->rows + sample_idx->cols - 1;
}
CV_CALL( out_responses = cvCreateMat( 1, sample_count, CV_32FC1 ));
dst = out_responses->data.fl;
if( r_type == CV_32FC1 )
{
const float* src = responses->data.fl;
for( i = 0; i < sample_count; i++ )
{
int idx = map ? map[i] : i;
assert( (unsigned)idx < (unsigned)sample_all );
dst[i] = src[idx*r_step];
}
}
else
{
const int* src = responses->data.i;
for( i = 0; i < sample_count; i++ )
{
int idx = map ? map[i] : i;
assert( (unsigned)idx < (unsigned)sample_all );
dst[i] = (float)src[idx*r_step];
}
}
__END__;
return out_responses;
}
CvMat*
cvPreprocessCategoricalResponses( const CvMat* responses,
const CvMat* sample_idx, int sample_all,
CvMat** out_response_map, CvMat** class_counts )
{
CvMat* out_responses = 0;
int** response_ptr = 0;
CV_FUNCNAME( "cvPreprocessCategoricalResponses" );
if( out_response_map )
*out_response_map = 0;
if( class_counts )
*class_counts = 0;
__BEGIN__;
int i, r_type, r_step;
int cls_count = 1, prev_cls, prev_i;
const int* map = 0;
const int* srci;
const float* srcfl;
int* dst;
int* cls_map;
int* cls_counts = 0;
int sample_count = sample_all;
if( !CV_IS_MAT(responses) )
CV_ERROR( CV_StsBadArg, "Invalid response array" );
if( responses->rows != 1 && responses->cols != 1 )
CV_ERROR( CV_StsBadSize, "Response array must be 1-dimensional" );
if( responses->rows + responses->cols - 1 != sample_count )
CV_ERROR( CV_StsUnmatchedSizes,
"Response array must contain as many elements as the total number of samples" );
r_type = CV_MAT_TYPE(responses->type);
if( r_type != CV_32FC1 && r_type != CV_32SC1 )
CV_ERROR( CV_StsUnsupportedFormat, "Unsupported response type" );
r_step = responses->rows == 1 ? 1 : responses->step / CV_ELEM_SIZE(responses->type);
if( sample_idx )
{
if( !CV_IS_MAT(sample_idx) || CV_MAT_TYPE(sample_idx->type) != CV_32SC1 ||
(sample_idx->rows != 1 && sample_idx->cols != 1) || !CV_IS_MAT_CONT(sample_idx->type) )
CV_ERROR( CV_StsBadArg, "sample index array should be continuous 1-dimensional integer vector" );
if( sample_idx->rows + sample_idx->cols - 1 > sample_count )
CV_ERROR( CV_StsBadSize, "sample index array is too large" );
map = sample_idx->data.i;
sample_count = sample_idx->rows + sample_idx->cols - 1;
}
CV_CALL( out_responses = cvCreateMat( 1, sample_count, CV_32SC1 ));
if( !out_response_map )
CV_ERROR( CV_StsNullPtr, "out_response_map pointer is NULL" );
CV_CALL( response_ptr = (int**)cvAlloc( sample_count*sizeof(response_ptr[0])));
srci = responses->data.i;
srcfl = responses->data.fl;
dst = out_responses->data.i;
for( i = 0; i < sample_count; i++ )
{
int idx = map ? map[i] : i;
assert( (unsigned)idx < (unsigned)sample_all );
if( r_type == CV_32SC1 )
dst[i] = srci[idx*r_step];
else
{
float rf = srcfl[idx*r_step];
int ri = cvRound(rf);
if( ri != rf )
{
char buf[100];
sprintf( buf, "response #%d is not integral", idx );
CV_ERROR( CV_StsBadArg, buf );
}
dst[i] = ri;
}
response_ptr[i] = dst + i;
}
qsort( response_ptr, sample_count, sizeof(int*), icvCmpIntegersPtr );
// count the classes
for( i = 1; i < sample_count; i++ )
cls_count += *response_ptr[i] != *response_ptr[i-1];
if( cls_count < 2 )
CV_ERROR( CV_StsBadArg, "There is only a single class" );
CV_CALL( *out_response_map = cvCreateMat( 1, cls_count, CV_32SC1 ));
if( class_counts )
{
CV_CALL( *class_counts = cvCreateMat( 1, cls_count, CV_32SC1 ));
cls_counts = (*class_counts)->data.i;
}
// compact the class indices and build the map
prev_cls = ~*response_ptr[0];
cls_count = -1;
cls_map = (*out_response_map)->data.i;
for( i = 0, prev_i = -1; i < sample_count; i++ )
{
int cur_cls = *response_ptr[i];
if( cur_cls != prev_cls )
{
if( cls_counts && cls_count >= 0 )
cls_counts[cls_count] = i - prev_i;
cls_map[++cls_count] = prev_cls = cur_cls;
prev_i = i;
}
*response_ptr[i] = cls_count;
}
if( cls_counts )
cls_counts[cls_count] = i - prev_i;
__END__;
cvFree( &response_ptr );
return out_responses;
}
const float**
cvGetTrainSamples( const CvMat* train_data, int tflag,
const CvMat* var_idx, const CvMat* sample_idx,
int* _var_count, int* _sample_count,
bool always_copy_data )
{
float** samples = 0;
CV_FUNCNAME( "cvGetTrainSamples" );
__BEGIN__;
int i, j, var_count, sample_count, s_step, v_step;
bool copy_data;
const float* data;
const int *s_idx, *v_idx;
if( !CV_IS_MAT(train_data) )
CV_ERROR( CV_StsBadArg, "Invalid or NULL training data matrix" );
var_count = var_idx ? var_idx->cols + var_idx->rows - 1 :
tflag == CV_ROW_SAMPLE ? train_data->cols : train_data->rows;
sample_count = sample_idx ? sample_idx->cols + sample_idx->rows - 1 :
tflag == CV_ROW_SAMPLE ? train_data->rows : train_data->cols;
if( _var_count )
*_var_count = var_count;
if( _sample_count )
*_sample_count = sample_count;
copy_data = tflag != CV_ROW_SAMPLE || var_idx || always_copy_data;
CV_CALL( samples = (float**)cvAlloc(sample_count*sizeof(samples[0]) +
(copy_data ? 1 : 0)*var_count*sample_count*sizeof(samples[0][0])) );
data = train_data->data.fl;
s_step = train_data->step / sizeof(samples[0][0]);
v_step = 1;
s_idx = sample_idx ? sample_idx->data.i : 0;
v_idx = var_idx ? var_idx->data.i : 0;
if( !copy_data )
{
for( i = 0; i < sample_count; i++ )
samples[i] = (float*)(data + (s_idx ? s_idx[i] : i)*s_step);
}
else
{
samples[0] = (float*)(samples + sample_count);
if( tflag != CV_ROW_SAMPLE )
CV_SWAP( s_step, v_step, i );
for( i = 0; i < sample_count; i++ )
{
float* dst = samples[i] = samples[0] + i*var_count;
const float* src = data + (s_idx ? s_idx[i] : i)*s_step;
if( !v_idx )
for( j = 0; j < var_count; j++ )
dst[j] = src[j*v_step];
else
for( j = 0; j < var_count; j++ )
dst[j] = src[v_idx[j]*v_step];
}
}
__END__;
return (const float**)samples;
}
void
cvCheckTrainData( const CvMat* train_data, int tflag,
const CvMat* missing_mask,
int* var_all, int* sample_all )
{
CV_FUNCNAME( "cvCheckTrainData" );
if( var_all )
*var_all = 0;
if( sample_all )
*sample_all = 0;
__BEGIN__;
// check parameter types and sizes
if( !CV_IS_MAT(train_data) || CV_MAT_TYPE(train_data->type) != CV_32FC1 )
CV_ERROR( CV_StsBadArg, "train data must be floating-point matrix" );
if( missing_mask )
{
if( !CV_IS_MAT(missing_mask) || !CV_IS_MASK_ARR(missing_mask) ||
!CV_ARE_SIZES_EQ(train_data, missing_mask) )
CV_ERROR( CV_StsBadArg,
"missing value mask must be 8-bit matrix of the same size as training data" );
}
if( tflag != CV_ROW_SAMPLE && tflag != CV_COL_SAMPLE )
CV_ERROR( CV_StsBadArg,
"Unknown training data layout (must be CV_ROW_SAMPLE or CV_COL_SAMPLE)" );
if( var_all )
*var_all = tflag == CV_ROW_SAMPLE ? train_data->cols : train_data->rows;
if( sample_all )
*sample_all = tflag == CV_ROW_SAMPLE ? train_data->rows : train_data->cols;
__END__;
}
int
cvPrepareTrainData( const char* /*funcname*/,
const CvMat* train_data, int tflag,
const CvMat* responses, int response_type,
const CvMat* var_idx,
const CvMat* sample_idx,
bool always_copy_data,
const float*** out_train_samples,
int* _sample_count,
int* _var_count,
int* _var_all,
CvMat** out_responses,
CvMat** out_response_map,
CvMat** out_var_idx,
CvMat** out_sample_idx )
{
int ok = 0;
CvMat* _var_idx = 0;
CvMat* _sample_idx = 0;
CvMat* _responses = 0;
int sample_all = 0, sample_count = 0, var_all = 0, var_count = 0;
CV_FUNCNAME( "cvPrepareTrainData" );
// step 0. clear all the output pointers to ensure we do not try
// to call free() with uninitialized pointers
if( out_responses )
*out_responses = 0;
if( out_response_map )
*out_response_map = 0;
if( out_var_idx )
*out_var_idx = 0;
if( out_sample_idx )
*out_sample_idx = 0;
if( out_train_samples )
*out_train_samples = 0;
if( _sample_count )
*_sample_count = 0;
if( _var_count )
*_var_count = 0;
if( _var_all )
*_var_all = 0;
__BEGIN__;
if( !out_train_samples )
CV_ERROR( CV_StsBadArg, "output pointer to train samples is NULL" );
CV_CALL( cvCheckTrainData( train_data, tflag, 0, &var_all, &sample_all ));
if( sample_idx )
CV_CALL( _sample_idx = cvPreprocessIndexArray( sample_idx, sample_all ));
if( var_idx )
CV_CALL( _var_idx = cvPreprocessIndexArray( var_idx, var_all ));
if( responses )
{
if( !out_responses )
CV_ERROR( CV_StsNullPtr, "output response pointer is NULL" );
if( response_type == CV_VAR_NUMERICAL )
{
CV_CALL( _responses = cvPreprocessOrderedResponses( responses,
_sample_idx, sample_all ));
}
else
{
CV_CALL( _responses = cvPreprocessCategoricalResponses( responses,
_sample_idx, sample_all, out_response_map, 0 ));
}
}
CV_CALL( *out_train_samples =
cvGetTrainSamples( train_data, tflag, _var_idx, _sample_idx,
&var_count, &sample_count, always_copy_data ));
ok = 1;
__END__;
if( ok )
{
if( out_responses )
*out_responses = _responses, _responses = 0;
if( out_var_idx )
*out_var_idx = _var_idx, _var_idx = 0;
if( out_sample_idx )
*out_sample_idx = _sample_idx, _sample_idx = 0;
if( _sample_count )
*_sample_count = sample_count;
if( _var_count )
*_var_count = var_count;
if( _var_all )
*_var_all = var_all;
}
else
{
if( out_response_map )
cvReleaseMat( out_response_map );
cvFree( out_train_samples );
}
if( _responses != responses )
cvReleaseMat( &_responses );
cvReleaseMat( &_var_idx );
cvReleaseMat( &_sample_idx );
return ok;
}
typedef struct CvSampleResponsePair
{
const float* sample;
const uchar* mask;
int response;
int index;
}
CvSampleResponsePair;
static int
CV_CDECL icvCmpSampleResponsePairs( const void* a, const void* b )
{
int ra = ((const CvSampleResponsePair*)a)->response;
int rb = ((const CvSampleResponsePair*)b)->response;
int ia = ((const CvSampleResponsePair*)a)->index;
int ib = ((const CvSampleResponsePair*)b)->index;
return ra < rb ? -1 : ra > rb ? 1 : ia - ib;
//return (ra > rb ? -1 : 0)|(ra < rb);
}
void
cvSortSamplesByClasses( const float** samples, const CvMat* classes,
int* class_ranges, const uchar** mask )
{
CvSampleResponsePair* pairs = 0;
CV_FUNCNAME( "cvSortSamplesByClasses" );
__BEGIN__;
int i, k = 0, sample_count;
if( !samples || !classes || !class_ranges )
CV_ERROR( CV_StsNullPtr, "INTERNAL ERROR: some of the args are NULL pointers" );
if( classes->rows != 1 || CV_MAT_TYPE(classes->type) != CV_32SC1 )
CV_ERROR( CV_StsBadArg, "classes array must be a single row of integers" );
sample_count = classes->cols;
CV_CALL( pairs = (CvSampleResponsePair*)cvAlloc( (sample_count+1)*sizeof(pairs[0])));
for( i = 0; i < sample_count; i++ )
{
pairs[i].sample = samples[i];
pairs[i].mask = (mask) ? (mask[i]) : 0;
pairs[i].response = classes->data.i[i];
pairs[i].index = i;
assert( classes->data.i[i] >= 0 );
}
qsort( pairs, sample_count, sizeof(pairs[0]), icvCmpSampleResponsePairs );
pairs[sample_count].response = -1;
class_ranges[0] = 0;
for( i = 0; i < sample_count; i++ )
{
samples[i] = pairs[i].sample;
if (mask)
mask[i] = pairs[i].mask;
classes->data.i[i] = pairs[i].response;
if( pairs[i].response != pairs[i+1].response )
class_ranges[++k] = i+1;
}
__END__;
cvFree( &pairs );
}
void
cvPreparePredictData( const CvArr* _sample, int dims_all,
const CvMat* comp_idx, int class_count,
const CvMat* prob, float** _row_sample,
int as_sparse )
{
float* row_sample = 0;
int* inverse_comp_idx = 0;
CV_FUNCNAME( "cvPreparePredictData" );
__BEGIN__;
const CvMat* sample = (const CvMat*)_sample;
float* sample_data;
int sample_step;
int is_sparse = CV_IS_SPARSE_MAT(sample);
int d, sizes[CV_MAX_DIM];
int i, dims_selected;
int vec_size;
if( !is_sparse && !CV_IS_MAT(sample) )
CV_ERROR( !sample ? CV_StsNullPtr : CV_StsBadArg, "The sample is not a valid vector" );
if( cvGetElemType( sample ) != CV_32FC1 )
CV_ERROR( CV_StsUnsupportedFormat, "Input sample must have 32fC1 type" );
CV_CALL( d = cvGetDims( sample, sizes ));
if( !((is_sparse && d == 1) || (!is_sparse && d == 2 && (sample->rows == 1 || sample->cols == 1))) )
CV_ERROR( CV_StsBadSize, "Input sample must be 1-dimensional vector" );
if( d == 1 )
sizes[1] = 1;
if( sizes[0] + sizes[1] - 1 != dims_all )
CV_ERROR( CV_StsUnmatchedSizes,
"The sample size is different from what has been used for training" );
if( !_row_sample )
CV_ERROR( CV_StsNullPtr, "INTERNAL ERROR: The row_sample pointer is NULL" );
if( comp_idx && (!CV_IS_MAT(comp_idx) || comp_idx->rows != 1 ||
CV_MAT_TYPE(comp_idx->type) != CV_32SC1) )
CV_ERROR( CV_StsBadArg, "INTERNAL ERROR: invalid comp_idx" );
dims_selected = comp_idx ? comp_idx->cols : dims_all;
if( prob )
{
if( !CV_IS_MAT(prob) )
CV_ERROR( CV_StsBadArg, "The output matrix of probabilities is invalid" );
if( (prob->rows != 1 && prob->cols != 1) ||
(CV_MAT_TYPE(prob->type) != CV_32FC1 &&
CV_MAT_TYPE(prob->type) != CV_64FC1) )
CV_ERROR( CV_StsBadSize,
"The matrix of probabilities must be 1-dimensional vector of 32fC1 type" );
if( prob->rows + prob->cols - 1 != class_count )
CV_ERROR( CV_StsUnmatchedSizes,
"The vector of probabilities must contain as many elements as "
"the number of classes in the training set" );
}
vec_size = !as_sparse ? dims_selected*sizeof(row_sample[0]) :
(dims_selected + 1)*sizeof(CvSparseVecElem32f);
if( CV_IS_MAT(sample) )
{
sample_data = sample->data.fl;
sample_step = CV_IS_MAT_CONT(sample->type) ? 1 : sample->step/sizeof(row_sample[0]);
if( !comp_idx && CV_IS_MAT_CONT(sample->type) && !as_sparse )
*_row_sample = sample_data;
else
{
CV_CALL( row_sample = (float*)cvAlloc( vec_size ));
if( !comp_idx )
for( i = 0; i < dims_selected; i++ )
row_sample[i] = sample_data[sample_step*i];
else
{
int* comp = comp_idx->data.i;
for( i = 0; i < dims_selected; i++ )
row_sample[i] = sample_data[sample_step*comp[i]];
}
*_row_sample = row_sample;
}
if( as_sparse )
{
const float* src = (const float*)row_sample;
CvSparseVecElem32f* dst = (CvSparseVecElem32f*)row_sample;
dst[dims_selected].idx = -1;
for( i = dims_selected - 1; i >= 0; i-- )
{
dst[i].idx = i;
dst[i].val = src[i];
}
}
}
else
{
CvSparseNode* node;
CvSparseMatIterator mat_iterator;
const CvSparseMat* sparse = (const CvSparseMat*)sample;
assert( is_sparse );
node = cvInitSparseMatIterator( sparse, &mat_iterator );
CV_CALL( row_sample = (float*)cvAlloc( vec_size ));