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 #ifndef __OPENCV_PRECOMP_H__ 42 #define __OPENCV_PRECOMP_H__ 43 44 #include "opencv2/core.hpp" 45 #include "old_ml.hpp" 46 #include "opencv2/core/core_c.h" 47 #include "opencv2/core/utility.hpp" 48 49 #include "opencv2/core/private.hpp" 50 51 #include <assert.h> 52 #include <float.h> 53 #include <limits.h> 54 #include <math.h> 55 #include <stdlib.h> 56 #include <stdio.h> 57 #include <string.h> 58 #include <time.h> 59 60 #define ML_IMPL CV_IMPL 61 #define __BEGIN__ __CV_BEGIN__ 62 #define __END__ __CV_END__ 63 #define EXIT __CV_EXIT__ 64 65 #define CV_MAT_ELEM_FLAG( mat, type, comp, vect, tflag ) \ 66 (( tflag == CV_ROW_SAMPLE ) \ 67 ? (CV_MAT_ELEM( mat, type, comp, vect )) \ 68 : (CV_MAT_ELEM( mat, type, vect, comp ))) 69 70 /* Convert matrix to vector */ 71 #define ICV_MAT2VEC( mat, vdata, vstep, num ) \ 72 if( MIN( (mat).rows, (mat).cols ) != 1 ) \ 73 CV_ERROR( CV_StsBadArg, "" ); \ 74 (vdata) = ((mat).data.ptr); \ 75 if( (mat).rows == 1 ) \ 76 { \ 77 (vstep) = CV_ELEM_SIZE( (mat).type ); \ 78 (num) = (mat).cols; \ 79 } \ 80 else \ 81 { \ 82 (vstep) = (mat).step; \ 83 (num) = (mat).rows; \ 84 } 85 86 /* get raw data */ 87 #define ICV_RAWDATA( mat, flags, rdata, sstep, cstep, m, n ) \ 88 (rdata) = (mat).data.ptr; \ 89 if( CV_IS_ROW_SAMPLE( flags ) ) \ 90 { \ 91 (sstep) = (mat).step; \ 92 (cstep) = CV_ELEM_SIZE( (mat).type ); \ 93 (m) = (mat).rows; \ 94 (n) = (mat).cols; \ 95 } \ 96 else \ 97 { \ 98 (cstep) = (mat).step; \ 99 (sstep) = CV_ELEM_SIZE( (mat).type ); \ 100 (n) = (mat).rows; \ 101 (m) = (mat).cols; \ 102 } 103 104 #define ICV_IS_MAT_OF_TYPE( mat, mat_type) \ 105 (CV_IS_MAT( mat ) && CV_MAT_TYPE( mat->type ) == (mat_type) && \ 106 (mat)->cols > 0 && (mat)->rows > 0) 107 108 /* 109 uchar* data; int sstep, cstep; - trainData->data 110 uchar* classes; int clstep; int ncl;- trainClasses 111 uchar* tmask; int tmstep; int ntm; - typeMask 112 uchar* missed;int msstep, mcstep; -missedMeasurements... 113 int mm, mn; == m,n == size,dim 114 uchar* sidx;int sistep; - sampleIdx 115 uchar* cidx;int cistep; - compIdx 116 int k, l; == n,m == dim,size (length of cidx, sidx) 117 int m, n; == size,dim 118 */ 119 #define ICV_DECLARE_TRAIN_ARGS() \ 120 uchar* data; \ 121 int sstep, cstep; \ 122 uchar* classes; \ 123 int clstep; \ 124 int ncl; \ 125 uchar* tmask; \ 126 int tmstep; \ 127 int ntm; \ 128 uchar* missed; \ 129 int msstep, mcstep; \ 130 int mm, mn; \ 131 uchar* sidx; \ 132 int sistep; \ 133 uchar* cidx; \ 134 int cistep; \ 135 int k, l; \ 136 int m, n; \ 137 \ 138 data = classes = tmask = missed = sidx = cidx = NULL; \ 139 sstep = cstep = clstep = ncl = tmstep = ntm = msstep = mcstep = mm = mn = 0; \ 140 sistep = cistep = k = l = m = n = 0; 141 142 #define ICV_TRAIN_DATA_REQUIRED( param, flags ) \ 143 if( !ICV_IS_MAT_OF_TYPE( (param), CV_32FC1 ) ) \ 144 { \ 145 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \ 146 } \ 147 else \ 148 { \ 149 ICV_RAWDATA( *(param), (flags), data, sstep, cstep, m, n ); \ 150 k = n; \ 151 l = m; \ 152 } 153 154 #define ICV_TRAIN_CLASSES_REQUIRED( param ) \ 155 if( !ICV_IS_MAT_OF_TYPE( (param), CV_32FC1 ) ) \ 156 { \ 157 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \ 158 } \ 159 else \ 160 { \ 161 ICV_MAT2VEC( *(param), classes, clstep, ncl ); \ 162 if( m != ncl ) \ 163 { \ 164 CV_ERROR( CV_StsBadArg, "Unmatched sizes" ); \ 165 } \ 166 } 167 168 #define ICV_ARG_NULL( param ) \ 169 if( (param) != NULL ) \ 170 { \ 171 CV_ERROR( CV_StsBadArg, #param " parameter must be NULL" ); \ 172 } 173 174 #define ICV_MISSED_MEASUREMENTS_OPTIONAL( param, flags ) \ 175 if( param ) \ 176 { \ 177 if( !ICV_IS_MAT_OF_TYPE( param, CV_8UC1 ) ) \ 178 { \ 179 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \ 180 } \ 181 else \ 182 { \ 183 ICV_RAWDATA( *(param), (flags), missed, msstep, mcstep, mm, mn ); \ 184 if( mm != m || mn != n ) \ 185 { \ 186 CV_ERROR( CV_StsBadArg, "Unmatched sizes" ); \ 187 } \ 188 } \ 189 } 190 191 #define ICV_COMP_IDX_OPTIONAL( param ) \ 192 if( param ) \ 193 { \ 194 if( !ICV_IS_MAT_OF_TYPE( param, CV_32SC1 ) ) \ 195 { \ 196 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \ 197 } \ 198 else \ 199 { \ 200 ICV_MAT2VEC( *(param), cidx, cistep, k ); \ 201 if( k > n ) \ 202 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \ 203 } \ 204 } 205 206 #define ICV_SAMPLE_IDX_OPTIONAL( param ) \ 207 if( param ) \ 208 { \ 209 if( !ICV_IS_MAT_OF_TYPE( param, CV_32SC1 ) ) \ 210 { \ 211 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \ 212 } \ 213 else \ 214 { \ 215 ICV_MAT2VEC( *sampleIdx, sidx, sistep, l ); \ 216 if( l > m ) \ 217 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \ 218 } \ 219 } 220 221 /****************************************************************************************/ 222 #define ICV_CONVERT_FLOAT_ARRAY_TO_MATRICE( array, matrice ) \ 223 { \ 224 CvMat a, b; \ 225 int dims = (matrice)->cols; \ 226 int nsamples = (matrice)->rows; \ 227 int type = CV_MAT_TYPE((matrice)->type); \ 228 int i, offset = dims; \ 229 \ 230 CV_ASSERT( type == CV_32FC1 || type == CV_64FC1 ); \ 231 offset *= ((type == CV_32FC1) ? sizeof(float) : sizeof(double));\ 232 \ 233 b = cvMat( 1, dims, CV_32FC1 ); \ 234 cvGetRow( matrice, &a, 0 ); \ 235 for( i = 0; i < nsamples; i++, a.data.ptr += offset ) \ 236 { \ 237 b.data.fl = (float*)array[i]; \ 238 CV_CALL( cvConvert( &b, &a ) ); \ 239 } \ 240 } 241 242 /****************************************************************************************\ 243 * Auxiliary functions declarations * 244 \****************************************************************************************/ 245 246 /* Generates a set of classes centers in quantity <num_of_clusters> that are generated as 247 uniform random vectors in parallelepiped, where <data> is concentrated. Vectors in 248 <data> should have horizontal orientation. If <centers> != NULL, the function doesn't 249 allocate any memory and stores generated centers in <centers>, returns <centers>. 250 If <centers> == NULL, the function allocates memory and creates the matrice. Centers 251 are supposed to be oriented horizontally. */ 252 CvMat* icvGenerateRandomClusterCenters( int seed, 253 const CvMat* data, 254 int num_of_clusters, 255 CvMat* centers CV_DEFAULT(0)); 256 257 /* Fills the <labels> using <probs> by choosing the maximal probability. Outliers are 258 fixed by <oulier_tresh> and have cluster label (-1). Function also controls that there 259 weren't "empty" clusters by filling empty clusters with the maximal probability vector. 260 If probs_sums != NULL, filles it with the sums of probabilities for each sample (it is 261 useful for normalizing probabilities' matrice of FCM) */ 262 void icvFindClusterLabels( const CvMat* probs, float outlier_thresh, float r, 263 const CvMat* labels ); 264 265 typedef struct CvSparseVecElem32f 266 { 267 int idx; 268 float val; 269 } 270 CvSparseVecElem32f; 271 272 /* Prepare training data and related parameters */ 273 #define CV_TRAIN_STATMODEL_DEFRAGMENT_TRAIN_DATA 1 274 #define CV_TRAIN_STATMODEL_SAMPLES_AS_ROWS 2 275 #define CV_TRAIN_STATMODEL_SAMPLES_AS_COLUMNS 4 276 #define CV_TRAIN_STATMODEL_CATEGORICAL_RESPONSE 8 277 #define CV_TRAIN_STATMODEL_ORDERED_RESPONSE 16 278 #define CV_TRAIN_STATMODEL_RESPONSES_ON_OUTPUT 32 279 #define CV_TRAIN_STATMODEL_ALWAYS_COPY_TRAIN_DATA 64 280 #define CV_TRAIN_STATMODEL_SPARSE_AS_SPARSE 128 281 282 int 283 cvPrepareTrainData( const char* /*funcname*/, 284 const CvMat* train_data, int tflag, 285 const CvMat* responses, int response_type, 286 const CvMat* var_idx, 287 const CvMat* sample_idx, 288 bool always_copy_data, 289 const float*** out_train_samples, 290 int* _sample_count, 291 int* _var_count, 292 int* _var_all, 293 CvMat** out_responses, 294 CvMat** out_response_map, 295 CvMat** out_var_idx, 296 CvMat** out_sample_idx=0 ); 297 298 void 299 cvSortSamplesByClasses( const float** samples, const CvMat* classes, 300 int* class_ranges, const uchar** mask CV_DEFAULT(0) ); 301 302 void 303 cvCombineResponseMaps (CvMat* _responses, 304 const CvMat* old_response_map, 305 CvMat* new_response_map, 306 CvMat** out_response_map); 307 308 void 309 cvPreparePredictData( const CvArr* sample, int dims_all, const CvMat* comp_idx, 310 int class_count, const CvMat* prob, float** row_sample, 311 int as_sparse CV_DEFAULT(0) ); 312 313 /* copies clustering [or batch "predict"] results 314 (labels and/or centers and/or probs) back to the output arrays */ 315 void 316 cvWritebackLabels( const CvMat* labels, CvMat* dst_labels, 317 const CvMat* centers, CvMat* dst_centers, 318 const CvMat* probs, CvMat* dst_probs, 319 const CvMat* sample_idx, int samples_all, 320 const CvMat* comp_idx, int dims_all ); 321 #define cvWritebackResponses cvWritebackLabels 322 323 #define XML_FIELD_NAME "_name" 324 CvFileNode* icvFileNodeGetChild(CvFileNode* father, const char* name); 325 CvFileNode* icvFileNodeGetChildArrayElem(CvFileNode* father, const char* name,int index); 326 CvFileNode* icvFileNodeGetNext(CvFileNode* n, const char* name); 327 328 329 void cvCheckTrainData( const CvMat* train_data, int tflag, 330 const CvMat* missing_mask, 331 int* var_all, int* sample_all ); 332 333 CvMat* cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, bool check_for_duplicates=false ); 334 335 CvMat* cvPreprocessVarType( const CvMat* type_mask, const CvMat* var_idx, 336 int var_all, int* response_type ); 337 338 CvMat* cvPreprocessOrderedResponses( const CvMat* responses, 339 const CvMat* sample_idx, int sample_all ); 340 341 CvMat* cvPreprocessCategoricalResponses( const CvMat* responses, 342 const CvMat* sample_idx, int sample_all, 343 CvMat** out_response_map, CvMat** class_counts=0 ); 344 345 const float** cvGetTrainSamples( const CvMat* train_data, int tflag, 346 const CvMat* var_idx, const CvMat* sample_idx, 347 int* _var_count, int* _sample_count, 348 bool always_copy_data=false ); 349 350 namespace cv 351 { 352 struct DTreeBestSplitFinder 353 { DTreeBestSplitFindercv::DTreeBestSplitFinder354 DTreeBestSplitFinder(){ splitSize = 0, tree = 0; node = 0; } 355 DTreeBestSplitFinder( CvDTree* _tree, CvDTreeNode* _node); 356 DTreeBestSplitFinder( const DTreeBestSplitFinder& finder, Split ); ~DTreeBestSplitFindercv::DTreeBestSplitFinder357 virtual ~DTreeBestSplitFinder() {} 358 virtual void operator()(const BlockedRange& range); 359 void join( DTreeBestSplitFinder& rhs ); 360 Ptr<CvDTreeSplit> bestSplit; 361 Ptr<CvDTreeSplit> split; 362 int splitSize; 363 CvDTree* tree; 364 CvDTreeNode* node; 365 }; 366 367 struct ForestTreeBestSplitFinder : DTreeBestSplitFinder 368 { ForestTreeBestSplitFindercv::ForestTreeBestSplitFinder369 ForestTreeBestSplitFinder() : DTreeBestSplitFinder() {} 370 ForestTreeBestSplitFinder( CvForestTree* _tree, CvDTreeNode* _node ); 371 ForestTreeBestSplitFinder( const ForestTreeBestSplitFinder& finder, Split ); 372 virtual void operator()(const BlockedRange& range); 373 }; 374 } 375 376 #endif /* __ML_H__ */ 377