From 85aaf88aa9253a39dd47717c709835f4ba12c025 Mon Sep 17 00:00:00 2001 From: Vlad Shakhuro Date: Thu, 3 Jul 2014 08:32:36 +0400 Subject: [PATCH] Derive ACFFeatureEvaluator from Algorithm --- .../xobjdetect/include/opencv2/xobjdetect.hpp | 26 +++----- modules/xobjdetect/src/acffeature.cpp | 65 ++++++++++++++----- modules/xobjdetect/src/icfdetector.cpp | 6 +- 3 files changed, 60 insertions(+), 37 deletions(-) diff --git a/modules/xobjdetect/include/opencv2/xobjdetect.hpp b/modules/xobjdetect/include/opencv2/xobjdetect.hpp index 5d2dabbd0..7d6fa238a 100644 --- a/modules/xobjdetect/include/opencv2/xobjdetect.hpp +++ b/modules/xobjdetect/include/opencv2/xobjdetect.hpp @@ -60,37 +60,31 @@ namespace xobjdetect */ void computeChannels(InputArray image, OutputArrayOfArrays channels); -class CV_EXPORTS ACFFeatureEvaluator +class CV_EXPORTS ACFFeatureEvaluator : public Algorithm { public: - /* Construct evaluator, set features to evaluate */ - ACFFeatureEvaluator(const std::vector& features); - /* Set channels for feature evaluation */ - void setChannels(InputArrayOfArrays channels); + virtual void setChannels(InputArrayOfArrays channels) = 0; /* Set window position */ - void setPosition(Size position); + virtual void setPosition(Size position) = 0; /* Evaluate feature with given index for current channels and window position */ - int evaluate(size_t feature_ind) const; + virtual int evaluate(size_t feature_ind) const = 0; /* Evaluate all features for current channels and window position Returns matrix-column of features */ - void evaluateAll(OutputArray feature_values) const; + virtual void evaluateAll(OutputArray feature_values) const = 0; -private: - /* Features to evaluate */ - std::vector features_; - /* Channels for feature evaluation */ - std::vector channels_; - /* Channels window position */ - Size position_; }; +/* Construct evaluator, set features to evaluate */ +CV_EXPORTS Ptr +createACFFeatureEvaluator(const std::vector& features); + /* Generate acf features window_size — size of window in which features should be evaluated @@ -158,7 +152,7 @@ private: float pos_value_, neg_value_; }; -class CV_EXPORTS WaldBoost +class CV_EXPORTS WaldBoost : public Algorithm { public: /* Initialize WaldBoost cascade with default of specified parameters */ diff --git a/modules/xobjdetect/src/acffeature.cpp b/modules/xobjdetect/src/acffeature.cpp index d62fb843c..1c642f661 100644 --- a/modules/xobjdetect/src/acffeature.cpp +++ b/modules/xobjdetect/src/acffeature.cpp @@ -45,33 +45,44 @@ using std::vector; using std::min; +#include +using std::cout; +using std::endl; + namespace cv { namespace xobjdetect { -ACFFeatureEvaluator::ACFFeatureEvaluator(const vector& features): - features_(features), channels_(), position_() -{ -} -int ACFFeatureEvaluator::evaluate(size_t feature_ind) const +class ACFFeatureEvaluatorImpl : public ACFFeatureEvaluator { - /* Assume there are 10 channels */ - CV_Assert(channels_.size() == 10); - CV_Assert(feature_ind < features_.size()); - - Point3i feature = features_.at(feature_ind); - int x = feature.x; - int y = feature.y; - int n = feature.z; - return channels_[n].at(y, x); -} +public: + ACFFeatureEvaluatorImpl(const vector& features): + features_(features), channels_(), position_() + { + CV_Assert(features.size() > 0); + } -void ACFFeatureEvaluator::setChannels(cv::InputArrayOfArrays channels) + virtual void setChannels(InputArrayOfArrays channels); + virtual void setPosition(Size position); + virtual int evaluate(size_t feature_ind) const; + virtual void evaluateAll(OutputArray feature_values) const; + +private: + /* Features to evaluate */ + std::vector features_; + /* Channels for feature evaluation */ + std::vector channels_; + /* Channels window position */ + Size position_; +}; + +void ACFFeatureEvaluatorImpl::setChannels(cv::InputArrayOfArrays channels) { channels_.clear(); vector ch; channels.getMatVector(ch); + CV_Assert(ch.size() == 10); for( size_t i = 0; i < ch.size(); ++i ) { const Mat &channel = ch[i]; @@ -92,12 +103,24 @@ void ACFFeatureEvaluator::setChannels(cv::InputArrayOfArrays channels) } } -void ACFFeatureEvaluator::setPosition(Size position) +void ACFFeatureEvaluatorImpl::setPosition(Size position) { position_ = position; } -void ACFFeatureEvaluator::evaluateAll(OutputArray feature_values) const +int ACFFeatureEvaluatorImpl::evaluate(size_t feature_ind) const +{ + CV_Assert(channels_.size() == 10); + CV_Assert(feature_ind < features_.size()); + + Point3i feature = features_.at(feature_ind); + int x = feature.x; + int y = feature.y; + int n = feature.z; + return channels_[n].at(y, x); +} + +void ACFFeatureEvaluatorImpl::evaluateAll(OutputArray feature_values) const { Mat_ feature_vals(1, (int)features_.size()); for( int i = 0; i < (int)features_.size(); ++i ) @@ -107,6 +130,12 @@ void ACFFeatureEvaluator::evaluateAll(OutputArray feature_values) const feature_values.setTo(feature_vals); } +Ptr +createACFFeatureEvaluator(const vector& features) +{ + return Ptr(new ACFFeatureEvaluatorImpl(features)); +} + vector generateFeatures(Size window_size, int count) { CV_Assert(count > 0); diff --git a/modules/xobjdetect/src/icfdetector.cpp b/modules/xobjdetect/src/icfdetector.cpp index a7d328069..d29484056 100644 --- a/modules/xobjdetect/src/icfdetector.cpp +++ b/modules/xobjdetect/src/icfdetector.cpp @@ -118,7 +118,7 @@ void ICFDetector::train(const vector& image_filenames, labels(0, i) = -1; vector features = generateFeatures(model_size); - ACFFeatureEvaluator feature_evaluator(features); + Ptr feature_evaluator = createACFFeatureEvaluator(features); Mat_ data((int)features.size(), (int)samples.size()); Mat_ feature_col; @@ -127,8 +127,8 @@ void ICFDetector::train(const vector& image_filenames, for( int i = 0; i < (int)samples.size(); ++i ) { computeChannels(samples[i], channels); - feature_evaluator.setChannels(channels); - feature_evaluator.evaluateAll(feature_col); + feature_evaluator->setChannels(channels); + feature_evaluator->evaluateAll(feature_col); for( int j = 0; j < feature_col.rows; ++j ) data(i, j) = feature_col(0, j); }