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.
41 #ifndef __OPENCV_PRECOMP_H__
42 #define __OPENCV_PRECOMP_H__
44 #ifdef HAVE_CVCONFIG_H
48 #include "opencv2/ml/ml.hpp"
49 #include "opencv2/core/core_c.h"
50 #include "opencv2/core/internal.hpp"
61 #define ML_IMPL CV_IMPL
63 #define CV_MAT_ELEM_FLAG( mat, type, comp, vect, tflag ) \
64 (( tflag == CV_ROW_SAMPLE ) \
65 ? (CV_MAT_ELEM( mat, type, comp, vect )) \
66 : (CV_MAT_ELEM( mat, type, vect, comp )))
68 /* Convert matrix to vector */
69 #define ICV_MAT2VEC( mat, vdata, vstep, num ) \
70 if( MIN( (mat).rows, (mat).cols ) != 1 ) \
71 CV_ERROR( CV_StsBadArg, "" ); \
72 (vdata) = ((mat).data.ptr); \
73 if( (mat).rows == 1 ) \
75 (vstep) = CV_ELEM_SIZE( (mat).type ); \
80 (vstep) = (mat).step; \
85 #define ICV_RAWDATA( mat, flags, rdata, sstep, cstep, m, n ) \
86 (rdata) = (mat).data.ptr; \
87 if( CV_IS_ROW_SAMPLE( flags ) ) \
89 (sstep) = (mat).step; \
90 (cstep) = CV_ELEM_SIZE( (mat).type ); \
96 (cstep) = (mat).step; \
97 (sstep) = CV_ELEM_SIZE( (mat).type ); \
102 #define ICV_IS_MAT_OF_TYPE( mat, mat_type) \
103 (CV_IS_MAT( mat ) && CV_MAT_TYPE( mat->type ) == (mat_type) && \
104 (mat)->cols > 0 && (mat)->rows > 0)
107 uchar* data; int sstep, cstep; - trainData->data
108 uchar* classes; int clstep; int ncl;- trainClasses
109 uchar* tmask; int tmstep; int ntm; - typeMask
110 uchar* missed;int msstep, mcstep; -missedMeasurements...
111 int mm, mn; == m,n == size,dim
112 uchar* sidx;int sistep; - sampleIdx
113 uchar* cidx;int cistep; - compIdx
114 int k, l; == n,m == dim,size (length of cidx, sidx)
115 int m, n; == size,dim
117 #define ICV_DECLARE_TRAIN_ARGS() \
127 int msstep, mcstep; \
136 data = classes = tmask = missed = sidx = cidx = NULL; \
137 sstep = cstep = clstep = ncl = tmstep = ntm = msstep = mcstep = mm = mn = 0; \
138 sistep = cistep = k = l = m = n = 0;
140 #define ICV_TRAIN_DATA_REQUIRED( param, flags ) \
141 if( !ICV_IS_MAT_OF_TYPE( (param), CV_32FC1 ) ) \
143 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
147 ICV_RAWDATA( *(param), (flags), data, sstep, cstep, m, n ); \
152 #define ICV_TRAIN_CLASSES_REQUIRED( param ) \
153 if( !ICV_IS_MAT_OF_TYPE( (param), CV_32FC1 ) ) \
155 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
159 ICV_MAT2VEC( *(param), classes, clstep, ncl ); \
162 CV_ERROR( CV_StsBadArg, "Unmatched sizes" ); \
166 #define ICV_ARG_NULL( param ) \
167 if( (param) != NULL ) \
169 CV_ERROR( CV_StsBadArg, #param " parameter must be NULL" ); \
172 #define ICV_MISSED_MEASUREMENTS_OPTIONAL( param, flags ) \
175 if( !ICV_IS_MAT_OF_TYPE( param, CV_8UC1 ) ) \
177 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
181 ICV_RAWDATA( *(param), (flags), missed, msstep, mcstep, mm, mn ); \
182 if( mm != m || mn != n ) \
184 CV_ERROR( CV_StsBadArg, "Unmatched sizes" ); \
189 #define ICV_COMP_IDX_OPTIONAL( param ) \
192 if( !ICV_IS_MAT_OF_TYPE( param, CV_32SC1 ) ) \
194 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
198 ICV_MAT2VEC( *(param), cidx, cistep, k ); \
200 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
204 #define ICV_SAMPLE_IDX_OPTIONAL( param ) \
207 if( !ICV_IS_MAT_OF_TYPE( param, CV_32SC1 ) ) \
209 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
213 ICV_MAT2VEC( *sampleIdx, sidx, sistep, l ); \
215 CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \
219 /****************************************************************************************/
220 #define ICV_CONVERT_FLOAT_ARRAY_TO_MATRICE( array, matrice ) \
223 int dims = (matrice)->cols; \
224 int nsamples = (matrice)->rows; \
225 int type = CV_MAT_TYPE((matrice)->type); \
226 int i, offset = dims; \
228 CV_ASSERT( type == CV_32FC1 || type == CV_64FC1 ); \
229 offset *= ((type == CV_32FC1) ? sizeof(float) : sizeof(double));\
231 b = cvMat( 1, dims, CV_32FC1 ); \
232 cvGetRow( matrice, &a, 0 ); \
233 for( i = 0; i < nsamples; i++, a.data.ptr += offset ) \
235 b.data.fl = (float*)array[i]; \
236 CV_CALL( cvConvert( &b, &a ) ); \
240 /****************************************************************************************\
241 * Auxiliary functions declarations *
242 \****************************************************************************************/
244 /* Generates a set of classes centers in quantity <num_of_clusters> that are generated as
245 uniform random vectors in parallelepiped, where <data> is concentrated. Vectors in
246 <data> should have horizontal orientation. If <centers> != NULL, the function doesn't
247 allocate any memory and stores generated centers in <centers>, returns <centers>.
248 If <centers> == NULL, the function allocates memory and creates the matrice. Centers
249 are supposed to be oriented horizontally. */
250 CvMat* icvGenerateRandomClusterCenters( int seed,
253 CvMat* centers CV_DEFAULT(0));
255 /* Fills the <labels> using <probs> by choosing the maximal probability. Outliers are
256 fixed by <oulier_tresh> and have cluster label (-1). Function also controls that there
257 weren't "empty" clusters by filling empty clusters with the maximal probability vector.
258 If probs_sums != NULL, filles it with the sums of probabilities for each sample (it is
259 useful for normalizing probabilities' matrice of FCM) */
260 void icvFindClusterLabels( const CvMat* probs, float outlier_thresh, float r,
261 const CvMat* labels );
263 typedef struct CvSparseVecElem32f
270 /* Prepare training data and related parameters */
271 #define CV_TRAIN_STATMODEL_DEFRAGMENT_TRAIN_DATA 1
272 #define CV_TRAIN_STATMODEL_SAMPLES_AS_ROWS 2
273 #define CV_TRAIN_STATMODEL_SAMPLES_AS_COLUMNS 4
274 #define CV_TRAIN_STATMODEL_CATEGORICAL_RESPONSE 8
275 #define CV_TRAIN_STATMODEL_ORDERED_RESPONSE 16
276 #define CV_TRAIN_STATMODEL_RESPONSES_ON_OUTPUT 32
277 #define CV_TRAIN_STATMODEL_ALWAYS_COPY_TRAIN_DATA 64
278 #define CV_TRAIN_STATMODEL_SPARSE_AS_SPARSE 128
281 cvPrepareTrainData( const char* /*funcname*/,
282 const CvMat* train_data, int tflag,
283 const CvMat* responses, int response_type,
284 const CvMat* var_idx,
285 const CvMat* sample_idx,
286 bool always_copy_data,
287 const float*** out_train_samples,
291 CvMat** out_responses,
292 CvMat** out_response_map,
294 CvMat** out_sample_idx=0 );
297 cvSortSamplesByClasses( const float** samples, const CvMat* classes,
298 int* class_ranges, const uchar** mask CV_DEFAULT(0) );
301 cvCombineResponseMaps (CvMat* _responses,
302 const CvMat* old_response_map,
303 CvMat* new_response_map,
304 CvMat** out_response_map);
307 cvPreparePredictData( const CvArr* sample, int dims_all, const CvMat* comp_idx,
308 int class_count, const CvMat* prob, float** row_sample,
309 int as_sparse CV_DEFAULT(0) );
311 /* copies clustering [or batch "predict"] results
312 (labels and/or centers and/or probs) back to the output arrays */
314 cvWritebackLabels( const CvMat* labels, CvMat* dst_labels,
315 const CvMat* centers, CvMat* dst_centers,
316 const CvMat* probs, CvMat* dst_probs,
317 const CvMat* sample_idx, int samples_all,
318 const CvMat* comp_idx, int dims_all );
319 #define cvWritebackResponses cvWritebackLabels
321 #define XML_FIELD_NAME "_name"
322 CvFileNode* icvFileNodeGetChild(CvFileNode* father, const char* name);
323 CvFileNode* icvFileNodeGetChildArrayElem(CvFileNode* father, const char* name,int index);
324 CvFileNode* icvFileNodeGetNext(CvFileNode* n, const char* name);
327 void cvCheckTrainData( const CvMat* train_data, int tflag,
328 const CvMat* missing_mask,
329 int* var_all, int* sample_all );
331 CvMat* cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, bool check_for_duplicates=false );
333 CvMat* cvPreprocessVarType( const CvMat* type_mask, const CvMat* var_idx,
334 int var_all, int* response_type );
336 CvMat* cvPreprocessOrderedResponses( const CvMat* responses,
337 const CvMat* sample_idx, int sample_all );
339 CvMat* cvPreprocessCategoricalResponses( const CvMat* responses,
340 const CvMat* sample_idx, int sample_all,
341 CvMat** out_response_map, CvMat** class_counts=0 );
343 const float** cvGetTrainSamples( const CvMat* train_data, int tflag,
344 const CvMat* var_idx, const CvMat* sample_idx,
345 int* _var_count, int* _sample_count,
346 bool always_copy_data=false );
350 struct DTreeBestSplitFinder
352 DTreeBestSplitFinder(){ tree = 0; node = 0; }
353 DTreeBestSplitFinder( CvDTree* _tree, CvDTreeNode* _node);
354 DTreeBestSplitFinder( const DTreeBestSplitFinder& finder, Split );
355 virtual ~DTreeBestSplitFinder() {}
356 virtual void operator()(const BlockedRange& range);
357 void join( DTreeBestSplitFinder& rhs );
358 Ptr<CvDTreeSplit> bestSplit;
359 Ptr<CvDTreeSplit> split;
365 struct ForestTreeBestSplitFinder : DTreeBestSplitFinder
367 ForestTreeBestSplitFinder() : DTreeBestSplitFinder() {}
368 ForestTreeBestSplitFinder( CvForestTree* _tree, CvDTreeNode* _node );
369 ForestTreeBestSplitFinder( const ForestTreeBestSplitFinder& finder, Split );
370 virtual void operator()(const BlockedRange& range);
374 #endif /* __ML_H__ */