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 "precomp.hpp" 42 43 namespace cv { namespace ml { 44 45 struct PairDI 46 { 47 double d; 48 int i; 49 }; 50 51 struct CmpPairDI 52 { 53 bool operator ()(const PairDI& e1, const PairDI& e2) const 54 { 55 return (e1.d < e2.d) || (e1.d == e2.d && e1.i < e2.i); 56 } 57 }; 58 59 void createConcentricSpheresTestSet( int num_samples, int num_features, int num_classes, 60 OutputArray _samples, OutputArray _responses) 61 { 62 if( num_samples < 1 ) 63 CV_Error( CV_StsBadArg, "num_samples parameter must be positive" ); 64 65 if( num_features < 1 ) 66 CV_Error( CV_StsBadArg, "num_features parameter must be positive" ); 67 68 if( num_classes < 1 ) 69 CV_Error( CV_StsBadArg, "num_classes parameter must be positive" ); 70 71 int i, cur_class; 72 73 _samples.create( num_samples, num_features, CV_32F ); 74 _responses.create( 1, num_samples, CV_32S ); 75 76 Mat responses = _responses.getMat(); 77 78 Mat mean = Mat::zeros(1, num_features, CV_32F); 79 Mat cov = Mat::eye(num_features, num_features, CV_32F); 80 81 // fill the feature values matrix with random numbers drawn from standard normal distribution 82 randMVNormal( mean, cov, num_samples, _samples ); 83 Mat samples = _samples.getMat(); 84 85 // calculate distances from the origin to the samples and put them 86 // into the sequence along with indices 87 std::vector<PairDI> dis(samples.rows); 88 89 for( i = 0; i < samples.rows; i++ ) 90 { 91 PairDI& elem = dis[i]; 92 elem.i = i; 93 elem.d = norm(samples.row(i), NORM_L2); 94 } 95 96 std::sort(dis.begin(), dis.end(), CmpPairDI()); 97 98 // assign class labels 99 num_classes = std::min( num_samples, num_classes ); 100 for( i = 0, cur_class = 0; i < num_samples; ++cur_class ) 101 { 102 int last_idx = num_samples * (cur_class + 1) / num_classes - 1; 103 double max_dst = dis[last_idx].d; 104 max_dst = std::max( max_dst, dis[i].d ); 105 106 for( ; i < num_samples && dis[i].d <= max_dst; ++i ) 107 responses.at<int>(i) = cur_class; 108 } 109 } 110 111 }} 112 113 /* End of file. */ 114