1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
18 // * Redistribution's of source code must retain the above copyright notice,
19 // this list of conditions and the following disclaimer.
21 // * Redistribution's in binary form must reproduce the above copyright notice,
22 // this list of conditions and the following disclaimer in the documentation
23 // and/or other materials provided with the distribution.
25 // * The name of Intel Corporation may not be used to endorse or promote products
26 // derived from this software without specific prior written permission.
28 // This software is provided by the copyright holders and contributors "as is" and
29 // any express or implied warranties, including, but not limited to, the implied
30 // warranties of merchantability and fitness for a particular purpose are disclaimed.
31 // In no event shall the Intel Corporation or contributors be liable for any direct,
32 // indirect, incidental, special, exemplary, or consequential damages
33 // (including, but not limited to, procurement of substitute goods or services;
34 // loss of use, data, or profits; or business interruption) however caused
35 // and on any theory of liability, whether in contract, strict liability,
36 // or tort (including negligence or otherwise) arising in any way out of
37 // the use of this software, even if advised of the possibility of such damage.
50 icvCmpDI( const void* a, const void* b, void* )
52 const CvDI* e1 = (const CvDI*) a;
53 const CvDI* e2 = (const CvDI*) b;
55 return (e1->d < e2->d) ? -1 : (e1->d > e2->d);
59 cvCreateTestSet( int type, CvMat** samples,
63 int num_classes, ... )
67 CvMemStorage* storage = NULL;
69 CV_FUNCNAME( "cvCreateTestSet" );
78 if( type != CV_TS_CONCENTRIC_SPHERES )
79 CV_ERROR( CV_StsBadArg, "Invalid type parameter" );
82 CV_ERROR( CV_StsNullPtr, "samples parameter must be not NULL" );
85 CV_ERROR( CV_StsNullPtr, "responses parameter must be not NULL" );
88 CV_ERROR( CV_StsBadArg, "num_samples parameter must be positive" );
90 if( num_features < 1 )
91 CV_ERROR( CV_StsBadArg, "num_features parameter must be positive" );
94 CV_ERROR( CV_StsBadArg, "num_classes parameter must be positive" );
96 if( type == CV_TS_CONCENTRIC_SPHERES )
105 CV_CALL( *samples = cvCreateMat( num_samples, num_features, CV_32FC1 ) );
106 CV_CALL( *responses = cvCreateMat( 1, num_samples, CV_32SC1 ) );
107 CV_CALL( mean = cvCreateMat( 1, num_features, CV_32FC1 ) );
108 CV_CALL( cvSetZero( mean ) );
109 CV_CALL( cov = cvCreateMat( num_features, num_features, CV_32FC1 ) );
110 CV_CALL( cvSetIdentity( cov ) );
112 /* fill the feature values matrix with random numbers drawn from standard
113 normal distribution */
114 CV_CALL( cvRandMVNormal( mean, cov, *samples ) );
116 /* calculate distances from the origin to the samples and put them
117 into the sequence along with indices */
118 CV_CALL( storage = cvCreateMemStorage() );
119 CV_CALL( cvStartWriteSeq( 0, sizeof( CvSeq ), sizeof( CvDI ), storage, &writer ));
120 for( i = 0; i < (*samples)->rows; ++i )
122 CV_CALL( cvGetRow( *samples, &sample, i ));
124 CV_CALL( elem.d = cvNorm( &sample, NULL, CV_L2 ));
125 CV_WRITE_SEQ_ELEM( elem, writer );
127 CV_CALL( seq = cvEndWriteSeq( &writer ) );
129 /* sort the sequence in a distance ascending order */
130 CV_CALL( cvSeqSort( seq, icvCmpDI, NULL ) );
132 /* assign class labels */
133 num_classes = MIN( num_samples, num_classes );
134 CV_CALL( cvStartReadSeq( seq, &reader ) );
135 CV_READ_SEQ_ELEM( elem, reader );
136 for( i = 0, cur_class = 0; i < num_samples; ++cur_class )
141 last_idx = num_samples * (cur_class + 1) / num_classes - 1;
142 CV_CALL( max_dst = (*((CvDI*) cvGetSeqElem( seq, last_idx ))).d );
143 max_dst = MAX( max_dst, elem.d );
145 for( ; elem.d <= max_dst && i < num_samples; ++i )
147 CV_MAT_ELEM( **responses, int, 0, elem.i ) = cur_class;
148 if( i < num_samples - 1 )
150 CV_READ_SEQ_ELEM( elem, reader );
158 if( cvGetErrStatus() < 0 )
161 cvReleaseMat( samples );
163 cvReleaseMat( responses );
165 cvReleaseMat( &mean );
166 cvReleaseMat( &cov );
167 cvReleaseMemStorage( &storage );