From: marina.kolpakova Date: Wed, 30 Jan 2013 06:43:18 +0000 (+0400) Subject: move feature pool to softcascade module X-Git-Tag: submit/tizen_ivi/20141117.190038~2^2~1187^2~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fa15fcc53eb070f3ade8036ec7afdd824bc12136;p=profile%2Fivi%2Fopencv.git move feature pool to softcascade module --- diff --git a/apps/sft/fpool.cpp b/apps/sft/fpool.cpp index d114c01..825d88d 100644 --- a/apps/sft/fpool.cpp +++ b/apps/sft/fpool.cpp @@ -41,101 +41,10 @@ //M*/ #include -#include #include - #include -// ========= FeaturePool ========= // - -sft::ICFFeaturePool::ICFFeaturePool(cv::Size m, int n) : FeaturePool(), model(m), nfeatures(n) -{ - CV_Assert(m != cv::Size() && n > 0); - fill(nfeatures); -} - -float sft::ICFFeaturePool::apply(int fi, int si, const Mat& integrals) const -{ - return pool[fi](integrals.row(si), model); -} - -void sft::ICFFeaturePool::write( cv::FileStorage& fs, int index) const -{ - CV_Assert((index > 0) && (index < (int)pool.size())); - fs << pool[index]; -} - -sft::ICFFeaturePool::~ICFFeaturePool(){} - -#if defined _WIN32 && (_WIN32 || _WIN64) -# if _WIN64 -# define USE_LONG_SEEDS -# endif -#endif -#if defined (__GNUC__) &&__GNUC__ -# if defined(__x86_64__) || defined(__ppc64__) -# define USE_LONG_SEEDS -# endif -#endif - -#if defined USE_LONG_SEEDS -# define FEATURE_RECT_SEED 8854342234LU -#else -# define FEATURE_RECT_SEED 88543422LU -#endif -# define DCHANNELS_SEED 314152314LU -#undef USE_LONG_SEEDS - -void sft::ICFFeaturePool::fill(int desired) -{ - int mw = model.width; - int mh = model.height; - - int maxPoolSize = (mw -1) * mw / 2 * (mh - 1) * mh / 2 * N_CHANNELS; - - nfeatures = std::min(desired, maxPoolSize); - dprintf("Requeste feature pool %d max %d suggested %d\n", desired, maxPoolSize, nfeatures); - - pool.reserve(nfeatures); - - sft::Random::engine eng(FEATURE_RECT_SEED); - sft::Random::engine eng_ch(DCHANNELS_SEED); - - sft::Random::uniform chRand(0, N_CHANNELS - 1); - - sft::Random::uniform xRand(0, model.width - 2); - sft::Random::uniform yRand(0, model.height - 2); - - sft::Random::uniform wRand(1, model.width - 1); - sft::Random::uniform hRand(1, model.height - 1); - - while (pool.size() < size_t(nfeatures)) - { - int x = xRand(eng); - int y = yRand(eng); - - int w = 1 + wRand(eng, model.width - x - 1); - int h = 1 + hRand(eng, model.height - y - 1); - - CV_Assert(w > 0); - CV_Assert(h > 0); - - CV_Assert(w + x < model.width); - CV_Assert(h + y < model.height); - - int ch = chRand(eng_ch); - - cv::ChannelFeature f(x, y, w, h, ch); - - if (std::find(pool.begin(), pool.end(),f) == pool.end()) - { - pool.push_back(f); - std::cout << f << std::endl; - } - } -} - // ============ Dataset ============ // namespace { using namespace sft; diff --git a/apps/sft/include/sft/fpool.hpp b/apps/sft/include/sft/fpool.hpp index 0790b09..ad0f56d 100644 --- a/apps/sft/include/sft/fpool.hpp +++ b/apps/sft/include/sft/fpool.hpp @@ -51,36 +51,8 @@ namespace sft { -using cv::FeaturePool; using cv::Dataset; -class ICFFeaturePool : public cv::FeaturePool -{ -public: - ICFFeaturePool(cv::Size model, int nfeatures); - - virtual int size() const { return (int)pool.size(); } - virtual float apply(int fi, int si, const cv::Mat& integrals) const; - virtual void write( cv::FileStorage& fs, int index) const; - - virtual ~ICFFeaturePool(); - -private: - - void fill(int desired); - - cv::Size model; - int nfeatures; - - std::vector pool; - - static const unsigned int seed = 0; - - enum { N_CHANNELS = 10 }; -}; - - - class ScaledDataset : public Dataset { public: diff --git a/apps/sft/include/sft/random.hpp b/apps/sft/include/sft/random.hpp deleted file mode 100644 index 3a3be8b..0000000 --- a/apps/sft/include/sft/random.hpp +++ /dev/null @@ -1,117 +0,0 @@ -/*M/////////////////////////////////////////////////////////////////////////////////////// -// -// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. -// -// By downloading, copying, installing or using the software you agree to this license. -// If you do not agree to this license, do not download, install, -// copy or use the software. -// -// -// License Agreement -// For Open Source Computer Vision Library -// -// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. -// Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved. -// Third party copyrights are property of their respective owners. -// -// Redistribution and use in source and binary forms, with or without modification, -// are permitted provided that the following conditions are met: -// -// * Redistribution's of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// * Redistribution's in binary form must reproduce the above copyright notice, -// this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * The name of the copyright holders may not be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// This software is provided by the copyright holders and contributors "as is" and -// any express or implied warranties, including, but not limited to, the implied -// warranties of merchantability and fitness for a particular purpose are disclaimed. -// In no event shall the Intel Corporation or contributors be liable for any direct, -// indirect, incidental, special, exemplary, or consequential damages -// (including, but not limited to, procurement of substitute goods or services; -// loss of use, data, or profits; or business interruption) however caused -// and on any theory of liability, whether in contract, strict liability, -// or tort (including negligence or otherwise) arising in any way out of -// the use of this software, even if advised of the possibility of such damage. -// -//M*/ - -#ifndef __SFT_RANDOM_HPP__ -#define __SFT_RANDOM_HPP__ - -#if defined(_MSC_VER) && _MSC_VER >= 1600 - -# include -namespace sft { -struct Random -{ - typedef std::mt19937 engine; - typedef std::uniform_int uniform; -}; -} - -#elif (__GNUC__) && __GNUC__ > 3 && __GNUC_MINOR__ > 1 && !defined(__ANDROID__) - -# if defined (__cplusplus) && __cplusplus > 201100L -# include -namespace sft { -struct Random -{ - typedef std::mt19937 engine; - typedef std::uniform_int uniform; -}; -} -# else -# include - -namespace sft { -struct Random -{ - typedef std::tr1::mt19937 engine; - typedef std::tr1::uniform_int uniform; -}; -} -# endif - -#else -#include -namespace rnd { - -typedef cv::RNG engine; - -template -struct uniform_int -{ - uniform_int(const int _min, const int _max) : min(_min), max(_max) {} - T operator() (engine& eng, const int bound) const - { - return (T)eng.uniform(min, bound); - } - - T operator() (engine& eng) const - { - return (T)eng.uniform(min, max); - } - -private: - int min; - int max; -}; - -} - -namespace sft { -struct Random -{ - typedef rnd::engine engine; - typedef rnd::uniform_int uniform; -}; -} - -#endif - -#endif \ No newline at end of file diff --git a/apps/sft/sft.cpp b/apps/sft/sft.cpp index 531e39f..67c56fc 100644 --- a/apps/sft/sft.cpp +++ b/apps/sft/sft.cpp @@ -118,8 +118,8 @@ int main(int argc, char** argv) int nfeatures = cfg.poolSize; cv::Size model = cfg.model(it); std::cout << "Model " << model << std::endl; - sft::ICFFeaturePool pool(model, nfeatures); - nfeatures = pool.size(); + cv::Ptr pool = cv::FeaturePool::create(model, nfeatures); + nfeatures = pool->size(); int npositives = cfg.positives; @@ -135,7 +135,7 @@ int main(int argc, char** argv) std::string path = cfg.trainPath; sft::ScaledDataset dataset(path, *it); - if (boost->train(&dataset, &pool, cfg.weaks, cfg.treeDepth)) + if (boost->train(&dataset, pool, cfg.weaks, cfg.treeDepth)) { CvFileStorage* fout = cvOpenFileStorage(cfg.resPath(it).c_str(), 0, CV_STORAGE_WRITE); boost->write(fout, cfg.cascadeName); @@ -145,7 +145,7 @@ int main(int argc, char** argv) cv::Mat thresholds; boost->setRejectThresholds(thresholds); - boost->write(fso, &pool, thresholds); + boost->write(fso, pool, thresholds); cv::FileStorage tfs(("thresholds." + cfg.resPath(it)).c_str(), cv::FileStorage::WRITE); tfs << "thresholds" << thresholds; diff --git a/modules/softcascade/include/opencv2/softcascade/softcascade.hpp b/modules/softcascade/include/opencv2/softcascade/softcascade.hpp index ebf088a..70123c0 100644 --- a/modules/softcascade/include/opencv2/softcascade/softcascade.hpp +++ b/modules/softcascade/include/opencv2/softcascade/softcascade.hpp @@ -64,15 +64,7 @@ struct CV_EXPORTS Detection int kind; }; -class CV_EXPORTS FeaturePool -{ -public: - virtual int size() const = 0; - virtual float apply(int fi, int si, const Mat& integrals) const = 0; - virtual void write( cv::FileStorage& fs, int index) const = 0; - virtual ~FeaturePool(); -}; class CV_EXPORTS Dataset { @@ -85,6 +77,22 @@ public: }; // ========================================================================== // +// Public interface feature pool. +// ========================================================================== // + +class CV_EXPORTS FeaturePool +{ +public: + + virtual int size() const = 0; + virtual float apply(int fi, int si, const Mat& channels) const = 0; + virtual void write( cv::FileStorage& fs, int index) const = 0; + virtual ~FeaturePool(); + + static cv::Ptr create(const cv::Size& model, int nfeatures); +}; + +// ========================================================================== // // First order channel feature. // ========================================================================== // diff --git a/modules/softcascade/src/integral_channel_builder.cpp b/modules/softcascade/src/integral_channel_builder.cpp index ce437db..539642a 100644 --- a/modules/softcascade/src/integral_channel_builder.cpp +++ b/modules/softcascade/src/integral_channel_builder.cpp @@ -41,6 +41,7 @@ //M*/ #include "precomp.hpp" +#include "_random.hpp" namespace { @@ -160,3 +161,116 @@ std::ostream& cv::operator<<(std::ostream& out, const cv::ChannelFeature& m) } cv::ChannelFeature::~ChannelFeature(){} + +namespace { + +class ChannelFeaturePool : public cv::FeaturePool +{ +public: + ChannelFeaturePool(cv::Size m, int n) : FeaturePool(), model(m) + { + CV_Assert(m != cv::Size() && n > 0); + fill(n); + } + + virtual int size() const { return (int)pool.size(); } + virtual float apply(int fi, int si, const cv::Mat& integrals) const; + virtual void write( cv::FileStorage& fs, int index) const; + + virtual ~ChannelFeaturePool() {} + +private: + + void fill(int desired); + + cv::Size model; + std::vector pool; + enum { N_CHANNELS = 10 }; +}; + +float ChannelFeaturePool::apply(int fi, int si, const cv::Mat& integrals) const +{ + return pool[fi](integrals.row(si), model); +} + +void ChannelFeaturePool::write( cv::FileStorage& fs, int index) const +{ + CV_Assert((index > 0) && (index < (int)pool.size())); + fs << pool[index]; +} + +#if defined _WIN32 && (_WIN32 || _WIN64) +# if _WIN64 +# define USE_LONG_SEEDS +# endif +#endif +#if defined (__GNUC__) &&__GNUC__ +# if defined(__x86_64__) || defined(__ppc64__) +# define USE_LONG_SEEDS +# endif +#endif + +#if defined USE_LONG_SEEDS +# define FEATURE_RECT_SEED 8854342234LU +#else +# define FEATURE_RECT_SEED 88543422LU +#endif +# define DCHANNELS_SEED 314152314LU +#undef USE_LONG_SEEDS + +void ChannelFeaturePool::fill(int desired) +{ + int mw = model.width; + int mh = model.height; + + int maxPoolSize = (mw -1) * mw / 2 * (mh - 1) * mh / 2 * N_CHANNELS; + + int nfeatures = std::min(desired, maxPoolSize); + // dprintf("Requeste feature pool %d max %d suggested %d\n", desired, maxPoolSize, nfeatures); + + pool.reserve(nfeatures); + + sft::Random::engine eng(FEATURE_RECT_SEED); + sft::Random::engine eng_ch(DCHANNELS_SEED); + + sft::Random::uniform chRand(0, N_CHANNELS - 1); + + sft::Random::uniform xRand(0, model.width - 2); + sft::Random::uniform yRand(0, model.height - 2); + + sft::Random::uniform wRand(1, model.width - 1); + sft::Random::uniform hRand(1, model.height - 1); + + while (pool.size() < size_t(nfeatures)) + { + int x = xRand(eng); + int y = yRand(eng); + + int w = 1 + wRand(eng, model.width - x - 1); + int h = 1 + hRand(eng, model.height - y - 1); + + CV_Assert(w > 0); + CV_Assert(h > 0); + + CV_Assert(w + x < model.width); + CV_Assert(h + y < model.height); + + int ch = chRand(eng_ch); + + cv::ChannelFeature f(x, y, w, h, ch); + + if (std::find(pool.begin(), pool.end(),f) == pool.end()) + { + pool.push_back(f); + std::cout << f << std::endl; + } + } +} + +} + +cv::Ptr cv::FeaturePool::create(const cv::Size& model, int nfeatures) +{ + cv::Ptr pool(new ChannelFeaturePool(model, nfeatures)); + return pool; +}