Home | History | Annotate | Download | only in src
      1 /*M///////////////////////////////////////////////////////////////////////////////////////
      2 //
      3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
      4 //
      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.
      8 //
      9 //
     10 //                        Intel License Agreement
     11 //
     12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
     13 // Third party copyrights are property of their respective owners.
     14 //
     15 // Redistribution and use in source and binary forms, with or without modification,
     16 // are permitted provided that the following conditions are met:
     17 //
     18 //   * Redistribution's of source code must retain the above copyright notice,
     19 //     this list of conditions and the following disclaimer.
     20 //
     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.
     24 //
     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.
     27 //
     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.
     38 //
     39 //M*/
     40 
     41 #include "_ml.h"
     42 
     43 typedef struct CvDI
     44 {
     45     double d;
     46     int    i;
     47 } CvDI;
     48 
     49 int CV_CDECL
     50 icvCmpDI( const void* a, const void* b, void* )
     51 {
     52     const CvDI* e1 = (const CvDI*) a;
     53     const CvDI* e2 = (const CvDI*) b;
     54 
     55     return (e1->d < e2->d) ? -1 : (e1->d > e2->d);
     56 }
     57 
     58 CV_IMPL void
     59 cvCreateTestSet( int type, CvMat** samples,
     60                  int num_samples,
     61                  int num_features,
     62                  CvMat** responses,
     63                  int num_classes, ... )
     64 {
     65     CvMat* mean = NULL;
     66     CvMat* cov = NULL;
     67     CvMemStorage* storage = NULL;
     68 
     69     CV_FUNCNAME( "cvCreateTestSet" );
     70 
     71     __BEGIN__;
     72 
     73     if( samples )
     74         *samples = NULL;
     75     if( responses )
     76         *responses = NULL;
     77 
     78     if( type != CV_TS_CONCENTRIC_SPHERES )
     79         CV_ERROR( CV_StsBadArg, "Invalid type parameter" );
     80 
     81     if( !samples )
     82         CV_ERROR( CV_StsNullPtr, "samples parameter must be not NULL" );
     83 
     84     if( !responses )
     85         CV_ERROR( CV_StsNullPtr, "responses parameter must be not NULL" );
     86 
     87     if( num_samples < 1 )
     88         CV_ERROR( CV_StsBadArg, "num_samples parameter must be positive" );
     89 
     90     if( num_features < 1 )
     91         CV_ERROR( CV_StsBadArg, "num_features parameter must be positive" );
     92 
     93     if( num_classes < 1 )
     94         CV_ERROR( CV_StsBadArg, "num_classes parameter must be positive" );
     95 
     96     if( type == CV_TS_CONCENTRIC_SPHERES )
     97     {
     98         CvSeqWriter writer;
     99         CvSeqReader reader;
    100         CvMat sample;
    101         CvDI elem;
    102         CvSeq* seq = NULL;
    103         int i, cur_class;
    104 
    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 ) );
    111 
    112         /* fill the feature values matrix with random numbers drawn from standard
    113            normal distribution */
    114         CV_CALL( cvRandMVNormal( mean, cov, *samples ) );
    115 
    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 )
    121         {
    122             CV_CALL( cvGetRow( *samples, &sample, i ));
    123             elem.i = i;
    124             CV_CALL( elem.d = cvNorm( &sample, NULL, CV_L2 ));
    125             CV_WRITE_SEQ_ELEM( elem, writer );
    126         }
    127         CV_CALL( seq = cvEndWriteSeq( &writer ) );
    128 
    129         /* sort the sequence in a distance ascending order */
    130         CV_CALL( cvSeqSort( seq, icvCmpDI, NULL ) );
    131 
    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 )
    137         {
    138             int last_idx;
    139             double max_dst;
    140 
    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 );
    144 
    145             for( ; elem.d <= max_dst && i < num_samples; ++i )
    146             {
    147                 CV_MAT_ELEM( **responses, int, 0, elem.i ) = cur_class;
    148                 if( i < num_samples - 1 )
    149                 {
    150                     CV_READ_SEQ_ELEM( elem, reader );
    151                 }
    152             }
    153         }
    154     }
    155 
    156     __END__;
    157 
    158     if( cvGetErrStatus() < 0 )
    159     {
    160         if( samples )
    161             cvReleaseMat( samples );
    162         if( responses )
    163             cvReleaseMat( responses );
    164     }
    165     cvReleaseMat( &mean );
    166     cvReleaseMat( &cov );
    167     cvReleaseMemStorage( &storage );
    168 }
    169 
    170 /* End of file. */
    171