diff --git a/openbr/core/boost.cpp b/openbr/core/boost.cpp new file mode 100644 index 0000000..6a75156 --- /dev/null +++ b/openbr/core/boost.cpp @@ -0,0 +1,1396 @@ +#include + +#include "boost.h" +#include "cxmisc.h" + +using namespace std; +using namespace br; +using namespace cv; + +static inline double +logRatio( double val ) +{ + const double eps = 1e-5; + + val = max( val, eps ); + val = min( val, 1. - eps ); + return log( val/(1. - val) ); +} + +#define CV_CMP_NUM_IDX(i,j) (aux[i] < aux[j]) +static CV_IMPLEMENT_QSORT_EX( icvSortIntAux, int, CV_CMP_NUM_IDX, const float* ) +static CV_IMPLEMENT_QSORT_EX( icvSortUShAux, unsigned short, CV_CMP_NUM_IDX, const float* ) + +#define CV_THRESHOLD_EPS (0.00001F) + +static const int MinBlockSize = 1 << 16; +static const int BlockSizeDelta = 1 << 10; + +// TODO remove this code duplication with ml/precomp.hpp + +static int CV_CDECL icvCmpIntegers( const void* a, const void* b ) +{ + return *(const int*)a - *(const int*)b; +} + +static CvMat* cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, bool check_for_duplicates=false, const int channels = 1 ) +{ + CvMat* idx = 0; + + CV_FUNCNAME( "cvPreprocessIndexArray" ); + + __BEGIN__; + + int i, idx_total, idx_selected = 0, step, type, prev = INT_MIN, is_sorted = 1; + uchar* srcb = 0; + int* srci = 0; + int* dsti; + + if( !CV_IS_MAT(idx_arr) ) + CV_ERROR( CV_StsBadArg, "Invalid index array" ); + + if( idx_arr->rows != 1 && idx_arr->cols != 1 ) + CV_ERROR( CV_StsBadSize, "the index array must be 1-dimensional" ); + + idx_total = idx_arr->rows + idx_arr->cols - 1; + srcb = idx_arr->data.ptr; + srci = idx_arr->data.i; + + type = CV_MAT_TYPE(idx_arr->type); + step = CV_IS_MAT_CONT(idx_arr->type) ? 1 : idx_arr->step/CV_ELEM_SIZE(type); + + if (type == CV_8UC(channels) || type == CV_8SC1) { + // idx_arr is array of 1's and 0's - + // i.e. it is a mask of the selected components + if( idx_total != data_arr_size ) + CV_ERROR( CV_StsUnmatchedSizes, + "Component mask should contain as many elements as the total number of input variables" ); + + for( i = 0; i < idx_total; i++ ) + idx_selected += srcb[i*step] != 0; + + if( idx_selected == 0 ) + CV_ERROR( CV_StsOutOfRange, "No components/input_variables is selected!" ); + } else if (type == CV_32SC(channels)) { + // idx_arr is array of integer indices of selected components + if( idx_total > data_arr_size ) + CV_ERROR( CV_StsOutOfRange, + "index array may not contain more elements than the total number of input variables" ); + idx_selected = idx_total; + // check if sorted already + for( i = 0; i < idx_total; i++ ) + { + int val = srci[i*step]; + if( val >= prev ) + { + is_sorted = 0; + break; + } + prev = val; + } + } else { + CV_ERROR( CV_StsUnsupportedFormat, "Unsupported index array data type " + "(it should be 8uC1, 8sC1 or 32sC1)" ); + } + + CV_CALL( idx = cvCreateMat( 1, idx_selected, CV_32SC(channels) )); + dsti = idx->data.i; + + if( type < CV_32SC(channels) ) + { + for( i = 0; i < idx_total; i++ ) + if( srcb[i*step] ) + *dsti++ = i; + } + else + { + for( i = 0; i < idx_total; i++ ) + dsti[i] = srci[i*step]; + + if( !is_sorted ) + qsort( dsti, idx_total, sizeof(dsti[0]), icvCmpIntegers ); + + if( dsti[0] < 0 || dsti[idx_total-1] >= data_arr_size ) + CV_ERROR( CV_StsOutOfRange, "the index array elements are out of range" ); + + if( check_for_duplicates ) + { + for( i = 1; i < idx_total; i++ ) + if( dsti[i] <= dsti[i-1] ) + CV_ERROR( CV_StsBadArg, "There are duplicated index array elements" ); + } + } + + __END__; + + if( cvGetErrStatus() < 0 ) + cvReleaseMat( &idx ); + + return idx; +} + +//------------------------------------- FeatureEvaluator --------------------------------------- + +void FeatureEvaluator::init(Representation *_representation, int _maxSampleCount, int channels) +{ + representation = _representation; + + int dx, dy; + Size windowSize = representation->windowSize(&dx, &dy); + data.create((int)_maxSampleCount, (windowSize.width + dx) * (windowSize.height + dy), CV_32SC(channels)); + cls.create( (int)_maxSampleCount, 1, CV_32FC1 ); +} + +void FeatureEvaluator::setImage(const Mat &img, uchar clsLabel, int idx) +{ + cls.ptr(idx)[0] = clsLabel; + + Mat pp; + representation->preprocess(img, pp); + pp.reshape(0, 1).copyTo(data.row(idx)); +} + +//----------------------------- CascadeBoostParams ------------------------------------------------- + +CascadeBoostParams::CascadeBoostParams() : minHitRate( 0.995F), maxFalseAlarm( 0.5F ) +{ + boost_type = CvBoost::GENTLE; + use_surrogates = use_1se_rule = truncate_pruned_tree = false; +} + +CascadeBoostParams::CascadeBoostParams(int _boostType, + float _minHitRate, float _maxFalseAlarm, + double _weightTrimRate, int _maxDepth, int _maxWeakCount ) : + CvBoostParams( _boostType, _maxWeakCount, _weightTrimRate, _maxDepth, false, 0 ) +{ + boost_type = _boostType; + minHitRate = _minHitRate; + maxFalseAlarm = _maxFalseAlarm; + use_surrogates = use_1se_rule = truncate_pruned_tree = false; +} + +//---------------------------- CascadeBoostTrainData ----------------------------- + +struct CascadeBoostTrainData : CvDTreeTrainData +{ + CascadeBoostTrainData(const FeatureEvaluator* _featureEvaluator, int _channels, const CvDTreeParams& _params); + CascadeBoostTrainData(const FeatureEvaluator* _featureEvaluator, + int _numSamples, int _precalcValBufSize, int _precalcIdxBufSize, int _channels, + const CvDTreeParams& _params = CvDTreeParams()); + virtual void setData(const FeatureEvaluator* _featureEvaluator, + int _numSamples, int _precalcValBufSize, int _precalcIdxBufSize, + const CvDTreeParams& _params=CvDTreeParams()); + void precalculate(); + + virtual CvDTreeNode* subsample_data(const CvMat* _subsample_idx); + + virtual const int* get_class_labels(CvDTreeNode* n, int* labelsBuf); + virtual const int* get_cv_labels(CvDTreeNode* n, int* labelsBuf); + virtual const int* get_sample_indices(CvDTreeNode* n, int* indicesBuf); + + virtual void get_ord_var_data(CvDTreeNode* n, int vi, float* ordValuesBuf, int* sortedIndicesBuf, + const float** ordValues, const int** sortedIndices, int* sampleIndicesBuf); + virtual const int* get_cat_var_data(CvDTreeNode* n, int vi, int* catValuesBuf); + virtual float getVarValue(int vi, int si); + virtual void free_train_data(); + + const FeatureEvaluator* featureEvaluator; + cv::Mat valCache; // precalculated feature values (CV_32FC1) + CvMat _resp; // for casting + int numPrecalcVal, numPrecalcIdx, channels; +}; + +CvDTreeNode* CascadeBoostTrainData::subsample_data( const CvMat* _subsample_idx ) +{ + CvDTreeNode* root = 0; + CvMat* isubsample_idx = 0; + CvMat* subsample_co = 0; + + bool isMakeRootCopy = true; + + if( !data_root ) + CV_Error( CV_StsError, "No training data has been set" ); + + if( _subsample_idx ) + { + CV_Assert( (isubsample_idx = cvPreprocessIndexArray( _subsample_idx, sample_count, channels )) != 0 ); + + if( isubsample_idx->cols + isubsample_idx->rows - 1 == sample_count ) + { + const int* sidx = isubsample_idx->data.i; + for( int i = 0; i < sample_count; i++ ) + { + if( sidx[i] != i ) + { + isMakeRootCopy = false; + break; + } + } + } + else + isMakeRootCopy = false; + } + + if( isMakeRootCopy ) + { + // make a copy of the root node + CvDTreeNode temp; + int i; + root = new_node( 0, 1, 0, 0 ); + temp = *root; + *root = *data_root; + root->num_valid = temp.num_valid; + if( root->num_valid ) + { + for( i = 0; i < var_count; i++ ) + root->num_valid[i] = data_root->num_valid[i]; + } + root->cv_Tn = temp.cv_Tn; + root->cv_node_risk = temp.cv_node_risk; + root->cv_node_error = temp.cv_node_error; + } + else + { + int* sidx = isubsample_idx->data.i; + // co - array of count/offset pairs (to handle duplicated values in _subsample_idx) + int* co, cur_ofs = 0; + int workVarCount = get_work_var_count(); + int count = isubsample_idx->rows + isubsample_idx->cols - 1; + + root = new_node( 0, count, 1, 0 ); + + CV_Assert( (subsample_co = cvCreateMat( 1, sample_count*2, CV_32SC(channels) )) != 0); + cvZero( subsample_co ); + co = subsample_co->data.i; + for( int i = 0; i < count; i++ ) + co[sidx[i]*2]++; + for( int i = 0; i < sample_count; i++ ) + { + if( co[i*2] ) + { + co[i*2+1] = cur_ofs; + cur_ofs += co[i*2]; + } + else + co[i*2+1] = -1; + } + + cv::AutoBuffer inn_buf(sample_count*(2*sizeof(int) + sizeof(float))); + // subsample ordered variables + for( int vi = 0; vi < numPrecalcIdx; vi++ ) + { + int ci = get_var_type(vi); + CV_Assert( ci < 0 ); + + int *src_idx_buf = (int*)(uchar*)inn_buf; + float *src_val_buf = (float*)(src_idx_buf + sample_count); + int* sample_indices_buf = (int*)(src_val_buf + sample_count); + const int* src_idx = 0; + const float* src_val = 0; + get_ord_var_data( data_root, vi, src_val_buf, src_idx_buf, &src_val, &src_idx, sample_indices_buf ); + + int j = 0, idx, count_i; + int num_valid = data_root->get_num_valid(vi); + CV_Assert( num_valid == sample_count ); + + if (is_buf_16u) + { + unsigned short* udst_idx = (unsigned short*)(buf->data.s + root->buf_idx*get_length_subbuf() + + vi*sample_count + data_root->offset); + for( int i = 0; i < num_valid; i++ ) + { + idx = src_idx[i]; + count_i = co[idx*2]; + if( count_i ) + for( cur_ofs = co[idx*2+1]; count_i > 0; count_i--, j++, cur_ofs++ ) + udst_idx[j] = (unsigned short)cur_ofs; + } + } + else + { + int* idst_idx = buf->data.i + root->buf_idx*get_length_subbuf() + + vi*sample_count + root->offset; + for( int i = 0; i < num_valid; i++ ) + { + idx = src_idx[i]; + count_i = co[idx*2]; + if( count_i ) + for( cur_ofs = co[idx*2+1]; count_i > 0; count_i--, j++, cur_ofs++ ) + idst_idx[j] = cur_ofs; + } + } + } + + // subsample cv_lables + const int* src_lbls = get_cv_labels(data_root, (int*)(uchar*)inn_buf); + if (is_buf_16u) + { + unsigned short* udst = (unsigned short*)(buf->data.s + root->buf_idx*get_length_subbuf() + + (workVarCount-1)*sample_count + root->offset); + for( int i = 0; i < count; i++ ) + udst[i] = (unsigned short)src_lbls[sidx[i]]; + } + else + { + int* idst = buf->data.i + root->buf_idx*get_length_subbuf() + + (workVarCount-1)*sample_count + root->offset; + for( int i = 0; i < count; i++ ) + idst[i] = src_lbls[sidx[i]]; + } + + // subsample sample_indices + const int* sample_idx_src = get_sample_indices(data_root, (int*)(uchar*)inn_buf); + if (is_buf_16u) + { + unsigned short* sample_idx_dst = (unsigned short*)(buf->data.s + root->buf_idx*get_length_subbuf() + + workVarCount*sample_count + root->offset); + for( int i = 0; i < count; i++ ) + sample_idx_dst[i] = (unsigned short)sample_idx_src[sidx[i]]; + } + else + { + int* sample_idx_dst = buf->data.i + root->buf_idx*get_length_subbuf() + + workVarCount*sample_count + root->offset; + for( int i = 0; i < count; i++ ) + sample_idx_dst[i] = sample_idx_src[sidx[i]]; + } + + for( int vi = 0; vi < var_count; vi++ ) + root->set_num_valid(vi, count); + } + + cvReleaseMat( &isubsample_idx ); + cvReleaseMat( &subsample_co ); + + return root; +} + +CascadeBoostTrainData::CascadeBoostTrainData(const FeatureEvaluator* _featureEvaluator, + int _channels, + const CvDTreeParams& _params) +{ + is_classifier = true; + var_all = var_count = (int)_featureEvaluator->getNumFeatures(); + + featureEvaluator = _featureEvaluator; + channels = _channels; + shared = true; + set_params( _params ); + max_c_count = MAX( 2, featureEvaluator->getMaxCatCount() ); + var_type = cvCreateMat( 1, var_count + 2, CV_32SC(channels) ); + if ( featureEvaluator->getMaxCatCount() > 0 ) + { + numPrecalcIdx = 0; + cat_var_count = var_count; + ord_var_count = 0; + for( int vi = 0; vi < var_count; vi++ ) + { + var_type->data.i[vi] = vi; + } + } + else + { + cat_var_count = 0; + ord_var_count = var_count; + for( int vi = 1; vi <= var_count; vi++ ) + { + var_type->data.i[vi-1] = -vi; + } + } + var_type->data.i[var_count] = cat_var_count; + var_type->data.i[var_count+1] = cat_var_count+1; + + int maxSplitSize = cvAlign(sizeof(CvDTreeSplit) + (MAX(0,max_c_count - 33)/32)*sizeof(int),sizeof(void*)); + int treeBlockSize = MAX((int)sizeof(CvDTreeNode)*8, maxSplitSize); + treeBlockSize = MAX(treeBlockSize + BlockSizeDelta, MinBlockSize); + tree_storage = cvCreateMemStorage( treeBlockSize ); + node_heap = cvCreateSet( 0, sizeof(node_heap[0]), sizeof(CvDTreeNode), tree_storage ); + split_heap = cvCreateSet( 0, sizeof(split_heap[0]), maxSplitSize, tree_storage ); +} + +CascadeBoostTrainData::CascadeBoostTrainData(const FeatureEvaluator* _featureEvaluator, + int _numSamples, + int _precalcValBufSize, int _precalcIdxBufSize, int _channels , + const CvDTreeParams& _params) +{ + channels = _channels; + setData( _featureEvaluator, _numSamples, _precalcValBufSize, _precalcIdxBufSize, _params ); +} + +void CascadeBoostTrainData::setData( const FeatureEvaluator* _featureEvaluator, + int _numSamples, + int _precalcValBufSize, int _precalcIdxBufSize, + const CvDTreeParams& _params ) +{ + int* idst = 0; + unsigned short* udst = 0; + + uint64 effective_buf_size = 0; + int effective_buf_height = 0, effective_buf_width = 0; + + clear(); + shared = true; + have_labels = true; + have_priors = false; + is_classifier = true; + + rng = &cv::theRNG(); + + set_params( _params ); + + CV_Assert( _featureEvaluator ); + featureEvaluator = _featureEvaluator; + + max_c_count = MAX( 2, featureEvaluator->getMaxCatCount() ); + _resp = featureEvaluator->getCls(); + responses = &_resp; + // TODO: check responses: elements must be 0 or 1 + + if( _precalcValBufSize < 0 || _precalcIdxBufSize < 0) + CV_Error( CV_StsOutOfRange, "_numPrecalcVal and _numPrecalcIdx must be positive or 0" ); + + var_count = var_all = featureEvaluator->getNumFeatures() * featureEvaluator->getFeatureSize(); + sample_count = _numSamples; + + is_buf_16u = false; + if (sample_count < 65536) + is_buf_16u = true; + + numPrecalcVal = min( cvRound((double)_precalcValBufSize*1048576. / (sizeof(float)*sample_count)), var_count ); + numPrecalcIdx = min( cvRound((double)_precalcIdxBufSize*1048576. / + ((is_buf_16u ? sizeof(unsigned short) : sizeof (int))*sample_count)), var_count ); + + assert( numPrecalcIdx >= 0 && numPrecalcVal >= 0 ); + + valCache.create( numPrecalcVal, sample_count, CV_32FC1 ); + var_type = cvCreateMat( 1, var_count + 2, CV_32SC(channels) ); + if ( featureEvaluator->getMaxCatCount() > 0 ) + { + numPrecalcIdx = 0; + cat_var_count = var_count; + ord_var_count = 0; + for( int vi = 0; vi < var_count; vi++ ) + { + var_type->data.i[vi] = vi; + } + } + else + { + cat_var_count = 0; + ord_var_count = var_count; + for( int vi = 1; vi <= var_count; vi++ ) + { + var_type->data.i[vi-1] = -vi; + } + } + var_type->data.i[var_count] = cat_var_count; + var_type->data.i[var_count+1] = cat_var_count+1; + work_var_count = ( cat_var_count ? 0 : numPrecalcIdx ) + 1/*cv_lables*/; + buf_count = 2; + + buf_size = -1; // the member buf_size is obsolete + + effective_buf_size = (uint64)(work_var_count + 1)*(uint64)sample_count * buf_count; // this is the total size of "CvMat buf" to be allocated + effective_buf_width = sample_count; + effective_buf_height = work_var_count+1; + + if (effective_buf_width >= effective_buf_height) + effective_buf_height *= buf_count; + else + effective_buf_width *= buf_count; + + if ((uint64)effective_buf_width * (uint64)effective_buf_height != effective_buf_size) + { + CV_Error(CV_StsBadArg, "The memory buffer cannot be allocated since its size exceeds integer fields limit"); + } + if ( is_buf_16u ) + buf = cvCreateMat( effective_buf_height, effective_buf_width, CV_16UC(channels) ); + else + buf = cvCreateMat( effective_buf_height, effective_buf_width, CV_32SC(channels) ); + + cat_count = cvCreateMat( 1, cat_var_count + 1, CV_32SC(channels) ); + + // precalculate valCache and set indices in buf + precalculate(); + + // now calculate the maximum size of split, + // create memory storage that will keep nodes and splits of the decision tree + // allocate root node and the buffer for the whole training data + int maxSplitSize = cvAlign(sizeof(CvDTreeSplit) + + (MAX(0,sample_count - 33)/32)*sizeof(int),sizeof(void*)); + int treeBlockSize = MAX((int)sizeof(CvDTreeNode)*8, maxSplitSize); + treeBlockSize = MAX(treeBlockSize + BlockSizeDelta, MinBlockSize); + tree_storage = cvCreateMemStorage( treeBlockSize ); + node_heap = cvCreateSet( 0, sizeof(*node_heap), sizeof(CvDTreeNode), tree_storage ); + + int nvSize = var_count*sizeof(int); + nvSize = cvAlign(MAX( nvSize, (int)sizeof(CvSetElem) ), sizeof(void*)); + int tempBlockSize = nvSize; + tempBlockSize = MAX( tempBlockSize + BlockSizeDelta, MinBlockSize ); + temp_storage = cvCreateMemStorage( tempBlockSize ); + nv_heap = cvCreateSet( 0, sizeof(*nv_heap), nvSize, temp_storage ); + + data_root = new_node( 0, sample_count, 0, 0 ); + + // set sample labels + if (is_buf_16u) + udst = (unsigned short*)(buf->data.s + work_var_count*sample_count); + else + idst = buf->data.i + work_var_count*sample_count; + + for (int si = 0; si < sample_count; si++) + { + if (udst) + udst[si] = (unsigned short)si; + else + idst[si] = si; + } + for( int vi = 0; vi < var_count; vi++ ) + data_root->set_num_valid(vi, sample_count); + for( int vi = 0; vi < cat_var_count; vi++ ) + cat_count->data.i[vi] = max_c_count; + + cat_count->data.i[cat_var_count] = 2; + + maxSplitSize = cvAlign(sizeof(CvDTreeSplit) + + (MAX(0,max_c_count - 33)/32)*sizeof(int),sizeof(void*)); + split_heap = cvCreateSet( 0, sizeof(*split_heap), maxSplitSize, tree_storage ); + + priors = cvCreateMat( 1, get_num_classes(), CV_64F ); + cvSet(priors, cvScalar(1)); + priors_mult = cvCloneMat( priors ); + counts = cvCreateMat( 1, get_num_classes(), CV_32SC(channels) ); + direction = cvCreateMat( 1, sample_count, CV_8UC(channels) ); + split_buf = cvCreateMat( 1, sample_count, CV_32SC(channels) );//TODO: make a pointer +} + +void CascadeBoostTrainData::free_train_data() +{ + CvDTreeTrainData::free_train_data(); + valCache.release(); +} + +const int* CascadeBoostTrainData::get_class_labels( CvDTreeNode* n, int* labelsBuf) +{ + int nodeSampleCount = n->sample_count; + int rStep = CV_IS_MAT_CONT( responses->type ) ? 1 : responses->step / CV_ELEM_SIZE( responses->type ); + + int* sampleIndicesBuf = labelsBuf; // + const int* sampleIndices = get_sample_indices(n, sampleIndicesBuf); + for( int si = 0; si < nodeSampleCount; si++ ) + { + int sidx = sampleIndices[si]; + labelsBuf[si] = (int)responses->data.fl[sidx*rStep]; + } + return labelsBuf; +} + +const int* CascadeBoostTrainData::get_sample_indices( CvDTreeNode* n, int* indicesBuf ) +{ + return CvDTreeTrainData::get_cat_var_data( n, get_work_var_count(), indicesBuf ); +} + +const int* CascadeBoostTrainData::get_cv_labels( CvDTreeNode* n, int* labels_buf ) +{ + return CvDTreeTrainData::get_cat_var_data( n, get_work_var_count() - 1, labels_buf ); +} + +void CascadeBoostTrainData::get_ord_var_data( CvDTreeNode* n, int vi, float* ordValuesBuf, int* sortedIndicesBuf, + const float** ordValues, const int** sortedIndices, int* sampleIndicesBuf ) +{ + int nodeSampleCount = n->sample_count; + const int* sampleIndices = get_sample_indices(n, sampleIndicesBuf); + + if ( vi < numPrecalcIdx ) + { + if( !is_buf_16u ) + *sortedIndices = buf->data.i + n->buf_idx*get_length_subbuf() + vi*sample_count + n->offset; + else + { + const unsigned short* shortIndices = (const unsigned short*)(buf->data.s + n->buf_idx*get_length_subbuf() + + vi*sample_count + n->offset ); + for( int i = 0; i < nodeSampleCount; i++ ) + sortedIndicesBuf[i] = shortIndices[i]; + + *sortedIndices = sortedIndicesBuf; + } + + if( vi < numPrecalcVal ) + { + for( int i = 0; i < nodeSampleCount; i++ ) + { + int idx = (*sortedIndices)[i]; + idx = sampleIndices[idx]; + ordValuesBuf[i] = valCache.at( vi, idx); + } + } + else + { + for( int i = 0; i < nodeSampleCount; i++ ) + { + int idx = (*sortedIndices)[i]; + idx = sampleIndices[idx]; + ordValuesBuf[i] = (*featureEvaluator)( vi, idx); + } + } + } + else // vi >= numPrecalcIdx + { + cv::AutoBuffer abuf(nodeSampleCount); + float* sampleValues = &abuf[0]; + + if ( vi < numPrecalcVal ) + { + for( int i = 0; i < nodeSampleCount; i++ ) + { + sortedIndicesBuf[i] = i; + sampleValues[i] = valCache.at( vi, sampleIndices[i] ); + } + } + else + { + for( int i = 0; i < nodeSampleCount; i++ ) + { + sortedIndicesBuf[i] = i; + sampleValues[i] = (*featureEvaluator)( vi, sampleIndices[i]); + } + } + icvSortIntAux( sortedIndicesBuf, nodeSampleCount, &sampleValues[0] ); + for( int i = 0; i < nodeSampleCount; i++ ) + ordValuesBuf[i] = (&sampleValues[0])[sortedIndicesBuf[i]]; + *sortedIndices = sortedIndicesBuf; + } + + *ordValues = ordValuesBuf; +} + +const int* CascadeBoostTrainData::get_cat_var_data( CvDTreeNode* n, int vi, int* catValuesBuf ) +{ + int nodeSampleCount = n->sample_count; + int* sampleIndicesBuf = catValuesBuf; // + const int* sampleIndices = get_sample_indices(n, sampleIndicesBuf); + + if ( vi < numPrecalcVal ) + { + for( int i = 0; i < nodeSampleCount; i++ ) + catValuesBuf[i] = (int) valCache.at( vi, sampleIndices[i]); + } + else + { + if( vi >= numPrecalcVal && vi < var_count ) + { + for( int i = 0; i < nodeSampleCount; i++ ) + catValuesBuf[i] = (int)(*featureEvaluator)( vi, sampleIndices[i] ); + } + else + { + get_cv_labels( n, catValuesBuf ); + } + } + + return catValuesBuf; +} + +float CascadeBoostTrainData::getVarValue( int vi, int si ) +{ + if ( vi < numPrecalcVal && !valCache.empty() ) + return valCache.at( vi, si ); + return (*featureEvaluator)( vi, si ); +} + +struct FeatureIdxOnlyPrecalc : ParallelLoopBody +{ + FeatureIdxOnlyPrecalc( const FeatureEvaluator* _featureEvaluator, CvMat* _buf, int _sample_count, bool _is_buf_16u ) + { + featureEvaluator = _featureEvaluator; + sample_count = _sample_count; + udst = (unsigned short*)_buf->data.s; + idst = _buf->data.i; + is_buf_16u = _is_buf_16u; + } + void operator()( const Range& range ) const + { + cv::AutoBuffer valCache(sample_count); + float* valCachePtr = (float*)valCache; + for ( int fi = range.start; fi < range.end; fi++) + { + for( int si = 0; si < sample_count; si++ ) + { + valCachePtr[si] = (*featureEvaluator)( fi, si ); + if ( is_buf_16u ) + *(udst + fi*sample_count + si) = (unsigned short)si; + else + *(idst + fi*sample_count + si) = si; + } + if ( is_buf_16u ) + icvSortUShAux( udst + fi*sample_count, sample_count, valCachePtr ); + else + icvSortIntAux( idst + fi*sample_count, sample_count, valCachePtr ); + } + } + const FeatureEvaluator* featureEvaluator; + int sample_count; + int* idst; + unsigned short* udst; + bool is_buf_16u; +}; + +struct FeatureValAndIdxPrecalc : ParallelLoopBody +{ + FeatureValAndIdxPrecalc( const FeatureEvaluator* _featureEvaluator, CvMat* _buf, Mat* _valCache, int _sample_count, bool _is_buf_16u ) + { + featureEvaluator = _featureEvaluator; + valCache = _valCache; + sample_count = _sample_count; + udst = (unsigned short*)_buf->data.s; + idst = _buf->data.i; + is_buf_16u = _is_buf_16u; + } + void operator()( const Range& range ) const + { + for ( int fi = range.start; fi < range.end; fi++) + { + for( int si = 0; si < sample_count; si++ ) + { + valCache->at(fi,si) = (*featureEvaluator)( fi, si ); + if ( is_buf_16u ) + *(udst + fi*sample_count + si) = (unsigned short)si; + else + *(idst + fi*sample_count + si) = si; + } + if ( is_buf_16u ) + icvSortUShAux( udst + fi*sample_count, sample_count, valCache->ptr(fi) ); + else + icvSortIntAux( idst + fi*sample_count, sample_count, valCache->ptr(fi) ); + } + } + const FeatureEvaluator* featureEvaluator; + Mat* valCache; + int sample_count; + int* idst; + unsigned short* udst; + bool is_buf_16u; +}; + +struct FeatureValOnlyPrecalc : ParallelLoopBody +{ + FeatureValOnlyPrecalc( const FeatureEvaluator* _featureEvaluator, Mat* _valCache, int _sample_count ) + { + featureEvaluator = _featureEvaluator; + valCache = _valCache; + sample_count = _sample_count; + } + void operator()( const Range& range ) const + { + for ( int fi = range.start; fi < range.end; fi++) + for( int si = 0; si < sample_count; si++ ) + valCache->at(fi,si) = (*featureEvaluator)( fi, si ); + } + const FeatureEvaluator* featureEvaluator; + Mat* valCache; + int sample_count; +}; + +void CascadeBoostTrainData::precalculate() +{ + int minNum = MIN( numPrecalcVal, numPrecalcIdx); + + double proctime = -TIME( 0 ); + parallel_for_( Range(numPrecalcVal, numPrecalcIdx), + FeatureIdxOnlyPrecalc(featureEvaluator, buf, sample_count, is_buf_16u!=0) ); + parallel_for_( Range(0, minNum), + FeatureValAndIdxPrecalc(featureEvaluator, buf, &valCache, sample_count, is_buf_16u!=0) ); + parallel_for_( Range(minNum, numPrecalcVal), + FeatureValOnlyPrecalc(featureEvaluator, &valCache, sample_count) ); + cout << "Precalculation time: " << (proctime + TIME( 0 )) << endl; +} + +//-------------------------------- CascadeBoostTree ---------------------------------------- + +CvDTreeNode* CascadeBoostTree::predict( int sampleIdx ) const +{ + CvDTreeNode* node = root; + if( !node ) + CV_Error( CV_StsError, "The tree has not been trained yet" ); + + if ( ((CascadeBoostTrainData*)data)->featureEvaluator->getMaxCatCount() == 0 ) // ordered + { + while( node->left ) + { + CvDTreeSplit* split = node->split; + float val = ((CascadeBoostTrainData*)data)->getVarValue( split->var_idx, sampleIdx ); + node = val <= split->ord.c ? node->left : node->right; + } + } + else // categorical + { + while( node->left ) + { + CvDTreeSplit* split = node->split; + int c = (int)((CascadeBoostTrainData*)data)->getVarValue( split->var_idx, sampleIdx ); + node = CV_DTREE_CAT_DIR(c, split->subset) < 0 ? node->left : node->right; + } + } + return node; +} + +void CascadeBoostTree::split_node_data( CvDTreeNode* node ) +{ + int n = node->sample_count, nl, nr, scount = data->sample_count; + char* dir = (char*)data->direction->data.ptr; + CvDTreeNode *left = 0, *right = 0; + int* newIdx = data->split_buf->data.i; + int newBufIdx = data->get_child_buf_idx( node ); + int workVarCount = data->get_work_var_count(); + CvMat* buf = data->buf; + size_t length_buf_row = data->get_length_subbuf(); + cv::AutoBuffer inn_buf(n*(3*sizeof(int)+sizeof(float))); + int* tempBuf = (int*)(uchar*)inn_buf; + bool splitInputData; + + complete_node_dir(node); + + for( int i = nl = nr = 0; i < n; i++ ) + { + int d = dir[i]; + // initialize new indices for splitting ordered variables + newIdx[i] = (nl & (d-1)) | (nr & -d); // d ? ri : li + nr += d; + nl += d^1; + } + + node->left = left = data->new_node( node, nl, newBufIdx, node->offset ); + node->right = right = data->new_node( node, nr, newBufIdx, node->offset + nl ); + + splitInputData = node->depth + 1 < data->params.max_depth && + (node->left->sample_count > data->params.min_sample_count || + node->right->sample_count > data->params.min_sample_count); + + // split ordered variables, keep both halves sorted. + for( int vi = 0; vi < ((CascadeBoostTrainData*)data)->numPrecalcIdx; vi++ ) + { + int ci = data->get_var_type(vi); + if( ci >= 0 || !splitInputData ) + continue; + + int n1 = node->get_num_valid(vi); + float *src_val_buf = (float*)(tempBuf + n); + int *src_sorted_idx_buf = (int*)(src_val_buf + n); + int *src_sample_idx_buf = src_sorted_idx_buf + n; + const int* src_sorted_idx = 0; + const float* src_val = 0; + data->get_ord_var_data(node, vi, src_val_buf, src_sorted_idx_buf, &src_val, &src_sorted_idx, src_sample_idx_buf); + + for(int i = 0; i < n; i++) + tempBuf[i] = src_sorted_idx[i]; + + if (data->is_buf_16u) + { + ushort *ldst, *rdst; + ldst = (ushort*)(buf->data.s + left->buf_idx*length_buf_row + + vi*scount + left->offset); + rdst = (ushort*)(ldst + nl); + + // split sorted + for( int i = 0; i < n1; i++ ) + { + int idx = tempBuf[i]; + int d = dir[idx]; + idx = newIdx[idx]; + if (d) + { + *rdst = (ushort)idx; + rdst++; + } + else + { + *ldst = (ushort)idx; + ldst++; + } + } + CV_Assert( n1 == n ); + } + else + { + int *ldst, *rdst; + ldst = buf->data.i + left->buf_idx*length_buf_row + + vi*scount + left->offset; + rdst = buf->data.i + right->buf_idx*length_buf_row + + vi*scount + right->offset; + + // split sorted + for( int i = 0; i < n1; i++ ) + { + int idx = tempBuf[i]; + int d = dir[idx]; + idx = newIdx[idx]; + if (d) + { + *rdst = idx; + rdst++; + } + else + { + *ldst = idx; + ldst++; + } + } + CV_Assert( n1 == n ); + } + } + + // split cv_labels using newIdx relocation table + int *src_lbls_buf = tempBuf + n; + const int* src_lbls = data->get_cv_labels(node, src_lbls_buf); + + for(int i = 0; i < n; i++) + tempBuf[i] = src_lbls[i]; + + if (data->is_buf_16u) + { + unsigned short *ldst = (unsigned short *)(buf->data.s + left->buf_idx*length_buf_row + + (workVarCount-1)*scount + left->offset); + unsigned short *rdst = (unsigned short *)(buf->data.s + right->buf_idx*length_buf_row + + (workVarCount-1)*scount + right->offset); + + for( int i = 0; i < n; i++ ) + { + int idx = tempBuf[i]; + if (dir[i]) + { + *rdst = (unsigned short)idx; + rdst++; + } + else + { + *ldst = (unsigned short)idx; + ldst++; + } + } + + } + else + { + int *ldst = buf->data.i + left->buf_idx*length_buf_row + + (workVarCount-1)*scount + left->offset; + int *rdst = buf->data.i + right->buf_idx*length_buf_row + + (workVarCount-1)*scount + right->offset; + + for( int i = 0; i < n; i++ ) + { + int idx = tempBuf[i]; + if (dir[i]) + { + *rdst = idx; + rdst++; + } + else + { + *ldst = idx; + ldst++; + } + } + } + + // split sample indices + int *sampleIdx_src_buf = tempBuf + n; + const int* sampleIdx_src = data->get_sample_indices(node, sampleIdx_src_buf); + + for(int i = 0; i < n; i++) + tempBuf[i] = sampleIdx_src[i]; + + if (data->is_buf_16u) + { + unsigned short* ldst = (unsigned short*)(buf->data.s + left->buf_idx*length_buf_row + + workVarCount*scount + left->offset); + unsigned short* rdst = (unsigned short*)(buf->data.s + right->buf_idx*length_buf_row + + workVarCount*scount + right->offset); + for (int i = 0; i < n; i++) + { + unsigned short idx = (unsigned short)tempBuf[i]; + if (dir[i]) + { + *rdst = idx; + rdst++; + } + else + { + *ldst = idx; + ldst++; + } + } + } + else + { + int* ldst = buf->data.i + left->buf_idx*length_buf_row + + workVarCount*scount + left->offset; + int* rdst = buf->data.i + right->buf_idx*length_buf_row + + workVarCount*scount + right->offset; + for (int i = 0; i < n; i++) + { + int idx = tempBuf[i]; + if (dir[i]) + { + *rdst = idx; + rdst++; + } + else + { + *ldst = idx; + ldst++; + } + } + } + + for( int vi = 0; vi < data->var_count; vi++ ) + { + left->set_num_valid(vi, (int)(nl)); + right->set_num_valid(vi, (int)(nr)); + } + + // deallocate the parent node data that is not needed anymore + data->free_node_data(node); +} + +//----------------------------------- CascadeBoost -------------------------------------- + +void CascadeBoost::train(const FeatureEvaluator* _featureEvaluator, + int _numSamples, + int _precalcValBufSize, int _precalcIdxBufSize, + int _channels, + const CascadeBoostParams& _params) +{ + CV_Assert(!data); + clear(); + + channels = _channels; + + data = new CascadeBoostTrainData(_featureEvaluator, _numSamples, + _precalcValBufSize, _precalcIdxBufSize, channels, _params); + + set_params(_params); + if ((_params.boost_type == LOGIT) || (_params.boost_type == GENTLE)) + data->do_responses_copy(); + + update_weights(0); + + cout << "+----+---------+---------+" << endl; + cout << "| N | HR | FA |" << endl; + cout << "+----+---------+---------+" << endl; + + do + { + CascadeBoostTree* tree = new CascadeBoostTree; + if (!tree->train( data, subsample_mask, this)) { + delete tree; + return; + } + + classifiers.append(tree); + update_weights(tree); + trim_weights(); + if (cvCountNonZero(subsample_mask) == 0) + return; + } + while (!isErrDesired() && (classifiers.size() < params.weak_count)); + + clear(); +} + +float CascadeBoost::predict(int sampleIdx, bool returnSum) const +{ + double sum = 0; + foreach (const CvBoostTree *tree, classifiers) + sum += ((CascadeBoostTree*)tree)->predict(sampleIdx)->value; + + if (!returnSum) + sum = sum < threshold - CV_THRESHOLD_EPS ? 0.0 : 1.0; + return (float)sum; +} + +bool CascadeBoost::set_params(const CvBoostParams& _params) +{ + minHitRate = ((CascadeBoostParams&)_params).minHitRate; + maxFalseAlarm = ((CascadeBoostParams&)_params).maxFalseAlarm; + return (( minHitRate > 0 ) && ( minHitRate < 1) && + (maxFalseAlarm > 0 ) && ( maxFalseAlarm < 1) && + CvBoost::set_params(_params)); +} + +void CascadeBoost::update_weights(CvBoostTree* tree) +{ + int n = data->sample_count; + double sumW = 0.; + int step = 0; + float* fdata = 0; + int *sampleIdxBuf; + const int* sampleIdx = 0; + int inn_buf_size = ((params.boost_type == LOGIT) || (params.boost_type == GENTLE) ? n*sizeof(int) : 0) + + ( !tree ? n*sizeof(int) : 0 ); + cv::AutoBuffer inn_buf(inn_buf_size); + uchar* cur_inn_buf_pos = (uchar*)inn_buf; + if ( (params.boost_type == LOGIT) || (params.boost_type == GENTLE) ) + { + step = CV_IS_MAT_CONT(data->responses_copy->type) ? + 1 : data->responses_copy->step / CV_ELEM_SIZE(data->responses_copy->type); + fdata = data->responses_copy->data.fl; + sampleIdxBuf = (int*)cur_inn_buf_pos; cur_inn_buf_pos = (uchar*)(sampleIdxBuf + n); + sampleIdx = data->get_sample_indices( data->data_root, sampleIdxBuf ); + } + CvMat* buf = data->buf; + size_t length_buf_row = data->get_length_subbuf(); + if( !tree ) // before training the first tree, initialize weights and other parameters + { + int* classLabelsBuf = (int*)cur_inn_buf_pos; cur_inn_buf_pos = (uchar*)(classLabelsBuf + n); + const int* classLabels = data->get_class_labels(data->data_root, classLabelsBuf); + // in case of logitboost and gentle adaboost each weak tree is a regression tree, + // so we need to convert class labels to floating-point values + double w0 = 1./n; + double p[2] = { 1, 1 }; + + cvReleaseMat( &orig_response ); + cvReleaseMat( &sum_response ); + cvReleaseMat( &weak_eval ); + cvReleaseMat( &subsample_mask ); + cvReleaseMat( &weights ); + + orig_response = cvCreateMat( 1, n, CV_32S ); + weak_eval = cvCreateMat( 1, n, CV_64F ); + subsample_mask = cvCreateMat( 1, n, CV_8U ); + weights = cvCreateMat( 1, n, CV_64F ); + subtree_weights = cvCreateMat( 1, n + 2, CV_64F ); + + if (data->is_buf_16u) + { + unsigned short* labels = (unsigned short*)(buf->data.s + data->data_root->buf_idx*length_buf_row + + data->data_root->offset + (data->work_var_count-1)*data->sample_count); + for( int i = 0; i < n; i++ ) + { + // save original categorical responses {0,1}, convert them to {-1,1} + orig_response->data.i[i] = classLabels[i]*2 - 1; + // make all the samples active at start. + // later, in trim_weights() deactivate/reactive again some, if need + subsample_mask->data.ptr[i] = (uchar)1; + // make all the initial weights the same. + weights->data.db[i] = w0*p[classLabels[i]]; + // set the labels to find (from within weak tree learning proc) + // the particular sample weight, and where to store the response. + labels[i] = (unsigned short)i; + } + } + else + { + int* labels = buf->data.i + data->data_root->buf_idx*length_buf_row + + data->data_root->offset + (data->work_var_count-1)*data->sample_count; + + for( int i = 0; i < n; i++ ) + { + // save original categorical responses {0,1}, convert them to {-1,1} + orig_response->data.i[i] = classLabels[i]*2 - 1; + subsample_mask->data.ptr[i] = (uchar)1; + weights->data.db[i] = w0*p[classLabels[i]]; + labels[i] = i; + } + } + + if( params.boost_type == LOGIT ) + { + sum_response = cvCreateMat( 1, n, CV_64F ); + + for( int i = 0; i < n; i++ ) + { + sum_response->data.db[i] = 0; + fdata[sampleIdx[i]*step] = orig_response->data.i[i] > 0 ? 2.f : -2.f; + } + + // in case of logitboost each weak tree is a regression tree. + // the target function values are recalculated for each of the trees + data->is_classifier = false; + } + else if( params.boost_type == GENTLE ) + { + for( int i = 0; i < n; i++ ) + fdata[sampleIdx[i]*step] = (float)orig_response->data.i[i]; + + data->is_classifier = false; + } + } + else + { + // at this moment, for all the samples that participated in the training of the most + // recent weak classifier we know the responses. For other samples we need to compute them + if( have_subsample ) + { + // invert the subsample mask + cvXorS( subsample_mask, cvScalar(1.), subsample_mask ); + + // run tree through all the non-processed samples + for( int i = 0; i < n; i++ ) + if( subsample_mask->data.ptr[i] ) + { + weak_eval->data.db[i] = ((CascadeBoostTree*)tree)->predict( i )->value; + } + } + + // now update weights and other parameters for each type of boosting + if( params.boost_type == DISCRETE ) + { + // Discrete AdaBoost: + // weak_eval[i] (=f(x_i)) is in {-1,1} + // err = sum(w_i*(f(x_i) != y_i))/sum(w_i) + // C = log((1-err)/err) + // w_i *= exp(C*(f(x_i) != y_i)) + + double C, err = 0.; + double scale[] = { 1., 0. }; + + for( int i = 0; i < n; i++ ) + { + double w = weights->data.db[i]; + sumW += w; + err += w*(weak_eval->data.db[i] != orig_response->data.i[i]); + } + + if( sumW != 0 ) + err /= sumW; + C = err = -logRatio( err ); + scale[1] = exp(err); + + sumW = 0; + for( int i = 0; i < n; i++ ) + { + double w = weights->data.db[i]* + scale[weak_eval->data.db[i] != orig_response->data.i[i]]; + sumW += w; + weights->data.db[i] = w; + } + + tree->scale( C ); + } + else if( params.boost_type == REAL ) + { + // Real AdaBoost: + // weak_eval[i] = f(x_i) = 0.5*log(p(x_i)/(1-p(x_i))), p(x_i)=P(y=1|x_i) + // w_i *= exp(-y_i*f(x_i)) + + for( int i = 0; i < n; i++ ) + weak_eval->data.db[i] *= -orig_response->data.i[i]; + + cvExp( weak_eval, weak_eval ); + + for( int i = 0; i < n; i++ ) + { + double w = weights->data.db[i]*weak_eval->data.db[i]; + sumW += w; + weights->data.db[i] = w; + } + } + else if( params.boost_type == LOGIT ) + { + // LogitBoost: + // weak_eval[i] = f(x_i) in [-z_max,z_max] + // sum_response = F(x_i). + // F(x_i) += 0.5*f(x_i) + // p(x_i) = exp(F(x_i))/(exp(F(x_i)) + exp(-F(x_i))=1/(1+exp(-2*F(x_i))) + // reuse weak_eval: weak_eval[i] <- p(x_i) + // w_i = p(x_i)*1(1 - p(x_i)) + // z_i = ((y_i+1)/2 - p(x_i))/(p(x_i)*(1 - p(x_i))) + // store z_i to the data->data_root as the new target responses + + const double lbWeightThresh = FLT_EPSILON; + const double lbZMax = 10.; + + for( int i = 0; i < n; i++ ) + { + double s = sum_response->data.db[i] + 0.5*weak_eval->data.db[i]; + sum_response->data.db[i] = s; + weak_eval->data.db[i] = -2*s; + } + + cvExp( weak_eval, weak_eval ); + + for( int i = 0; i < n; i++ ) + { + double p = 1./(1. + weak_eval->data.db[i]); + double w = p*(1 - p), z; + w = MAX( w, lbWeightThresh ); + weights->data.db[i] = w; + sumW += w; + if( orig_response->data.i[i] > 0 ) + { + z = 1./p; + fdata[sampleIdx[i]*step] = (float)min(z, lbZMax); + } + else + { + z = 1./(1-p); + fdata[sampleIdx[i]*step] = (float)-min(z, lbZMax); + } + } + } + else + { + // Gentle AdaBoost: + // weak_eval[i] = f(x_i) in [-1,1] + // w_i *= exp(-y_i*f(x_i)) + assert( params.boost_type == GENTLE ); + + for( int i = 0; i < n; i++ ) + weak_eval->data.db[i] *= -orig_response->data.i[i]; + + cvExp( weak_eval, weak_eval ); + + for( int i = 0; i < n; i++ ) + { + double w = weights->data.db[i] * weak_eval->data.db[i]; + weights->data.db[i] = w; + sumW += w; + } + } + } + + // renormalize weights + if( sumW > FLT_EPSILON ) + { + sumW = 1./sumW; + for( int i = 0; i < n; ++i ) + weights->data.db[i] *= sumW; + } +} + +bool CascadeBoost::isErrDesired() +{ + QList posVals; + for (int i = 0; i < data->sample_count; i++) + if (((CascadeBoostTrainData*)data)->featureEvaluator->getCls(i) == 1.0F) + posVals.append(predict(i, true)); + + std::sort(posVals.begin(), posVals.end()); + + int thresholdIdx = (int)((1.0F - minHitRate) * posVals.size()); + threshold = posVals[thresholdIdx]; + + int numPosTrue = posVals.size() - thresholdIdx; + for (int i = thresholdIdx - 1; i >= 0; i--) + if (abs(posVals[i] - threshold) < FLT_EPSILON) + numPosTrue++; + float hitRate = ((float)numPosTrue) / ((float)posVals.size()); + + int numNeg = 0, numFalse = 0; + for (int i = 0; i < data->sample_count; i++) { + if (((CascadeBoostTrainData*)data)->featureEvaluator->getCls(i) == 0.0F) { + numNeg++; + if (predict(i)) + numFalse++; + } + } + float falseAlarm = ((float)numFalse) / ((float)numNeg); + + cout << "|"; cout.width(4); cout << right << classifiers.size(); + cout << "|"; cout.width(9); cout << right << hitRate; + cout << "|"; cout.width(9); cout << right << falseAlarm; + cout << "|" << endl; + cout << "+----+---------+---------+" << endl; + + return falseAlarm <= maxFalseAlarm; +} diff --git a/openbr/core/boost.h b/openbr/core/boost.h new file mode 100644 index 0000000..d936022 --- /dev/null +++ b/openbr/core/boost.h @@ -0,0 +1,83 @@ +#ifndef _BOOST_H_ +#define _BOOST_H_ + +#include "ml.h" +#include + +#ifdef _WIN32 +#define TIME( arg ) (((double) clock()) / CLOCKS_PER_SEC) +#else +#define TIME( arg ) (time( arg )) +#endif + +namespace br +{ + +struct FeatureEvaluator +{ + ~FeatureEvaluator() {} + void init(Representation *_representation, int _maxSampleCount, int channels); + void setImage(const cv::Mat& img, uchar clsLabel, int idx); + float operator()(int featureIdx, int sampleIdx) const { return representation->evaluate(data.row(sampleIdx), featureIdx); } + + int getNumFeatures() const { return representation->numFeatures(); } + int getMaxCatCount() const { return representation->maxCatCount(); } + int getFeatureSize() const { return 1; } + const cv::Mat& getCls() const { return cls; } + float getCls(int si) const { return cls.at(si, 0); } + + cv::Mat data, cls; + Representation *representation; +}; + +struct CascadeBoostParams : CvBoostParams +{ + float minHitRate; + float maxFalseAlarm; + + CascadeBoostParams(); + CascadeBoostParams(int _boostType, float _minHitRate, float _maxFalseAlarm, + double _weightTrimRate, int _maxDepth, int _maxWeakCount); + virtual ~CascadeBoostParams() {} +}; + +class CascadeBoostTree : public CvBoostTree +{ +public: + using CvBoostTree::predict; + virtual CvDTreeNode* predict(int sampleIdx) const; + +protected: + virtual void split_node_data(CvDTreeNode* n); +}; + +class CascadeBoost : public CvBoost +{ +public: + using CvBoost::train; + virtual void train(const FeatureEvaluator *_featureEvaluator, + int _numSamples, int _precalcValBufSize, int _precalcIdxBufSize, int _channels, + const CascadeBoostParams &_params=CascadeBoostParams()); + + using CvBoost::predict; + virtual float predict( int sampleIdx, bool returnSum = false ) const; + + float getThreshold() const { return threshold; } + QList getClassifers() const { return classifiers; } + +protected: + virtual bool set_params(const CvBoostParams& _params); + virtual void update_weights(CvBoostTree* tree); + virtual bool isErrDesired(); + + QList classifiers; + + float threshold; + float minHitRate, maxFalseAlarm; + int channels; +}; + +} // namespace br + +#endif + diff --git a/openbr/core/opencvutils.cpp b/openbr/core/opencvutils.cpp index ccee66a..7a43f95 100644 --- a/openbr/core/opencvutils.cpp +++ b/openbr/core/opencvutils.cpp @@ -382,6 +382,129 @@ bool OpenCVUtils::overlaps(const QList &posRects, const Rect &negRect, dou return false; } +// class for grouping object candidates, detected by Cascade Classifier, HOG etc. +// instance of the class is to be passed to cv::partition (see cxoperations.hpp) +class SimilarRects +{ +public: + SimilarRects(double _eps) : eps(_eps) {} + inline bool operator()(const Rect& r1, const Rect& r2) const + { + double delta = eps*(std::min(r1.width, r2.width) + std::min(r1.height, r2.height))*0.5; + return std::abs(r1.x - r2.x) <= delta && + std::abs(r1.y - r2.y) <= delta && + std::abs(r1.x + r1.width - r2.x - r2.width) <= delta && + std::abs(r1.y + r1.height - r2.y - r2.height) <= delta; + } + double eps; +}; + +// TODO: Make sure case where no confidences are inputted works. +void OpenCVUtils::group(vector &rects, vector &confidences, float confidenceThreshold, float epsilon) +{ + if (rects.empty()) + return; + + const bool useConfidences = !confidences.empty(); + + vector labels; + int nClasses = cv::partition(rects, labels, SimilarRects(epsilon)); + + // Rect for each class (class meaning identity assigned by partition) + vector rrects(nClasses); + + // Total number of rects in each class + vector rweights(nClasses, 0); + vector rejectWeights(nClasses, -std::numeric_limits::max()); + + for (int i = 0; i < labels.size(); i++) + { + int cls = labels[i]; + rrects[cls].x += rects[i].x; + rrects[cls].y += rects[i].y; + rrects[cls].width += rects[i].width; + rrects[cls].height += rects[i].height; + rweights[cls]++; + } + + if (useConfidences) + { + // For each class, find maximum confidence + for (int i = 0; i < labels.size(); i++) + { + int cls = labels[i]; + if (confidences[i] > rejectWeights[cls]) + rejectWeights[cls] = confidences[i]; + } + } + + // Find average rectangle for all classes + for (int i = 0; i < nClasses; i++) + { + Rect r = rrects[i]; + float s = 1.f/rweights[i]; + rrects[i] = Rect(saturate_cast(r.x*s), + saturate_cast(r.y*s), + saturate_cast(r.width*s), + saturate_cast(r.height*s)); + } + + rects.clear(); + confidences.clear(); + + // Aggregate by comparing average rectangles against other average rectangels + for (int i = 0; i < nClasses; i++) + { + // Average rectangle + Rect r1 = rrects[i]; + + // Used to eliminate rectangles with few neighbors in the case of no weights + // int n1 = levelWeights ? rejectLevels[i] : rweights[i]; + float w1 = rejectWeights[i]; + + // Eliminate rectangle if it doesn't meet confidence criteria + if (w1 <= confidenceThreshold) + continue; + + // filter out small face rectangles inside large rectangles + int j; + for (j = 0; j < nClasses; j++) + { + float w2 = rejectWeights[j]; + + if (j == i) + continue; + + Rect r2 = rrects[j]; + + int dx = saturate_cast(r2.width * epsilon); + int dy = saturate_cast(r2.height * epsilon); + + // If, r1 is within the r2 AND + // the second rectangle reaches a later stage than the first + // where both the first and the second must have a stage greater than three OR + // the first doens't reach the third stage. + // Changeto: second rectangle has a higher confidence than the first OR + // the first has a low confidence. + // Then, eliminate the first rectangle. + if(r1.x >= r2.x - dx && + r1.y >= r2.y - dy && + r1.x + r1.width <= r2.x + r2.width + dx && + r1.y + r1.height <= r2.y + r2.height + dy && + (w2 > std::max(confidenceThreshold, w1))) + break; + } + + // Need to return rects and confidences + if( j == nClasses ) + { + rects.push_back(r1); + if (useConfidences) + confidences.push_back(w1); + } + } +} + QDataStream &operator<<(QDataStream &stream, const Mat &m) { // Write header diff --git a/openbr/core/opencvutils.h b/openbr/core/opencvutils.h index 516fb1b..cd51e70 100644 --- a/openbr/core/opencvutils.h +++ b/openbr/core/opencvutils.h @@ -98,6 +98,9 @@ namespace OpenCVUtils float overlap(const cv::Rect &rect1, const cv::Rect &rect2); float overlap(const QRectF &rect1, const QRectF &rect2); + // Misc + void group(std::vector &rects, std::vector &confidences, float confidenceThreshold, float epsilon); + int getFourcc(); } diff --git a/openbr/openbr_plugin.cpp b/openbr/openbr_plugin.cpp index b9f4fb4..8c9787b 100644 --- a/openbr/openbr_plugin.cpp +++ b/openbr/openbr_plugin.cpp @@ -1475,7 +1475,7 @@ Transform *Transform::make(QString str, QObject *parent) Transform *Transform::clone() const { Transform *clone = Factory::make("."+description(false)); - return clone; + return clone; } static void _project(const Transform *transform, const Template *src, Template *dst) diff --git a/openbr/openbr_plugin.h b/openbr/openbr_plugin.h index 77dfbb1..de14060 100644 --- a/openbr/openbr_plugin.h +++ b/openbr/openbr_plugin.h @@ -1401,12 +1401,18 @@ public: virtual ~Representation() {} static Representation *make(QString str, QObject *parent); /*!< \brief Make a representation from a string. */ - virtual cv::Mat preprocess(const cv::Mat &image) const { return image; } + virtual void preprocess(const cv::Mat &src, cv::Mat &dst) const { dst = src; } virtual void train(const QList &images, const QList &labels) { (void) images; (void)labels; } + + virtual float evaluate(const cv::Mat &image, int idx) const = 0; // By convention, an empty indices list will result in all feature responses being calculated // and returned. virtual cv::Mat evaluate(const cv::Mat &image, const QList &indices = QList()) const = 0; + + virtual cv::Size windowSize(int *dx = NULL, int *dy = NULL) const = 0; // dx and dy should indicate the change to the original window size after preprocessing + virtual int numChannels() const { return 1; } virtual int numFeatures() const = 0; + virtual int maxCatCount() const = 0; }; class BR_EXPORT Classifier : public Object @@ -1417,10 +1423,15 @@ public: virtual ~Classifier() {} static Classifier *make(QString str, QObject *parent); /*!< \brief Make a classifier from a string. */ + virtual void train(const QList &images, const QList &labels) = 0; - // By convention, classify should return a value normalized such that the threshold is 0. Negative values - // can be interpreted as a negative classification and positive values as a positive classification. - virtual float classify(const cv::Mat &image) const = 0; + virtual float classify(const cv::Mat &image, bool process = true, float *confidence = NULL) const = 0; + + // Slots for representations + virtual cv::Mat preprocess(const cv::Mat &image) const = 0; + virtual cv::Size windowSize(int *dx = NULL, int *dy = NULL) const = 0; + virtual int numFeatures() const = 0; + virtual int maxCatCount() const = 0; }; /*! diff --git a/openbr/plugins/classification/boostedforest.cpp b/openbr/plugins/classification/boostedforest.cpp new file mode 100644 index 0000000..0b2e027 --- /dev/null +++ b/openbr/plugins/classification/boostedforest.cpp @@ -0,0 +1,220 @@ +#include +#include + +#define THRESHOLD_EPS 1e-5 + +using namespace cv; + +namespace br +{ + +struct Node +{ + float value; // for leaf nodes + + float threshold; // for ordered features + QList subset; // for categorical features + int featureIdx; + + Node *left, *right; +}; + +static void buildTreeRecursive(Node *node, const CvDTreeNode *cv_node, int maxCatCount) +{ + if (!cv_node->left) { + node->value = cv_node->value; + node->left = node->right = NULL; + } else { + if (maxCatCount > 0) + for (int i = 0; i < (maxCatCount + 31)/32; i++) + node->subset.append(cv_node->split->subset[i]); + else + node->threshold = cv_node->split->ord.c; + + node->featureIdx = cv_node->split->var_idx; + + node->left = new Node; node->right = new Node; + buildTreeRecursive(node->left, cv_node->left, maxCatCount); + buildTreeRecursive(node->right, cv_node->right, maxCatCount); + } +} + +static void loadRecursive(QDataStream &stream, Node *node, int maxCatCount) +{ + bool hasChildren; stream >> hasChildren; + + if (!hasChildren) { + stream >> node->value; + node->left = node->right = NULL; + } else { + if (maxCatCount > 0) + for (int i = 0; i < (maxCatCount + 31)/32; i++) { + int s; stream >> s; node->subset.append(s); + } + else + stream >> node->threshold; + + stream >> node->featureIdx; + + node->left = new Node; node->right = new Node; + loadRecursive(stream, node->left, maxCatCount); + loadRecursive(stream, node->right, maxCatCount); + } +} + +static void storeRecursive(QDataStream &stream, const Node *node, int maxCatCount) +{ + bool hasChildren = node->left ? true : false; + stream << hasChildren; + + if (!hasChildren) + stream << node->value; + else { + if (maxCatCount > 0) + for (int i = 0; i < (maxCatCount + 31)/32; i++) + stream << node->subset[i]; + else + stream << node->threshold; + + stream << node->featureIdx; + + storeRecursive(stream, node->left, maxCatCount); + storeRecursive(stream, node->right, maxCatCount); + } +} + +class BoostedForestClassifier : public Classifier +{ + Q_OBJECT + Q_ENUMS(Type) + + Q_PROPERTY(br::Representation* representation READ get_representation WRITE set_representation RESET reset_representation STORED false) + Q_PROPERTY(float minTAR READ get_minTAR WRITE set_minTAR RESET reset_minTAR STORED false) + Q_PROPERTY(float maxFAR READ get_maxFAR WRITE set_maxFAR RESET reset_maxFAR STORED false) + Q_PROPERTY(float trimRate READ get_trimRate WRITE set_trimRate RESET reset_trimRate STORED false) + Q_PROPERTY(int maxDepth READ get_maxDepth WRITE set_maxDepth RESET reset_maxDepth STORED false) + Q_PROPERTY(int maxWeakCount READ get_maxWeakCount WRITE set_maxWeakCount RESET reset_maxWeakCount STORED false) + Q_PROPERTY(Type type READ get_type WRITE set_type RESET reset_type STORED false) + +public: + enum Type { Discrete = CvBoost::DISCRETE, + Real = CvBoost::REAL, + Logit = CvBoost::LOGIT, + Gentle = CvBoost::GENTLE}; +private: + BR_PROPERTY(br::Representation*, representation, NULL) + BR_PROPERTY(float, minTAR, 0.995) + BR_PROPERTY(float, maxFAR, 0.5) + BR_PROPERTY(float, trimRate, 0.95) + BR_PROPERTY(int, maxDepth, 1) + BR_PROPERTY(int, maxWeakCount, 100) + BR_PROPERTY(Type, type, Gentle) + + QList classifiers; + float threshold; + + void train(const QList &images, const QList &labels) + { + representation->train(images, labels); + + CascadeBoostParams params(type, minTAR, maxFAR, trimRate, maxDepth, maxWeakCount); + + FeatureEvaluator featureEvaluator; + featureEvaluator.init(representation, images.size(), representation->numChannels()); + + for (int i = 0; i < images.size(); i++) + featureEvaluator.setImage(images[i], labels[i], i); + + CascadeBoost boost; + boost.train(&featureEvaluator, images.size(), 1024, 1024, representation->numChannels(), params); + + threshold = boost.getThreshold(); + + foreach (const CvBoostTree *classifier, boost.getClassifers()) { + Node *root = new Node; + buildTreeRecursive(root, classifier->get_root(), representation->maxCatCount()); + classifiers.append(root); + } + } + + float classify(const Mat &image, bool process, float *confidence) const + { + Mat m; + if (process) + m = preprocess(image); + else + m = image; + + float sum = 0; + for (int i = 0; i < classifiers.size(); i++) { + Node *node = classifiers[i]; + + while (node->left) { + if (representation->maxCatCount() > 0) { + int c = (int)representation->evaluate(m, node->featureIdx); + node = (node->subset[c >> 5] & (1 << (c & 31))) ? node->left : node->right; + } else { + double val = representation->evaluate(m, node->featureIdx); + node = val <= node->threshold ? node->left : node->right; + } + } + + sum += node->value; + } + + if (confidence) + *confidence = sum; + return sum < threshold - THRESHOLD_EPS ? 0.0f : 1.0f; + } + + int numFeatures() const + { + return representation->numFeatures(); + } + + int maxCatCount() const + { + return representation->maxCatCount(); + } + + Mat preprocess(const Mat &image) const + { + Mat dst; + representation->preprocess(image, dst); + return dst; + } + + Size windowSize(int *dx, int *dy) const + { + return representation->windowSize(dx, dy); + } + + void load(QDataStream &stream) + { + representation->load(stream); + + stream >> threshold; + int numClassifiers; stream >> numClassifiers; + for (int i = 0; i < numClassifiers; i++) { + Node *classifier = new Node; + loadRecursive(stream, classifier, representation->maxCatCount()); + classifiers.append(classifier); + } + } + + void store(QDataStream &stream) const + { + representation->store(stream); + + stream << threshold; + stream << classifiers.size(); + foreach (const Node *classifier, classifiers) + storeRecursive(stream, classifier, representation->maxCatCount()); + } +}; + +BR_REGISTER(Classifier, BoostedForestClassifier) + +} // namespace br + +#include "classification/boostedforest.moc" diff --git a/openbr/plugins/classification/cascade.cpp b/openbr/plugins/classification/cascade.cpp new file mode 100644 index 0000000..d9cafc9 --- /dev/null +++ b/openbr/plugins/classification/cascade.cpp @@ -0,0 +1,251 @@ +#include + +#include + +using namespace cv; + +namespace br +{ + +struct ImageHandler +{ + bool create(const QList &_posImages, const QList &_negImages, Size _winSize) + { + posImages = _posImages; + negImages = _negImages; + winSize = _winSize; + + posIdx = negIdx = 0; + + src.create( 0, 0 , CV_8UC1 ); + img.create( 0, 0, CV_8UC1 ); + point = offset = Point( 0, 0 ); + scale = 1.0F; + scaleFactor = 1.4142135623730950488016887242097F; + stepFactor = 0.5F; + round = 0; + + return true; + } + + void restart() { posIdx = 0; } + + void nextNeg() + { + int count = negImages.size(); + for (int i = 0; i < count; i++) { + src = negImages[negIdx++]; + + round += negIdx / count; + round = round % (winSize.width * winSize.height); + negIdx %= count; + + offset.x = qMin( (int)round % winSize.width, src.cols - winSize.width ); + offset.y = qMin( (int)round / winSize.width, src.rows - winSize.height ); + if (!src.empty() && src.type() == CV_8UC1 && offset.x >= 0 && offset.y >= 0) + break; + } + + point = offset; + scale = max(((float)winSize.width + point.x) / ((float)src.cols), + ((float)winSize.height + point.y) / ((float)src.rows)); + + Size sz((int)(scale*src.cols + 0.5F), (int)(scale*src.rows + 0.5F)); + resize(src, img, sz); + } + + bool getNeg(Mat &_img) + { + if (img.empty()) + nextNeg(); + + Mat m(winSize.height, winSize.width, CV_8UC1, (void*)(img.data + point.y * img.step + point.x * img.elemSize()), img.step); + m.copyTo(_img); + + if ((int)(point.x + (1.0F + stepFactor) * winSize.width) < img.cols) + point.x += (int)(stepFactor * winSize.width); + else { + point.x = offset.x; + if ((int)( point.y + (1.0F + stepFactor ) * winSize.height ) < img.rows) + point.y += (int)(stepFactor * winSize.height); + else { + point.y = offset.y; + scale *= scaleFactor; + if (scale <= 1.0F) + resize(src, img, Size((int)(scale*src.cols), (int)(scale*src.rows))); + else + nextNeg(); + } + } + return true; + } + + bool getPos(Mat &_img) + { + if (posIdx >= posImages.size()) + return false; + + posImages[posIdx++].copyTo(_img); + return true; + } + + QList posImages, negImages; + + int posIdx, negIdx; + + Mat src, img; + Point offset, point; + float scale; + float scaleFactor; + float stepFactor; + size_t round; + Size winSize; +}; + +class CascadeClassifier : public Classifier +{ + Q_OBJECT + + Q_PROPERTY(QString stageDescription READ get_stageDescription WRITE set_stageDescription RESET reset_stageDescription STORED false) + Q_PROPERTY(int numStages READ get_numStages WRITE set_numStages RESET reset_numStages STORED false) + Q_PROPERTY(int numPos READ get_numPos WRITE set_numPos RESET reset_numPos STORED false) + Q_PROPERTY(int numNegs READ get_numNegs WRITE set_numNegs RESET reset_numNegs STORED false) + Q_PROPERTY(float maxFAR READ get_maxFAR WRITE set_maxFAR RESET reset_maxFAR STORED false) + + BR_PROPERTY(QString, stageDescription, "") + BR_PROPERTY(int, numStages, 20) + BR_PROPERTY(int, numPos, 1000) + BR_PROPERTY(int, numNegs, 1000) + BR_PROPERTY(float, maxFAR, pow(0.5, numStages)) + + QList stages; + + void train(const QList &images, const QList &labels) + { + QList posImages, negImages; + for (int i = 0; i < images.size(); i++) + labels[i] == 1 ? posImages.append(images[i]) : negImages.append(images[i]); + + ImageHandler imgHandler; + imgHandler.create(posImages, negImages, Size(24, 24)); + + stages.reserve(numStages); + for (int i = 0; i < numStages; i++) { + qDebug() << "===== TRAINING" << i << "stage ====="; + qDebug() << " trainingImages; + QList trainingLabels; + + float currFAR = fillTrainingSet(imgHandler, trainingImages, trainingLabels); + + if (currFAR < maxFAR) { + qDebug() << "FAR is below required level! Terminating early"; + return; + } + + Classifier *next_stage = Classifier::make(stageDescription, NULL); + next_stage->train(trainingImages, trainingLabels); + stages.append(next_stage); + + qDebug() << "END>"; + } + } + + float classify(const Mat &image, bool process, float *confidence) const + { + float stageConf = 0.0f; + foreach (const Classifier *stage, stages) { + float result = stage->classify(image, process, &stageConf); + if (confidence) + *confidence += stageConf; + if (result == 0.0f) + return 0.0f; + } + return 1.0f; + } + + int numFeatures() const + { + return stages.first()->numFeatures(); + } + + int maxCatCount() const + { + return stages.first()->maxCatCount(); + } + + Mat preprocess(const Mat &image) const + { + return stages.first()->preprocess(image); + } + + Size windowSize(int *dx, int *dy) const + { + return stages.first()->windowSize(dx, dy); + } + + void load(QDataStream &stream) + { + int numStages; stream >> numStages; + for (int i = 0; i < numStages; i++) { + Classifier *nextStage = Classifier::make(stageDescription, NULL); + nextStage->load(stream); + stages.append(nextStage); + } + } + + void store(QDataStream &stream) const + { + stream << stages.size(); + foreach (const Classifier *stage, stages) + stage->store(stream); + } + +private: + float fillTrainingSet(ImageHandler &imgHandler, QList &images, QList &labels) + { + imgHandler.restart(); + + float confidence = 0.0f; + + while (images.size() < numPos) { + Mat pos(imgHandler.winSize, CV_8UC1); + if (!imgHandler.getPos(pos)) + qFatal("Cannot get another positive sample!"); + + if (classify(pos, true, &confidence) > 0.0f) { + printf("POS current samples: %d\r", images.size()); + images.append(pos); + labels.append(1.0f); + } + } + + int posCount = images.size(); + qDebug() << "POS count : consumed " << posCount << ":" << imgHandler.posIdx; + + int passedNegs = 0; + while ((images.size() - posCount) < numNegs) { + Mat neg(imgHandler.winSize, CV_8UC1); + if (!imgHandler.getNeg(neg)) + qFatal("Cannot get another negative sample!"); + + if (classify(neg, true, &confidence) > 0.0f) { + printf("NEG current samples: %d\r", images.size() - posCount); + images.append(neg); + labels.append(0.0f); + } + passedNegs++; + } + + double acceptanceRatio = (images.size() - posCount) / (double)passedNegs; + qDebug() << "NEG count : acceptanceRatio " << images.size() - posCount << ":" << acceptanceRatio; + return acceptanceRatio; + } +}; + +BR_REGISTER(Classifier, CascadeClassifier) + +} // namespace br + +#include "classification/cascade.moc" diff --git a/openbr/plugins/gallery/vec.cpp b/openbr/plugins/gallery/vec.cpp new file mode 100644 index 0000000..f69a414 --- /dev/null +++ b/openbr/plugins/gallery/vec.cpp @@ -0,0 +1,116 @@ +#include + +namespace br +{ + +/*! + * \ingroup galleries + * \brief Reads/writes OpenCV's .vec format. + * \author Scott Klum \cite sklum + */ + +class vecGallery : public FileGallery +{ + Q_OBJECT + + Q_PROPERTY(int width READ get_width WRITE set_width RESET reset_width STORED false) + Q_PROPERTY(int height READ get_height WRITE set_height RESET reset_height STORED false) + BR_PROPERTY(int, width, 24) + BR_PROPERTY(int, height, 24) + + QList mats; + + ~vecGallery() + { + if (mats.isEmpty()) + return; + + writeOpen(); + + // Write header + int count = mats.size(); + int size = width*height; + short temp = 0; + + const size_t write1 = f.write((char*)&count,sizeof(count)); + const size_t write2 = f.write((char*)&size,sizeof(size)); + const size_t write3 = f.write((char*)&temp,sizeof(temp)); + const size_t write4 = f.write((char*)&temp,sizeof(temp)); + + if (write1 != sizeof(count) || write2 != sizeof(size) || write3 != sizeof(temp) || write4 != sizeof(temp)) + qFatal("Failed to write header."); + + for (int i=0; i affineParams; for (int i = 0 ; i < 2; i++) diff --git a/openbr/plugins/imgproc/resizefilter.cpp b/openbr/plugins/imgproc/resizefilter.cpp new file mode 100644 index 0000000..ab3345f --- /dev/null +++ b/openbr/plugins/imgproc/resizefilter.cpp @@ -0,0 +1,69 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * Copyright 2012 The MITRE Corporation * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); * + * you may not use this file except in compliance with the License. * + * You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include + +#include + +using namespace cv; + +namespace br +{ + +/*! + * \ingroup transforms + * \brief Resize the template depending on its metadata + * \author Jordan Cheney \cite JordanCheney + * \note Method: Area should be used for shrinking an image, Cubic for slow but accurate enlargment, Bilin for fast enlargement. + */ +class ResizeFilterTransform : public UntrainableTransform +{ + Q_OBJECT + Q_ENUMS(Method) + +public: + /*!< */ + enum Method { Near = INTER_NEAREST, + Area = INTER_AREA, + Bilin = INTER_LINEAR, + Cubic = INTER_CUBIC, + Lanczo = INTER_LANCZOS4}; + +private: + Q_PROPERTY(int rows READ get_rows WRITE set_rows RESET reset_rows STORED false) + Q_PROPERTY(int columns READ get_columns WRITE set_columns RESET reset_columns STORED false) + Q_PROPERTY(Method method READ get_method WRITE set_method RESET reset_method STORED false) + Q_PROPERTY(QString filterKey READ get_filterKey WRITE set_filterKey RESET reset_filterKey STORED false) + Q_PROPERTY(QString filterVal READ get_filterVal WRITE set_filterVal RESET reset_filterVal STORED false) + BR_PROPERTY(int, rows, -1) + BR_PROPERTY(int, columns, -1) + BR_PROPERTY(Method, method, Bilin) + BR_PROPERTY(QString, filterKey, "Label") + BR_PROPERTY(QString, filterVal, "1.0") + + void project(const Template &src, Template &dst) const + { + dst = src; + if (src.file.get(filterKey) == filterVal) + resize(src, dst, Size((columns == -1) ? src.m().cols*rows/src.m().rows : columns, rows), 0, 0, method); + } +}; + +BR_REGISTER(Transform, ResizeFilterTransform) + +} // namespace br + +#include "imgproc/resizefilter.moc" diff --git a/openbr/plugins/imgproc/rndaffine.cpp b/openbr/plugins/imgproc/rndaffine.cpp new file mode 100644 index 0000000..dff0d08 --- /dev/null +++ b/openbr/plugins/imgproc/rndaffine.cpp @@ -0,0 +1,70 @@ +#include + +#include +#include + +using namespace cv; + +namespace br +{ + +class RndAffineTransform : public UntrainableMetaTransform +{ + Q_OBJECT + Q_PROPERTY(int numAffines READ get_numAffines WRITE set_numAffines RESET reset_numAffines STORED false) + Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false) + Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false) + Q_PROPERTY(float scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false) + Q_PROPERTY(int maxAngle READ get_maxAngle WRITE set_maxAngle RESET reset_maxAngle STORED false) + BR_PROPERTY(int, numAffines, 0) + BR_PROPERTY(int, winWidth, 24) + BR_PROPERTY(int, winHeight, 24) + BR_PROPERTY(float, scaleFactor, 1.2) + BR_PROPERTY(int, maxAngle, 15) + + void project(const Template &src, Template &dst) const + { + TemplateList temp; + project(TemplateList() << src, temp); + if (!temp.isEmpty()) dst = temp.first(); + } + + void project(const TemplateList &src, TemplateList &dst) const + { + foreach (const Template &t, src) { + QPointF affine_0 = t.file.get("Affine_0"); + QPointF affine_1 = t.file.get("Affine_1"); + + // Append the original points + Template u = t; + u.file.setPoints(QList() << affine_0 << affine_1); + u.file.set("Affine_0", affine_0); + u.file.set("Affine_1", affine_1); + dst.append(u); + + const double IPD = sqrt(pow(affine_0.x() - affine_1.x(), 2) + pow(affine_0.y() - affine_1.y(), 2)); + for (int i = 0; i < numAffines; i++) { + int angle = (rand() % (2*maxAngle)) - maxAngle; + + int min = (int)(sqrt(1 / scaleFactor) * IPD); + int max = (int)(sqrt(scaleFactor) * IPD); + int dx = (rand() % (max - min)) + min; + int dy = (dx * sin(angle * M_PI / 180))/2; + + QPointF shiftedAffine_0 = QPointF(affine_1.x() - dx, affine_1.y() + dy); + + Template u = t; + u.file.setPoints(QList() << shiftedAffine_0 << affine_1); + u.file.set("Affine_0", shiftedAffine_0); + u.file.set("Affine_1", affine_1); + dst.append(u); + } + } + } +}; + +BR_REGISTER(Transform, RndAffineTransform) + +} // namespace br + +#include "imgproc/rndaffine.moc" diff --git a/openbr/plugins/imgproc/slidingwindow.cpp b/openbr/plugins/imgproc/slidingwindow.cpp index f3538b9..22845af 100644 --- a/openbr/plugins/imgproc/slidingwindow.cpp +++ b/openbr/plugins/imgproc/slidingwindow.cpp @@ -16,150 +16,150 @@ #include #include +#include + +#include using namespace cv; namespace br { -// Find avg aspect ratio -static float getAspectRatio(const TemplateList &data) -{ - double tempRatio = 0; - int ratioCnt = 0; - - foreach (const Template &tmpl, data) { - QList posRects = OpenCVUtils::toRects(tmpl.file.rects()); - foreach (const Rect &posRect, posRects) { - if (posRect.x + posRect.width >= tmpl.m().cols || posRect.y + posRect.height >= tmpl.m().rows || posRect.x < 0 || posRect.y < 0) { - continue; - } - tempRatio += (float)posRect.width / (float)posRect.height; - ratioCnt += 1; - } - } - return tempRatio / (double)ratioCnt; -} - /*! * \ingroup transforms - * \brief Applies a transform to a sliding window. - * Discards negative detections. - * \author Austin Blanton \cite imaus10 + * \brief Sliding Window Framework + * \author Jordan Cheney */ -class SlidingWindowTransform : public Transform + +class SlidingWindowTransform : public MetaTransform { Q_OBJECT - Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false) - Q_PROPERTY(int windowWidth READ get_windowWidth WRITE set_windowWidth RESET reset_windowWidth STORED false) - Q_PROPERTY(bool takeFirst READ get_takeFirst WRITE set_takeFirst RESET reset_takeFirst STORED false) - Q_PROPERTY(float threshold READ get_threshold WRITE set_threshold RESET reset_threshold STORED false) - Q_PROPERTY(float stepFraction READ get_stepFraction WRITE set_stepFraction RESET reset_stepFraction STORED false) - Q_PROPERTY(int ignoreBorder READ get_ignoreBorder WRITE set_ignoreBorder RESET reset_ignoreBorder STORED true) - BR_PROPERTY(br::Transform *, transform, NULL) - BR_PROPERTY(int, windowWidth, 24) - BR_PROPERTY(bool, takeFirst, false) - BR_PROPERTY(float, threshold, 0) - BR_PROPERTY(float, stepFraction, 0.25) - BR_PROPERTY(int, ignoreBorder, 0) - -private: - int windowHeight; - bool skipProject; + + Q_PROPERTY(br::Classifier* classifier READ get_classifier WRITE set_classifier RESET reset_classifier STORED false) + + Q_PROPERTY(int minSize READ get_minSize WRITE set_minSize RESET reset_minSize STORED false) + Q_PROPERTY(int maxSize READ get_maxSize WRITE set_maxSize RESET reset_maxSize STORED false) + Q_PROPERTY(float scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false) + Q_PROPERTY(int minNeighbors READ get_minNeighbors WRITE set_minNeighbors RESET reset_minNeighbors STORED false) + Q_PROPERTY(float confidenceThreshold READ get_confidenceThreshold WRITE set_confidenceThreshold RESET reset_confidenceThreshold STORED false) + Q_PROPERTY(float eps READ get_eps WRITE set_eps RESET reset_eps STORED false) + + BR_PROPERTY(br::Classifier*, classifier, NULL) + BR_PROPERTY(int, minSize, 20) + BR_PROPERTY(int, maxSize, -1) + BR_PROPERTY(float, scaleFactor, 1.2) + BR_PROPERTY(int, minNeighbors, 5) + BR_PROPERTY(float, confidenceThreshold, 10) + BR_PROPERTY(float, eps, 0.2) void train(const TemplateList &data) { - skipProject = true; - float aspectRatio = data.first().file.get("aspectRatio", -1); - if (aspectRatio == -1) - aspectRatio = getAspectRatio(data); - windowHeight = qRound(windowWidth / aspectRatio); - - if (transform->trainable) { - TemplateList dataOut = data; - if (ignoreBorder > 0) { - for (int i = 0; i < dataOut.size(); i++) { - Template t = dataOut[i]; - Mat m = t.m(); - dataOut.replace(i,Template(t.file, Mat(m,Rect(ignoreBorder,ignoreBorder,m.cols - ignoreBorder * 2, m.rows - ignoreBorder * 2)))); - } - } - transform->train(dataOut); - } + classifier->train(data.data(), File::get(data, "Label", -1)); } - void store(QDataStream &stream) const + void project(const Template &src, Template &dst) const { - transform->store(stream); - stream << windowHeight; + TemplateList temp; + project(TemplateList() << src, temp); + if (!temp.isEmpty()) dst = temp.first(); } - void load(QDataStream &stream) + void project(const TemplateList &src, TemplateList &dst) const { - transform->load(stream); - stream >> windowHeight; - } + Size minObjectSize(minSize, minSize); + Size maxObjectSize; - void project(const Template &src, Template &dst) const - { - float scale = src.file.get("scale", 1); - projectHelp(src, dst, windowWidth, windowHeight, scale); - } + foreach (const Template &t, src) { + const bool enrollAll = t.file.getBool("enrollAll"); + + // Mirror the behavior of ExpandTransform in the special case + // of an empty template. + if (t.empty() && !enrollAll) { + dst.append(t); + continue; + } - protected: - void projectHelp(const Template &src, Template &dst, int windowWidth, int windowHeight, float scale = 1) const - { + for (int i=0; i rects; + std::vector confidences; - dst = src; - if (skipProject) { - dst = src; - return; - } + if (maxObjectSize.height == 0 || maxObjectSize.width == 0) + maxObjectSize = m.size(); + + Mat imageBuffer(m.rows + 1, m.cols + 1, CV_8U); - Template windowTemplate(src.file, src); - QList confidences = dst.file.getList("Confidences", QList()); - for (float y = 0; y + windowHeight < src.m().rows; y += windowHeight*stepFraction) { - for (float x = 0; x + windowWidth < src.m().cols; x += windowWidth*stepFraction) { - Mat windowMat(src, Rect(x + ignoreBorder, y + ignoreBorder, windowWidth - ignoreBorder * 2, windowHeight - ignoreBorder * 2)); - windowTemplate.replace(0,windowMat); - Template detect; - transform->project(windowTemplate, detect); - float conf = detect.m().at(0); - - // the result will be in the Label - if (conf > threshold) { - dst.file.appendRect(QRectF(x*scale, y*scale, windowWidth*scale, windowHeight*scale)); - confidences.append(conf); - if (takeFirst) - return; + for (double factor = 1; ; factor *= scaleFactor) { + int dx, dy; + Size originalWindowSize = classifier->windowSize(&dx, &dy); + + Size windowSize(cvRound(originalWindowSize.width*factor), cvRound(originalWindowSize.height*factor) ); + Size scaledImageSize(cvRound(m.cols/factor ), cvRound(m.rows/factor)); + Size processingRectSize(scaledImageSize.width - originalWindowSize.width, scaledImageSize.height - originalWindowSize.height); + + if (processingRectSize.width <= 0 || processingRectSize.height <= 0) + break; + if (windowSize.width > maxObjectSize.width || windowSize.height > maxObjectSize.height) + break; + if (windowSize.width < minObjectSize.width || windowSize.height < minObjectSize.height) + continue; + + Mat scaledImage(scaledImageSize, CV_8U, imageBuffer.data); + resize(m, scaledImage, scaledImageSize, 0, 0, CV_INTER_LINEAR); + + Mat repImage = classifier->preprocess(scaledImage); + + int step = factor > 2. ? 1 : 2; + for (int y = 0; y < processingRectSize.height; y += step) { + for (int x = 0; x < processingRectSize.width; x += step) { + Mat window = repImage(Rect(Point(x, y), Size(originalWindowSize.width + dx, originalWindowSize.height + dy))).clone(); + + float confidence = 0; + int result = classifier->classify(window, false, &confidence); + + if (result == 1) { + rects.push_back(Rect(cvRound(x*factor), cvRound(y*factor), windowSize.width, windowSize.height)); + confidences.push_back(confidence); + } + + // TODO: Add non ROC mode + + if (result == 0) + x += step; + } + } + } + + OpenCVUtils::group(rects, confidences, confidenceThreshold, eps); + + if (!enrollAll && rects.empty()) + rects.push_back(Rect(0, 0, m.cols, m.rows)); + + for (size_t j=0; j("Confidences", confidences); } -}; -BR_REGISTER(Transform, SlidingWindowTransform) - -/*! - * \ingroup transforms - * \brief Overloads SlidingWindowTransform for integral images that should be - * sampled at multiple scales. - * \author Josh Klontz \cite jklontz - */ -class IntegralSlidingWindowTransform : public SlidingWindowTransform -{ - Q_OBJECT + void load(QDataStream &stream) + { + classifier->load(stream); + } - private: - void project(const Template &src, Template &dst) const + void store(QDataStream &stream) const { - // TODO: call SlidingWindowTransform::project on multiple scales - SlidingWindowTransform::projectHelp(src, dst, 24, 24); + classifier->store(stream); } }; -BR_REGISTER(Transform, IntegralSlidingWindowTransform) +BR_REGISTER(Transform, SlidingWindowTransform) } // namespace br diff --git a/openbr/plugins/io/read.cpp b/openbr/plugins/io/read.cpp index 1450c55..422de4a 100644 --- a/openbr/plugins/io/read.cpp +++ b/openbr/plugins/io/read.cpp @@ -15,7 +15,7 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include - +#include #include using namespace cv; @@ -59,9 +59,13 @@ private: else dst.file.fte = true; } else { foreach (const Mat &m, src) { - const Mat img = imdecode(m, mode); - if (img.data) dst.append(img); - else dst.file.fte = true; + if (((m.rows > 1) && (m.cols > 1)) || (m.type() != CV_8UC1)) + dst += m; + else { + const Mat img = imdecode(m, mode); + if (img.data) dst.append(img); + else dst.file.fte = true; + } } } if (dst.file.fte) diff --git a/openbr/plugins/io/write.cpp b/openbr/plugins/io/write.cpp index b030d36..e399677 100644 --- a/openbr/plugins/io/write.cpp +++ b/openbr/plugins/io/write.cpp @@ -31,10 +31,10 @@ class WriteTransform : public TimeVaryingTransform { Q_OBJECT Q_PROPERTY(QString outputDirectory READ get_outputDirectory WRITE set_outputDirectory RESET reset_outputDirectory STORED false) - Q_PROPERTY(QString imageName READ get_imageName WRITE set_imageName RESET reset_imageName STORED false) + Q_PROPERTY(QString underscore READ get_underscore WRITE set_underscore RESET reset_underscore STORED false) Q_PROPERTY(QString imgExtension READ get_imgExtension WRITE set_imgExtension RESET reset_imgExtension STORED false) BR_PROPERTY(QString, outputDirectory, "Temp") - BR_PROPERTY(QString, imageName, "image") + BR_PROPERTY(QString, underscore, "") BR_PROPERTY(QString, imgExtension, "jpg") int cnt; @@ -48,7 +48,8 @@ class WriteTransform : public TimeVaryingTransform void projectUpdate(const Template &src, Template &dst) { dst = src; - OpenCVUtils::saveImage(dst.m(), QString("%1/%2_%3.%4").arg(outputDirectory).arg(imageName).arg(cnt++, 5, 10, QChar('0')).arg(imgExtension)); + QString path = QString("%1/image%2%3.%4").arg(outputDirectory).arg(cnt++, 5, 10, QChar('0')).arg(underscore.isEmpty() ? "" : "_" + underscore).arg(imgExtension); + OpenCVUtils::saveImage(dst.m(), path); } }; diff --git a/openbr/plugins/metadata/registerpointsasaffine.cpp b/openbr/plugins/metadata/registerpointsasaffine.cpp new file mode 100644 index 0000000..18338aa --- /dev/null +++ b/openbr/plugins/metadata/registerpointsasaffine.cpp @@ -0,0 +1,41 @@ +#include + +namespace br +{ + +class RegisterPointsAsAffine : public UntrainableMetadataTransform +{ + Q_OBJECT + Q_PROPERTY(QList pointsIdxs READ get_pointIdxs WRITE set_pointIdxs RESET reset_pointIdxs STORED false) + BR_PROPERTY(QList, pointIdxs, QList()) + + void projectMetadata(const File &src, File &dst) const + { + const int chin = 20; + + if (pointIdxs.size() != 2 && pointIdxs.size() != 3) + qFatal("Need 2 or 3 points for affine transform"); + + dst = src; + + QList points = src.points(); + + if (points[pointIdxs[0]] == QPointF(-1, -1) || + points[pointIdxs[1]] == QPointF(-1, -1)) + dst.fte = true; + + if (points[chin] == QPointF(-1, -1)) + dst.fte = true; + + dst.set("Affine_0", points[pointIdxs[0]]); + dst.set("Affine_1", points[pointIdxs[1]]); + if (pointIdxs.size() == 3) + dst.set("Affine_2", points[pointIdxs[2]]); + } +}; + +BR_REGISTER(Transform, RegisterPointsAsAffine) + +} // namespace br + +#include "metadata/registerpointsasaffine.moc" diff --git a/openbr/plugins/representation/gradienthistogram.cpp b/openbr/plugins/representation/gradienthistogram.cpp new file mode 100644 index 0000000..91a5270 --- /dev/null +++ b/openbr/plugins/representation/gradienthistogram.cpp @@ -0,0 +1,136 @@ +#include +#include + +#include +#include + +using namespace cv; + +namespace br +{ + +/*! + * \ingroup galleries + * \brief Computes first order gradient histogram features using an integral image + * \author Scott Klum \cite sklum + */ +class GradientHistogramRepresentation : public Representation +{ + Q_OBJECT + + Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false) + Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false) + Q_PROPERTY(int bins READ get_bins WRITE set_bins RESET reset_bins STORED false) + BR_PROPERTY(int, winWidth, 24) + BR_PROPERTY(int, winHeight, 24) + BR_PROPERTY(int, bins, 6) + + void init() + { + int dx, dy; + Size size = windowSize(&dx,&dy); + + int width = size.width+dx, height = size.height+dy; + + // Enumerate all possible rectangles + for (int x=0; x 256 ? CV_16U : CV_8U, bins/360., floor); + + // Mask and compute integral image + std::vector outputs; + for (int i=0; i(0)[((features[index].y+features[index].height)*(size.height+dy)+(features[index].x+features[index].width))*bins+channel]; + int one = image.ptr(0)[(features[index].y*(size.height+dy)+features[index].x)*bins+channel]; + int two = image.ptr(0)[(features[index].y*(size.height+dy)+(features[index].x+features[index].width))*bins+channel]; + int three = image.ptr(0)[(features[index].y+features[index].height*(size.height+dy)+features[index].x)*bins+channel]; + + return four + one - (two + three); + } + + Mat evaluate(const Mat &image, const QList &indices) const + { + int size = indices.empty() ? numFeatures() : indices.size(); + + Mat result(1, size, CV_32FC1); + for (int i = 0; i < size; i++) + result.at(i) = evaluate(image, indices.empty() ? i : indices[i]); + return result; + } + + int numFeatures() const + { + return features.size()*bins; + } + + int numChannels() const + { + return bins; + } + + Size windowSize(int *dx, int *dy) const + { + if (dx && dy) + *dx = *dy = 1; + return Size(winWidth, winHeight); + } + + int maxCatCount() const { return 0; } + + QList features; +}; + +BR_REGISTER(Representation, GradientHistogramRepresentation) + +} // namespace br + +#include "representation/gradienthistogram.moc" + + diff --git a/openbr/plugins/representation/haar.cpp b/openbr/plugins/representation/haar.cpp new file mode 100644 index 0000000..3d38b6f --- /dev/null +++ b/openbr/plugins/representation/haar.cpp @@ -0,0 +1,178 @@ +#include + +#include +#include + +using namespace cv; + +namespace br +{ + +#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step ) \ + /* (x, y) */ \ + (p0) = (rect).x + (step) * (rect).y; \ + /* (x + w, y) */ \ + (p1) = (rect).x + (rect).width + (step) * (rect).y; \ + /* (x + w, y) */ \ + (p2) = (rect).x + (step) * ((rect).y + (rect).height); \ + /* (x + w, y + h) */ \ + (p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height); + +class HaarRepresentation : public Representation +{ + Q_OBJECT + + Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false) + Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false) + BR_PROPERTY(int, winWidth, 24) + BR_PROPERTY(int, winHeight, 24) + + void init() + { + int offset = winWidth + 1; + for (int x = 0; x < winWidth; x++) { + for (int y = 0; y < winHeight; y++) { + for (int dx = 1; dx <= winWidth; dx++) { + for (int dy = 1; dy <= winHeight; dy++) { + // haar_x2 + if ((x+dx*2 <= winWidth) && (y+dy <= winHeight)) + features.append(Feature(offset, + x, y, dx*2, dy, -1, + x+dx, y, dx , dy, +2)); + // haar_y2 + if ((x+dx <= winWidth) && (y+dy*2 <= winHeight)) + features.append(Feature(offset, + x, y, dx, dy*2, -1, + x, y+dy, dx, dy, +2)); + // haar_x3 + if ((x+dx*3 <= winWidth) && (y+dy <= winHeight)) + features.append(Feature(offset, + x, y, dx*3, dy, -1, + x+dx, y, dx , dy, +3)); + // haar_y3 + if ((x+dx <= winWidth) && (y+dy*3 <= winHeight)) + features.append(Feature(offset, + x, y, dx, dy*3, -1, + x, y+dy, dx, dy, +3)); + // x2_y2 + if ((x+dx*2 <= winWidth) && (y+dy*2 <= winHeight)) + features.append(Feature(offset, + x, y, dx*2, dy*2, -1, + x, y, dx, dy, +2, + x+dx, y+dy, dx, dy, +2)); + + + } + } + } + } + } + + void preprocess(const Mat &src, Mat &dst) const + { + integral(src, dst); + } + + float evaluate(const Mat &image, int idx) const + { + return (float)features[idx].calc(image); + } + + Mat evaluate(const Mat &image, const QList &indices) const + { + int size = indices.empty() ? numFeatures() : indices.size(); + + Mat result(1, size, CV_32FC1); + for (int i = 0; i < size; i++) + result.at(i) = evaluate(image, indices.empty() ? i : indices[i]); + return result; + } + + int numFeatures() const + { + return features.size(); + } + + Size windowSize(int *dx, int *dy) const + { + if (dx && dy) + *dx = *dy = 1; + return Size(winWidth, winHeight); + } + + int maxCatCount() const { return 0; } + + struct Feature + { + Feature(); + Feature( int offset, + int x0, int y0, int w0, int h0, float wt0, + int x1, int y1, int w1, int h1, float wt1, + int x2 = 0, int y2 = 0, int w2 = 0, int h2 = 0, float wt2 = 0.0F ); + float calc(const Mat &img) const; + + struct { + Rect r; + float weight; + } rect[3]; + + struct { + int p0, p1, p2, p3; + } fastRect[3]; + }; + + QList features; +}; + +BR_REGISTER(Representation, HaarRepresentation) + +HaarRepresentation::Feature::Feature() +{ + rect[0].r = rect[1].r = rect[2].r = Rect(0,0,0,0); + rect[0].weight = rect[1].weight = rect[2].weight = 0; +} + +HaarRepresentation::Feature::Feature(int offset, + int x0, int y0, int w0, int h0, float wt0, + int x1, int y1, int w1, int h1, float wt1, + int x2, int y2, int w2, int h2, float wt2) +{ + rect[0].r.x = x0; + rect[0].r.y = y0; + rect[0].r.width = w0; + rect[0].r.height = h0; + rect[0].weight = wt0; + + rect[1].r.x = x1; + rect[1].r.y = y1; + rect[1].r.width = w1; + rect[1].r.height = h1; + rect[1].weight = wt1; + + rect[2].r.x = x2; + rect[2].r.y = y2; + rect[2].r.width = w2; + rect[2].r.height = h2; + rect[2].weight = wt2; + + for (int j = 0; j < 3; j++) { + if( rect[j].weight == 0.0F ) + break; + CV_SUM_OFFSETS(fastRect[j].p0, fastRect[j].p1, fastRect[j].p2, fastRect[j].p3, rect[j].r, offset) + } +} + +inline float HaarRepresentation::Feature::calc(const Mat &img) const +{ + const int* ptr = img.ptr(); + float ret = rect[0].weight * (ptr[fastRect[0].p0] - ptr[fastRect[0].p1] - ptr[fastRect[0].p2] + ptr[fastRect[0].p3]) + + rect[1].weight * (ptr[fastRect[1].p0] - ptr[fastRect[1].p1] - ptr[fastRect[1].p2] + ptr[fastRect[1].p3]); + if (rect[2].weight != 0.0f) + ret += rect[2].weight * (ptr[fastRect[2].p0] - ptr[fastRect[2].p1] - ptr[fastRect[2].p2] + ptr[fastRect[2].p3]); + return ret; +} + +} // namespace br + +#include "representation/haar.moc" + diff --git a/openbr/plugins/representation/mblbp.cpp b/openbr/plugins/representation/mblbp.cpp new file mode 100644 index 0000000..4727d97 --- /dev/null +++ b/openbr/plugins/representation/mblbp.cpp @@ -0,0 +1,126 @@ +#include + +#include +#include + +using namespace cv; + +namespace br +{ + +#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step ) \ + /* (x, y) */ \ + (p0) = (rect).x + (step) * (rect).y; \ + /* (x + w, y) */ \ + (p1) = (rect).x + (rect).width + (step) * (rect).y; \ + /* (x + w, y) */ \ + (p2) = (rect).x + (step) * ((rect).y + (rect).height); \ + /* (x + w, y + h) */ \ + (p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height); + +class MBLBPRepresentation : public Representation +{ + Q_OBJECT + + Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false) + Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false) + BR_PROPERTY(int, winWidth, 24) + BR_PROPERTY(int, winHeight, 24) + + void init() + { + int offset = winWidth + 1; + for (int x = 0; x < winWidth; x++ ) + for (int y = 0; y < winHeight; y++ ) + for (int w = 1; w <= winWidth / 3; w++ ) + for (int h = 1; h <= winHeight / 3; h++ ) + if ((x+3*w <= winWidth) && (y+3*h <= winHeight) ) + features.append(Feature(offset, x, y, w, h ) ); + } + + void preprocess(const Mat &src, Mat &dst) const + { + integral(src, dst); + } + + float evaluate(const Mat &image, int idx) const + { + return (float)features[idx].calc(image); + } + + Mat evaluate(const Mat &image, const QList &indices) const + { + int size = indices.empty() ? numFeatures() : indices.size(); + + Mat result(1, size, CV_32FC1); + for (int i = 0; i < size; i++) + result.at(i) = evaluate(image, indices.empty() ? i : indices[i]); + return result; + } + + Size windowSize(int *dx, int *dy) const + { + if (dx && dy) + *dx = *dy = 1; + return Size(winWidth, winHeight); + } + + int numFeatures() const { return features.size(); } + int maxCatCount() const { return 256; } + + struct Feature + { + Feature() { rect = Rect(0, 0, 0, 0); } + Feature( int offset, int x, int y, int _block_w, int _block_h ); + uchar calc(const Mat &img) const; + + Rect rect; + int p[16]; + }; + QList features; +}; + +BR_REGISTER(Representation, MBLBPRepresentation) + +static inline void calcOffset(int &p0, int &p1, int &p2, int &p3, Rect rect, int offset) +{ + /* (x, y) */ + p0 = rect.x + offset * rect.y; + /* (x + w, y) */ + p1 = rect.x + rect.width + offset * rect.y; + /* (x + w, y) */ + p2 = rect.x + offset * (rect.y + rect.height); + /* (x + w, y + h) */ + p3 = rect.x + rect.width + offset * (rect.y + rect.height); +} + +MBLBPRepresentation::Feature::Feature( int offset, int x, int y, int _blockWidth, int _blockHeight ) +{ + Rect tr = rect = cvRect(x, y, _blockWidth, _blockHeight); + calcOffset(p[0], p[1], p[4], p[5], tr, offset); + tr.x += 2*rect.width; + calcOffset(p[2], p[3], p[6], p[7], tr, offset); + tr.y +=2*rect.height; + calcOffset(p[10], p[11], p[14], p[15], tr, offset); + tr.x -= 2*rect.width; + calcOffset(p[8], p[9], p[12], p[13], tr, offset); +} + +inline uchar MBLBPRepresentation::Feature::calc(const Mat &img) const +{ + const int* ptr = img.ptr(); + int cval = ptr[p[5]] - ptr[p[6]] - ptr[p[9]] + ptr[p[10]]; + + return (uchar)((ptr[p[0]] - ptr[p[1]] - ptr[p[4]] + ptr[p[5]] >= cval ? 128 : 0) | // 0 + (ptr[p[1]] - ptr[p[2]] - ptr[p[5]] + ptr[p[6]] >= cval ? 64 : 0) | // 1 + (ptr[p[2]] - ptr[p[3]] - ptr[p[6]] + ptr[p[7]] >= cval ? 32 : 0) | // 2 + (ptr[p[6]] - ptr[p[7]] - ptr[p[10]] + ptr[p[11]] >= cval ? 16 : 0) | // 5 + (ptr[p[10]] - ptr[p[11]] - ptr[p[14]] + ptr[p[15]] >= cval ? 8 : 0) | // 8 + (ptr[p[9]] - ptr[p[10]] - ptr[p[13]] + ptr[p[14]] >= cval ? 4 : 0) | // 7 + (ptr[p[8]] - ptr[p[9]] - ptr[p[12]] + ptr[p[13]] >= cval ? 2 : 0) | // 6 + (ptr[p[4]] - ptr[p[5]] - ptr[p[8]] + ptr[p[9]] >= cval ? 1 : 0)); // 3 +} + +} // namespace br + +#include "representation/mblbp.moc" diff --git a/openbr/plugins/representation/npd.cpp b/openbr/plugins/representation/npd.cpp new file mode 100644 index 0000000..30f1c5e --- /dev/null +++ b/openbr/plugins/representation/npd.cpp @@ -0,0 +1,71 @@ +#include + +using namespace cv; + +namespace br +{ + +class NPDRepresentation : public Representation +{ + Q_OBJECT + + Q_PROPERTY(int winWidth READ get_winWidth WRITE set_winWidth RESET reset_winWidth STORED false) + Q_PROPERTY(int winHeight READ get_winHeight WRITE set_winHeight RESET reset_winHeight STORED false) + BR_PROPERTY(int, winWidth, 24) + BR_PROPERTY(int, winHeight, 24) + + void init() + { + for (int p1 = 0; p1 < (winWidth * winHeight); p1++) + for (int p2 = p1; p2 < (winWidth * winHeight); p2++) + features.append(Feature(p1, p2)); + } + + float evaluate(const Mat &image, int idx) const + { + return features[idx].calc(image); + } + + Mat evaluate(const Mat &image, const QList &indices) const + { + int size = indices.empty() ? numFeatures() : indices.size(); + + Mat result(1, size, CV_32FC1); + for (int i = 0; i < size; i++) + result.at(i) = evaluate(image, indices.empty() ? i : indices[i]); + return result; + } + + Size windowSize(int *dx, int *dy) const + { + if (dx && dy) + *dx = *dy = 0; + return Size(winWidth, winHeight); + } + + int numFeatures() const { return features.size(); } + int maxCatCount() const { return 0; } + + struct Feature + { + Feature() {} + Feature(int p1, int p2) { p[0] = p1; p[1] = p2; } + float calc(const Mat &image) const; + + int p[2]; + }; + QList features; +}; + +BR_REGISTER(Representation, NPDRepresentation) + +inline float NPDRepresentation::Feature::calc(const Mat &image) const +{ + const int *ptr = image.ptr(); + int v1 = ptr[p[0]], v2 = ptr[p[1]]; + return v1 == 0 && v2 == 0 ? 0 : ((float)(v1 - v2)) / (v1 + v2); +} + +} // namespace br + +#include "representation/npd.moc" diff --git a/openbr/plugins/representation/random.cpp b/openbr/plugins/representation/random.cpp new file mode 100644 index 0000000..4d1ae74 --- /dev/null +++ b/openbr/plugins/representation/random.cpp @@ -0,0 +1,111 @@ +#include +#include + +#include +#include +#include + +using namespace cv; + +namespace br +{ + +/*! + * \ingroup galleries + * \brief Computes first order gradient histogram features using an integral image + * \author Scott Klum \cite sklum + */ +class RandomRepresentation : public Representation +{ + Q_OBJECT + + Q_PROPERTY(br::Representation* representation READ get_representation WRITE set_representation RESET reset_representation STORED false) + Q_PROPERTY(int count READ get_count WRITE set_count RESET reset_count STORED false) + BR_PROPERTY(br::Representation*, representation, NULL) + BR_PROPERTY(int, count, 20000) + + QList features; + + void train(const QList &images, const QList &labels) + { + representation->train(images, labels); + + const int nFeatures = representation->numFeatures(); + + if (Globals->verbose) + qDebug() << "Randomly sampling from" << nFeatures << "features."; + + features = Common::RandSample(count,nFeatures,0,true); + } + + void preprocess(const Mat &src, Mat &dst) const + { + representation->preprocess(src,dst); + } + + float evaluate(const Mat &image, int idx) const + { + return representation->evaluate(image,features[idx]); + } + + Mat evaluate(const Mat &image, const QList &indices) const + { + QList newIndices; + if (indices.empty()) + newIndices = features; + else + for (int i = 0; i < indices.size(); i++) + newIndices.append(features[indices[i]]); + + return representation->evaluate(image,newIndices); + } + + int numFeatures() const + { + return features.size(); + } + + int numChannels() const + { + return representation->numChannels(); + } + + Size windowSize(int *dx, int *dy) const + { + return representation->windowSize(dx,dy); + } + + int maxCatCount() const + { + return representation->maxCatCount(); + } + + void load(QDataStream &stream) + { + representation->load(stream); + + int numFeatures; stream >> numFeatures; + for (int i=0; i> feature; + features.append(feature); + } + } + + void store(QDataStream &stream) const + { + representation->store(stream); + + stream << features.size(); + for (int i=0; i