Path: blob/master/apps/traincascade/old_ml_precomp.hpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10//11// Copyright (C) 2000, Intel Corporation, all rights reserved.12// Third party copyrights are property of their respective owners.13//14// Redistribution and use in source and binary forms, with or without modification,15// are permitted provided that the following conditions are met:16//17// * Redistribution's of source code must retain the above copyright notice,18// this list of conditions and the following disclaimer.19//20// * Redistribution's in binary form must reproduce the above copyright notice,21// this list of conditions and the following disclaimer in the documentation22// and/or other materials provided with the distribution.23//24// * The name of Intel Corporation may not be used to endorse or promote products25// derived from this software without specific prior written permission.26//27// This software is provided by the copyright holders and contributors "as is" and28// any express or implied warranties, including, but not limited to, the implied29// warranties of merchantability and fitness for a particular purpose are disclaimed.30// In no event shall the Intel Corporation or contributors be liable for any direct,31// indirect, incidental, special, exemplary, or consequential damages32// (including, but not limited to, procurement of substitute goods or services;33// loss of use, data, or profits; or business interruption) however caused34// and on any theory of liability, whether in contract, strict liability,35// or tort (including negligence or otherwise) arising in any way out of36// the use of this software, even if advised of the possibility of such damage.37//38//M*/3940#ifndef OPENCV_PRECOMP_H41#define OPENCV_PRECOMP_H4243#include "opencv2/core.hpp"44#include "old_ml.hpp"45#include "opencv2/core/core_c.h"46#include "opencv2/core/utility.hpp"4748#include "opencv2/core/private.hpp"4950#include <assert.h>51#include <float.h>52#include <limits.h>53#include <math.h>54#include <stdlib.h>55#include <stdio.h>56#include <string.h>57#include <time.h>5859#define ML_IMPL CV_IMPL60#define __BEGIN__ __CV_BEGIN__61#define __END__ __CV_END__62#define EXIT __CV_EXIT__6364#define CV_MAT_ELEM_FLAG( mat, type, comp, vect, tflag ) \65(( tflag == CV_ROW_SAMPLE ) \66? (CV_MAT_ELEM( mat, type, comp, vect )) \67: (CV_MAT_ELEM( mat, type, vect, comp )))6869/* Convert matrix to vector */70#define ICV_MAT2VEC( mat, vdata, vstep, num ) \71if( MIN( (mat).rows, (mat).cols ) != 1 ) \72CV_ERROR( CV_StsBadArg, "" ); \73(vdata) = ((mat).data.ptr); \74if( (mat).rows == 1 ) \75{ \76(vstep) = CV_ELEM_SIZE( (mat).type ); \77(num) = (mat).cols; \78} \79else \80{ \81(vstep) = (mat).step; \82(num) = (mat).rows; \83}8485/* get raw data */86#define ICV_RAWDATA( mat, flags, rdata, sstep, cstep, m, n ) \87(rdata) = (mat).data.ptr; \88if( CV_IS_ROW_SAMPLE( flags ) ) \89{ \90(sstep) = (mat).step; \91(cstep) = CV_ELEM_SIZE( (mat).type ); \92(m) = (mat).rows; \93(n) = (mat).cols; \94} \95else \96{ \97(cstep) = (mat).step; \98(sstep) = CV_ELEM_SIZE( (mat).type ); \99(n) = (mat).rows; \100(m) = (mat).cols; \101}102103#define ICV_IS_MAT_OF_TYPE( mat, mat_type) \104(CV_IS_MAT( mat ) && CV_MAT_TYPE( mat->type ) == (mat_type) && \105(mat)->cols > 0 && (mat)->rows > 0)106107/*108uchar* data; int sstep, cstep; - trainData->data109uchar* classes; int clstep; int ncl;- trainClasses110uchar* tmask; int tmstep; int ntm; - typeMask111uchar* missed;int msstep, mcstep; -missedMeasurements...112int mm, mn; == m,n == size,dim113uchar* sidx;int sistep; - sampleIdx114uchar* cidx;int cistep; - compIdx115int k, l; == n,m == dim,size (length of cidx, sidx)116int m, n; == size,dim117*/118#define ICV_DECLARE_TRAIN_ARGS() \119uchar* data; \120int sstep, cstep; \121uchar* classes; \122int clstep; \123int ncl; \124uchar* tmask; \125int tmstep; \126int ntm; \127uchar* missed; \128int msstep, mcstep; \129int mm, mn; \130uchar* sidx; \131int sistep; \132uchar* cidx; \133int cistep; \134int k, l; \135int m, n; \136\137data = classes = tmask = missed = sidx = cidx = NULL; \138sstep = cstep = clstep = ncl = tmstep = ntm = msstep = mcstep = mm = mn = 0; \139sistep = cistep = k = l = m = n = 0;140141#define ICV_TRAIN_DATA_REQUIRED( param, flags ) \142if( !ICV_IS_MAT_OF_TYPE( (param), CV_32FC1 ) ) \143{ \144CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \145} \146else \147{ \148ICV_RAWDATA( *(param), (flags), data, sstep, cstep, m, n ); \149k = n; \150l = m; \151}152153#define ICV_TRAIN_CLASSES_REQUIRED( param ) \154if( !ICV_IS_MAT_OF_TYPE( (param), CV_32FC1 ) ) \155{ \156CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \157} \158else \159{ \160ICV_MAT2VEC( *(param), classes, clstep, ncl ); \161if( m != ncl ) \162{ \163CV_ERROR( CV_StsBadArg, "Unmatched sizes" ); \164} \165}166167#define ICV_ARG_NULL( param ) \168if( (param) != NULL ) \169{ \170CV_ERROR( CV_StsBadArg, #param " parameter must be NULL" ); \171}172173#define ICV_MISSED_MEASUREMENTS_OPTIONAL( param, flags ) \174if( param ) \175{ \176if( !ICV_IS_MAT_OF_TYPE( param, CV_8UC1 ) ) \177{ \178CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \179} \180else \181{ \182ICV_RAWDATA( *(param), (flags), missed, msstep, mcstep, mm, mn ); \183if( mm != m || mn != n ) \184{ \185CV_ERROR( CV_StsBadArg, "Unmatched sizes" ); \186} \187} \188}189190#define ICV_COMP_IDX_OPTIONAL( param ) \191if( param ) \192{ \193if( !ICV_IS_MAT_OF_TYPE( param, CV_32SC1 ) ) \194{ \195CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \196} \197else \198{ \199ICV_MAT2VEC( *(param), cidx, cistep, k ); \200if( k > n ) \201CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \202} \203}204205#define ICV_SAMPLE_IDX_OPTIONAL( param ) \206if( param ) \207{ \208if( !ICV_IS_MAT_OF_TYPE( param, CV_32SC1 ) ) \209{ \210CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \211} \212else \213{ \214ICV_MAT2VEC( *sampleIdx, sidx, sistep, l ); \215if( l > m ) \216CV_ERROR( CV_StsBadArg, "Invalid " #param " parameter" ); \217} \218}219220/****************************************************************************************/221#define ICV_CONVERT_FLOAT_ARRAY_TO_MATRICE( array, matrice ) \222{ \223CvMat a, b; \224int dims = (matrice)->cols; \225int nsamples = (matrice)->rows; \226int type = CV_MAT_TYPE((matrice)->type); \227int i, offset = dims; \228\229CV_ASSERT( type == CV_32FC1 || type == CV_64FC1 ); \230offset *= ((type == CV_32FC1) ? sizeof(float) : sizeof(double));\231\232b = cvMat( 1, dims, CV_32FC1 ); \233cvGetRow( matrice, &a, 0 ); \234for( i = 0; i < nsamples; i++, a.data.ptr += offset ) \235{ \236b.data.fl = (float*)array[i]; \237CV_CALL( cvConvert( &b, &a ) ); \238} \239}240241/****************************************************************************************\242* Auxiliary functions declarations *243\****************************************************************************************/244245/* Generates a set of classes centers in quantity <num_of_clusters> that are generated as246uniform random vectors in parallelepiped, where <data> is concentrated. Vectors in247<data> should have horizontal orientation. If <centers> != NULL, the function doesn't248allocate any memory and stores generated centers in <centers>, returns <centers>.249If <centers> == NULL, the function allocates memory and creates the matrice. Centers250are supposed to be oriented horizontally. */251CvMat* icvGenerateRandomClusterCenters( int seed,252const CvMat* data,253int num_of_clusters,254CvMat* centers CV_DEFAULT(0));255256/* Fills the <labels> using <probs> by choosing the maximal probability. Outliers are257fixed by <oulier_tresh> and have cluster label (-1). Function also controls that there258weren't "empty" clusters by filling empty clusters with the maximal probability vector.259If probs_sums != NULL, fills it with the sums of probabilities for each sample (it is260useful for normalizing probabilities' matrice of FCM) */261void icvFindClusterLabels( const CvMat* probs, float outlier_thresh, float r,262const CvMat* labels );263264typedef struct CvSparseVecElem32f265{266int idx;267float val;268}269CvSparseVecElem32f;270271/* Prepare training data and related parameters */272#define CV_TRAIN_STATMODEL_DEFRAGMENT_TRAIN_DATA 1273#define CV_TRAIN_STATMODEL_SAMPLES_AS_ROWS 2274#define CV_TRAIN_STATMODEL_SAMPLES_AS_COLUMNS 4275#define CV_TRAIN_STATMODEL_CATEGORICAL_RESPONSE 8276#define CV_TRAIN_STATMODEL_ORDERED_RESPONSE 16277#define CV_TRAIN_STATMODEL_RESPONSES_ON_OUTPUT 32278#define CV_TRAIN_STATMODEL_ALWAYS_COPY_TRAIN_DATA 64279#define CV_TRAIN_STATMODEL_SPARSE_AS_SPARSE 128280281int282cvPrepareTrainData( const char* /*funcname*/,283const CvMat* train_data, int tflag,284const CvMat* responses, int response_type,285const CvMat* var_idx,286const CvMat* sample_idx,287bool always_copy_data,288const float*** out_train_samples,289int* _sample_count,290int* _var_count,291int* _var_all,292CvMat** out_responses,293CvMat** out_response_map,294CvMat** out_var_idx,295CvMat** out_sample_idx=0 );296297void298cvSortSamplesByClasses( const float** samples, const CvMat* classes,299int* class_ranges, const uchar** mask CV_DEFAULT(0) );300301void302cvCombineResponseMaps (CvMat* _responses,303const CvMat* old_response_map,304CvMat* new_response_map,305CvMat** out_response_map);306307void308cvPreparePredictData( const CvArr* sample, int dims_all, const CvMat* comp_idx,309int class_count, const CvMat* prob, float** row_sample,310int as_sparse CV_DEFAULT(0) );311312/* copies clustering [or batch "predict"] results313(labels and/or centers and/or probs) back to the output arrays */314void315cvWritebackLabels( const CvMat* labels, CvMat* dst_labels,316const CvMat* centers, CvMat* dst_centers,317const CvMat* probs, CvMat* dst_probs,318const CvMat* sample_idx, int samples_all,319const CvMat* comp_idx, int dims_all );320#define cvWritebackResponses cvWritebackLabels321322#define XML_FIELD_NAME "_name"323CvFileNode* icvFileNodeGetChild(CvFileNode* father, const char* name);324CvFileNode* icvFileNodeGetChildArrayElem(CvFileNode* father, const char* name,int index);325CvFileNode* icvFileNodeGetNext(CvFileNode* n, const char* name);326327328void cvCheckTrainData( const CvMat* train_data, int tflag,329const CvMat* missing_mask,330int* var_all, int* sample_all );331332CvMat* cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, bool check_for_duplicates=false );333334CvMat* cvPreprocessVarType( const CvMat* type_mask, const CvMat* var_idx,335int var_all, int* response_type );336337CvMat* cvPreprocessOrderedResponses( const CvMat* responses,338const CvMat* sample_idx, int sample_all );339340CvMat* cvPreprocessCategoricalResponses( const CvMat* responses,341const CvMat* sample_idx, int sample_all,342CvMat** out_response_map, CvMat** class_counts=0 );343344const float** cvGetTrainSamples( const CvMat* train_data, int tflag,345const CvMat* var_idx, const CvMat* sample_idx,346int* _var_count, int* _sample_count,347bool always_copy_data=false );348349namespace cv350{351struct DTreeBestSplitFinder352{353DTreeBestSplitFinder(){ splitSize = 0, tree = 0; node = 0; }354DTreeBestSplitFinder( CvDTree* _tree, CvDTreeNode* _node);355DTreeBestSplitFinder( const DTreeBestSplitFinder& finder, Split );356virtual ~DTreeBestSplitFinder() {}357virtual void operator()(const BlockedRange& range);358void join( DTreeBestSplitFinder& rhs );359Ptr<CvDTreeSplit> bestSplit;360Ptr<CvDTreeSplit> split;361int splitSize;362CvDTree* tree;363CvDTreeNode* node;364};365366struct ForestTreeBestSplitFinder : DTreeBestSplitFinder367{368ForestTreeBestSplitFinder() : DTreeBestSplitFinder() {}369ForestTreeBestSplitFinder( CvForestTree* _tree, CvDTreeNode* _node );370ForestTreeBestSplitFinder( const ForestTreeBestSplitFinder& finder, Split );371virtual void operator()(const BlockedRange& range);372};373}374375#endif /* __ML_H__ */376377378