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
/*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 "precomp.hpp"
typedef struct CvDI
{
double d;
int i;
} CvDI;
static int CV_CDECL
icvCmpDI( const void* a, const void* b, void* )
{
const CvDI* e1 = (const CvDI*) a;
const CvDI* e2 = (const CvDI*) b;
return (e1->d < e2->d) ? -1 : (e1->d > e2->d);
}
CV_IMPL void
cvCreateTestSet( int type, CvMat** samples,
int num_samples,
int num_features,
CvMat** responses,
int num_classes, ... )
{
CvMat* mean = NULL;
CvMat* cov = NULL;
CvMemStorage* storage = NULL;
CV_FUNCNAME( "cvCreateTestSet" );
__BEGIN__;
if( samples )
*samples = NULL;
if( responses )
*responses = NULL;
if( type != CV_TS_CONCENTRIC_SPHERES )
CV_ERROR( CV_StsBadArg, "Invalid type parameter" );
if( !samples )
CV_ERROR( CV_StsNullPtr, "samples parameter must be not NULL" );
if( !responses )
CV_ERROR( CV_StsNullPtr, "responses parameter must be not NULL" );
if( num_samples < 1 )
CV_ERROR( CV_StsBadArg, "num_samples parameter must be positive" );
if( num_features < 1 )
CV_ERROR( CV_StsBadArg, "num_features parameter must be positive" );
if( num_classes < 1 )
CV_ERROR( CV_StsBadArg, "num_classes parameter must be positive" );
if( type == CV_TS_CONCENTRIC_SPHERES )
{
CvSeqWriter writer;
CvSeqReader reader;
CvMat sample;
CvDI elem;
CvSeq* seq = NULL;
int i, cur_class;
CV_CALL( *samples = cvCreateMat( num_samples, num_features, CV_32FC1 ) );
CV_CALL( *responses = cvCreateMat( 1, num_samples, CV_32SC1 ) );
CV_CALL( mean = cvCreateMat( 1, num_features, CV_32FC1 ) );
CV_CALL( cvSetZero( mean ) );
CV_CALL( cov = cvCreateMat( num_features, num_features, CV_32FC1 ) );
CV_CALL( cvSetIdentity( cov ) );
/* fill the feature values matrix with random numbers drawn from standard
normal distribution */
CV_CALL( cvRandMVNormal( mean, cov, *samples ) );
/* calculate distances from the origin to the samples and put them
into the sequence along with indices */
CV_CALL( storage = cvCreateMemStorage() );
CV_CALL( cvStartWriteSeq( 0, sizeof( CvSeq ), sizeof( CvDI ), storage, &writer ));
for( i = 0; i < (*samples)->rows; ++i )
{
CV_CALL( cvGetRow( *samples, &sample, i ));
elem.i = i;
CV_CALL( elem.d = cvNorm( &sample, NULL, CV_L2 ));
CV_WRITE_SEQ_ELEM( elem, writer );
}
CV_CALL( seq = cvEndWriteSeq( &writer ) );
/* sort the sequence in a distance ascending order */
CV_CALL( cvSeqSort( seq, icvCmpDI, NULL ) );
/* assign class labels */
num_classes = MIN( num_samples, num_classes );
CV_CALL( cvStartReadSeq( seq, &reader ) );
CV_READ_SEQ_ELEM( elem, reader );
for( i = 0, cur_class = 0; i < num_samples; ++cur_class )
{
int last_idx;
double max_dst;
last_idx = num_samples * (cur_class + 1) / num_classes - 1;
CV_CALL( max_dst = (*((CvDI*) cvGetSeqElem( seq, last_idx ))).d );
max_dst = MAX( max_dst, elem.d );
for( ; elem.d <= max_dst && i < num_samples; ++i )
{
CV_MAT_ELEM( **responses, int, 0, elem.i ) = cur_class;
if( i < num_samples - 1 )
{
CV_READ_SEQ_ELEM( elem, reader );
}
}
}
}
__END__;
if( cvGetErrStatus() < 0 )
{
if( samples )
cvReleaseMat( samples );
if( responses )
cvReleaseMat( responses );
}
cvReleaseMat( &mean );
cvReleaseMat( &cov );
cvReleaseMemStorage( &storage );
}
/* End of file. */