changes related with code review

This commit is contained in:
marina.kolpakova
2013-02-01 14:25:10 +04:00
parent f7ac73998a
commit cc538ddfa6
20 changed files with 175 additions and 137 deletions

View File

@@ -22,7 +22,7 @@
//
// * 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.
// 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.
@@ -44,41 +44,53 @@
#define __SFT_RANDOM_HPP__
#if defined(_MSC_VER) && _MSC_VER >= 1600
# include <random>
namespace sft {
namespace cv { namespace softcascade { namespace internal
{
struct Random
{
typedef std::mt19937 engine;
typedef std::uniform_int<int> uniform;
};
}
}}}
#elif (__GNUC__) && __GNUC__ > 3 && __GNUC_MINOR__ > 1 && !defined(__ANDROID__)
# if defined (__cplusplus) && __cplusplus > 201100L
# include <random>
namespace sft {
namespace cv { namespace softcascade { namespace internal
{
struct Random
{
typedef std::mt19937 engine;
typedef std::uniform_int<int> uniform;
};
}
# else
# include <tr1/random>
}}}
# else
# include <tr1/random>
namespace cv { namespace softcascade { namespace internal
{
namespace sft {
struct Random
{
typedef std::tr1::mt19937 engine;
typedef std::tr1::uniform_int<int> uniform;
};
}
}}}
# endif
#else
#include <opencv2/core/core.hpp>
# include <opencv2/core/core.hpp>
namespace cv { namespace softcascade { namespace internal
{
namespace rnd {
typedef cv::RNG engine;
@@ -104,13 +116,13 @@ private:
}
namespace sft {
struct Random
{
typedef rnd::engine engine;
typedef rnd::uniform_int<int> uniform;
};
}
}}}
#endif

View File

@@ -44,10 +44,13 @@
namespace {
using namespace cv::softcascade;
class ICFBuilder : public ChannelFeatureBuilder
{
virtual ~ICFBuilder() {}
virtual cv::AlgorithmInfo* info() const;
virtual void operator()(cv::InputArray _frame, CV_OUT cv::OutputArray _integrals) const
{
CV_Assert(_frame.type() == CV_8UC3);
@@ -107,9 +110,12 @@ class ICFBuilder : public ChannelFeatureBuilder
}
using cv::softcascade::ChannelFeatureBuilder;
using cv::softcascade::ChannelFeature;
CV_INIT_ALGORITHM(ICFBuilder, "ChannelFeatureBuilder.ICFBuilder", );
cv::scascade::ChannelFeatureBuilder::~ChannelFeatureBuilder() {}
ChannelFeatureBuilder::~ChannelFeatureBuilder() {}
cv::Ptr<ChannelFeatureBuilder> ChannelFeatureBuilder::create()
{
@@ -117,7 +123,7 @@ cv::Ptr<ChannelFeatureBuilder> ChannelFeatureBuilder::create()
return builder;
}
cv::scascade::ChannelFeature::ChannelFeature(int x, int y, int w, int h, int ch)
ChannelFeature::ChannelFeature(int x, int y, int w, int h, int ch)
: bb(cv::Rect(x, y, w, h)), channel(ch) {}
bool ChannelFeature::operator ==(ChannelFeature b)
@@ -131,7 +137,7 @@ bool ChannelFeature::operator !=(ChannelFeature b)
}
float cv::scascade::ChannelFeature::operator() (const cv::Mat& integrals, const cv::Size& model) const
float ChannelFeature::operator() (const cv::Mat& integrals, const cv::Size& model) const
{
int step = model.width + 1;
@@ -148,21 +154,23 @@ float cv::scascade::ChannelFeature::operator() (const cv::Mat& integrals, const
return (float)(a - b + c - d);
}
void cv::scascade::write(cv::FileStorage& fs, const string&, const ChannelFeature& f)
void cv::softcascade::write(cv::FileStorage& fs, const string&, const ChannelFeature& f)
{
fs << "{" << "channel" << f.channel << "rect" << f.bb << "}";
}
std::ostream& cv::scascade::operator<<(std::ostream& out, const ChannelFeature& m)
std::ostream& cv::softcascade::operator<<(std::ostream& out, const ChannelFeature& m)
{
out << m.channel << " " << m.bb;
return out;
}
cv::scascade::ChannelFeature::~ChannelFeature(){}
ChannelFeature::~ChannelFeature(){}
namespace {
using namespace cv::softcascade;
class ChannelFeaturePool : public FeaturePool
{
public:
@@ -200,6 +208,7 @@ void ChannelFeaturePool::write( cv::FileStorage& fs, int index) const
void ChannelFeaturePool::fill(int desired)
{
using namespace cv::softcascade::internal;
int mw = model.width;
int mh = model.height;
@@ -208,16 +217,16 @@ void ChannelFeaturePool::fill(int desired)
int nfeatures = std::min(desired, maxPoolSize);
pool.reserve(nfeatures);
sft::Random::engine eng(FEATURE_RECT_SEED);
sft::Random::engine eng_ch(DCHANNELS_SEED);
Random::engine eng(FEATURE_RECT_SEED);
Random::engine eng_ch(DCHANNELS_SEED);
sft::Random::uniform chRand(0, N_CHANNELS - 1);
Random::uniform chRand(0, N_CHANNELS - 1);
sft::Random::uniform xRand(0, model.width - 2);
sft::Random::uniform yRand(0, model.height - 2);
Random::uniform xRand(0, model.width - 2);
Random::uniform yRand(0, model.height - 2);
sft::Random::uniform wRand(1, model.width - 1);
sft::Random::uniform hRand(1, model.height - 1);
Random::uniform wRand(1, model.width - 1);
Random::uniform hRand(1, model.height - 1);
while (pool.size() < size_t(nfeatures))
{
@@ -246,7 +255,7 @@ void ChannelFeaturePool::fill(int desired)
}
cv::Ptr<FeaturePool> cv::scascade::FeaturePool::create(const cv::Size& model, int nfeatures)
cv::Ptr<FeaturePool> FeaturePool::create(const cv::Size& model, int nfeatures)
{
cv::Ptr<FeaturePool> pool(new ChannelFeaturePool(model, nfeatures));
return pool;

View File

@@ -55,6 +55,4 @@
#include "opencv2/ml/ml.hpp"
#include "_random.hpp"
using namespace cv::scascade;
#endif

View File

@@ -48,13 +48,17 @@ using cv::InputArray;
using cv::OutputArray;
using cv::Mat;
cv::scascade::FeaturePool::~FeaturePool(){}
cv::scascade::Dataset::~Dataset(){}
using cv::softcascade::Octave;
using cv::softcascade::FeaturePool;
using cv::softcascade::Dataset;
using cv::softcascade::ChannelFeatureBuilder;
FeaturePool::~FeaturePool(){}
Dataset::~Dataset(){}
namespace {
class BoostedSoftCascadeOctave : public cv::Boost, public SoftCascadeOctave
class BoostedSoftCascadeOctave : public cv::Boost, public Octave
{
public:
@@ -214,14 +218,15 @@ void BoostedSoftCascadeOctave::processPositives(const Dataset* dataset)
void BoostedSoftCascadeOctave::generateNegatives(const Dataset* dataset)
{
using namespace cv::softcascade::internal;
// ToDo: set seed, use offsets
sft::Random::engine eng(DX_DY_SEED);
sft::Random::engine idxEng(INDEX_ENGINE_SEED);
Random::engine eng(DX_DY_SEED);
Random::engine idxEng(INDEX_ENGINE_SEED);
int h = boundingBox.height;
int nimages = dataset->available(Dataset::NEGATIVE);
sft::Random::uniform iRand(0, nimages - 1);
Random::uniform iRand(0, nimages - 1);
int total = 0;
Mat sum;
@@ -236,8 +241,8 @@ void BoostedSoftCascadeOctave::generateNegatives(const Dataset* dataset)
int maxW = frame.cols - 2 * boundingBox.x - boundingBox.width;
int maxH = frame.rows - 2 * boundingBox.y - boundingBox.height;
sft::Random::uniform wRand(0, maxW -1);
sft::Random::uniform hRand(0, maxH -1);
Random::uniform wRand(0, maxW -1);
Random::uniform hRand(0, maxH -1);
int dx = wRand(eng);
int dy = hRand(eng);
@@ -439,12 +444,12 @@ void BoostedSoftCascadeOctave::write( CvFileStorage* fs, std::string _name) cons
CV_INIT_ALGORITHM(BoostedSoftCascadeOctave, "SoftCascadeOctave.BoostedSoftCascadeOctave", );
cv::scascade::SoftCascadeOctave::~SoftCascadeOctave(){}
Octave::~Octave(){}
cv::Ptr<SoftCascadeOctave> cv::scascade::SoftCascadeOctave::create(cv::Rect boundingBox, int npositives, int nnegatives,
cv::Ptr<Octave> Octave::create(cv::Rect boundingBox, int npositives, int nnegatives,
int logScale, int shrinkage, int poolSize)
{
cv::Ptr<SoftCascadeOctave> octave(
cv::Ptr<Octave> octave(
new BoostedSoftCascadeOctave(boundingBox, npositives, nnegatives, logScale, shrinkage, poolSize));
return octave;
}

View File

@@ -42,11 +42,17 @@
#include "precomp.hpp"
using cv::softcascade::Detection;
using cv::softcascade::Detector;
using cv::softcascade::ChannelFeatureBuilder;
using namespace cv;
namespace {
struct Octave
struct SOctave
{
Octave(const int i, const cv::Size& origObjSize, const cv::FileNode& fn)
SOctave(const int i, const cv::Size& origObjSize, const cv::FileNode& fn)
: index(i), weaks((int)fn[SC_OCT_WEAKS]), scale(pow(2,(float)fn[SC_OCT_SCALE])),
size(cvRound(origObjSize.width * scale), cvRound(origObjSize.height * scale)) {}
@@ -115,16 +121,16 @@ struct Feature
static const char *const SC_F_RECT;
};
const char *const Octave::SC_OCT_SCALE = "scale";
const char *const Octave::SC_OCT_WEAKS = "weaks";
const char *const Octave::SC_OCT_SHRINKAGE = "shrinkingFactor";
const char *const SOctave::SC_OCT_SCALE = "scale";
const char *const SOctave::SC_OCT_WEAKS = "weaks";
const char *const SOctave::SC_OCT_SHRINKAGE = "shrinkingFactor";
const char *const Weak::SC_WEAK_THRESHOLD = "treeThreshold";
const char *const Feature::SC_F_CHANNEL = "channel";
const char *const Feature::SC_F_RECT = "rect";
struct Level
{
const Octave* octave;
const SOctave* octave;
float origScale;
float relScale;
@@ -135,7 +141,7 @@ struct Level
float scaling[2]; // 0-th for channels <= 6, 1-st otherwise
Level(const Octave& oct, const float scale, const int shrinkage, const int w, const int h)
Level(const SOctave& oct, const float scale, const int shrinkage, const int w, const int h)
: octave(&oct), origScale(scale), relScale(scale / oct.scale),
workRect(cv::Size(cvRound(w / (float)shrinkage),cvRound(h / (float)shrinkage))),
objSize(cv::Size(cvRound(oct.size.width * relScale), cvRound(oct.size.height * relScale)))
@@ -205,7 +211,8 @@ struct ChannelStorage
}
struct SoftCascadeDetector::Fields
struct Detector::Fields
{
float minScale;
float maxScale;
@@ -216,7 +223,7 @@ struct SoftCascadeDetector::Fields
int shrinkage;
std::vector<Octave> octaves;
std::vector<SOctave> octaves;
std::vector<Weak> weaks;
std::vector<Node> nodes;
std::vector<float> leaves;
@@ -226,14 +233,14 @@ struct SoftCascadeDetector::Fields
cv::Size frameSize;
typedef std::vector<Octave>::iterator octIt_t;
typedef std::vector<SOctave>::iterator octIt_t;
typedef std::vector<Detection> dvector;
void detectAt(const int dx, const int dy, const Level& level, const ChannelStorage& storage, dvector& detections) const
{
float detectionScore = 0.f;
const Octave& octave = *(level.octave);
const SOctave& octave = *(level.octave);
int stBegin = octave.index * octave.weaks, stEnd = stBegin + octave.weaks;
@@ -279,7 +286,7 @@ struct SoftCascadeDetector::Fields
octIt_t res = octaves.begin();
for (octIt_t oct = octaves.begin(); oct < octaves.end(); ++oct)
{
const Octave& octave =*oct;
const SOctave& octave =*oct;
float logOctave = log(octave.scale);
float logAbsScale = fabs(logFactor - logOctave);
@@ -373,7 +380,7 @@ struct SoftCascadeDetector::Fields
for (int octIndex = 0; it != it_end; ++it, ++octIndex)
{
FileNode fns = *it;
Octave octave(octIndex, cv::Size(origObjWidth, origObjHeight), fns);
SOctave octave(octIndex, cv::Size(origObjWidth, origObjHeight), fns);
CV_Assert(octave.weaks > 0);
octaves.push_back(octave);
@@ -409,17 +416,17 @@ struct SoftCascadeDetector::Fields
}
};
SoftCascadeDetector::SoftCascadeDetector(const double mins, const double maxs, const int nsc, const int rej)
Detector::Detector(const double mins, const double maxs, const int nsc, const int rej)
: fields(0), minScale(mins), maxScale(maxs), scales(nsc), rejCriteria(rej) {}
SoftCascadeDetector::~SoftCascadeDetector() { delete fields;}
Detector::~Detector() { delete fields;}
void SoftCascadeDetector::read(const FileNode& fn)
void Detector::read(const cv::FileNode& fn)
{
Algorithm::read(fn);
}
bool SoftCascadeDetector::load(const FileNode& fn)
bool Detector::load(const cv::FileNode& fn)
{
if (fields) delete fields;
@@ -429,6 +436,7 @@ bool SoftCascadeDetector::load(const FileNode& fn)
namespace {
using cv::softcascade::Detection;
typedef std::vector<Detection> dvector;
@@ -472,13 +480,13 @@ void DollarNMS(dvector& objects)
static void suppress(int type, std::vector<Detection>& objects)
{
CV_Assert(type == SoftCascadeDetector::DOLLAR);
CV_Assert(type == Detector::DOLLAR);
DollarNMS(objects);
}
}
void SoftCascadeDetector::detectNoRoi(const cv::Mat& image, std::vector<Detection>& objects) const
void Detector::detectNoRoi(const cv::Mat& image, std::vector<Detection>& objects) const
{
Fields& fld = *fields;
// create integrals
@@ -502,10 +510,10 @@ void SoftCascadeDetector::detectNoRoi(const cv::Mat& image, std::vector<Detectio
}
}
// if (rejCriteria != NO_REJECT) suppress(rejCriteria, objects);
if (rejCriteria != NO_REJECT) suppress(rejCriteria, objects);
}
void SoftCascadeDetector::detect(cv::InputArray _image, cv::InputArray _rois, std::vector<Detection>& objects) const
void Detector::detect(cv::InputArray _image, cv::InputArray _rois, std::vector<Detection>& objects) const
{
// only color images are suppered
cv::Mat image = _image.getMat();
@@ -557,7 +565,7 @@ void SoftCascadeDetector::detect(cv::InputArray _image, cv::InputArray _rois, st
if (rejCriteria != NO_REJECT) suppress(rejCriteria, objects);
}
void SoftCascadeDetector::detect(InputArray _image, InputArray _rois, OutputArray _rects, OutputArray _confs) const
void Detector::detect(InputArray _image, InputArray _rois, OutputArray _rects, OutputArray _confs) const
{
std::vector<Detection> objects;
detect( _image, _rois, objects);

View File

@@ -42,10 +42,10 @@
#include "precomp.hpp"
namespace cv { namespace scascade
namespace cv { namespace softcascade
{
CV_INIT_ALGORITHM(SoftCascadeDetector, "SoftCascade.SoftCascadeDetector",
CV_INIT_ALGORITHM(Detector, "SoftCascade.Detector",
obj.info()->addParam(obj, "minScale", obj.minScale);
obj.info()->addParam(obj, "maxScale", obj.maxScale);
obj.info()->addParam(obj, "scales", obj.scales);
@@ -54,7 +54,7 @@ CV_INIT_ALGORITHM(SoftCascadeDetector, "SoftCascade.SoftCascadeDetector",
bool initModule_softcascade(void)
{
Ptr<Algorithm> sc1 = createSoftCascadeDetector();
Ptr<Algorithm> sc1 = createDetector();
return (sc1->info() != 0);
}