diff --git a/openbr/core/boost.cpp b/openbr/core/boost.cpp index ec3ed1b..d6478c5 100644 --- a/openbr/core/boost.cpp +++ b/openbr/core/boost.cpp @@ -142,7 +142,10 @@ static CvMat* cvPreprocessIndexArray( const CvMat* idx_arr, int data_arr_size, b void FeatureEvaluator::init(Representation *_representation, int _maxSampleCount) { representation = _representation; - data.create((int)_maxSampleCount, representation->postWindowSize().area(), CV_32SC1); + + int dx, dy; + Size windowSize = representation->windowSize(dx, dy); + data.create((int)_maxSampleCount, Size(windowSize.width + dx, windowSize.height + dy).area(), CV_32SC1); cls.create( (int)_maxSampleCount, 1, CV_32FC1 ); } @@ -150,7 +153,9 @@ void FeatureEvaluator::setImage(const Mat &img, uchar clsLabel, int idx) { cls.ptr(idx)[0] = clsLabel; - Mat integralImg(representation->postWindowSize(), data.type(), data.ptr(idx)); + int dx, dy; + Size windowSize = representation->windowSize(dx, dy); + Mat integralImg(Size(windowSize.width + dx, windowSize.height + dy), data.type(), data.ptr(idx)); representation->preprocess(img, integralImg); } diff --git a/openbr/core/cascade.cpp b/openbr/core/cascade.cpp index bf32e84..6db2b6e 100644 --- a/openbr/core/cascade.cpp +++ b/openbr/core/cascade.cpp @@ -231,7 +231,8 @@ void _CascadeClassifier::detectMultiScale(const Mat& image, vector& object Mat imageBuffer(image.rows + 1, image.cols + 1, CV_8U); for (double factor = 1; ; factor *= scaleFactor) { - Size originalWindowSize = representation->preWindowSize(); + int dx, dy; + Size originalWindowSize = representation->windowSize(dx, dy); Size windowSize(cvRound(originalWindowSize.width*factor), cvRound(originalWindowSize.height*factor) ); Size scaledImageSize(cvRound(image.cols/factor ), cvRound(image.rows/factor)); @@ -253,7 +254,7 @@ void _CascadeClassifier::detectMultiScale(const Mat& image, vector& object int yStep = factor > 2. ? 1 : 2; for (int y = 0; y < processingRectSize.height; y += yStep) { for (int x = 0; x < processingRectSize.width; x += yStep) { - Mat window = repImage(Rect(Point(x, y), representation->postWindowSize())).clone(); + Mat window = repImage(Rect(Point(x, y), Size(originalWindowSize.width + dx, originalWindowSize.height + dy))).clone(); double gypWeight; int result = predict(window, gypWeight); diff --git a/openbr/openbr_plugin.h b/openbr/openbr_plugin.h index 22b960b..7d14163 100644 --- a/openbr/openbr_plugin.h +++ b/openbr/openbr_plugin.h @@ -1407,8 +1407,7 @@ public: // and returned. virtual cv::Mat evaluate(const cv::Mat &image, const QList &indices = QList()) const = 0; - virtual cv::Size preWindowSize() const = 0; // window size before preprocessing - virtual cv::Size postWindowSize() const = 0; // window size after preprocessing + virtual cv::Size windowSize(int &dx, int &dy) const = 0; // dx and dy should indicate the change to the original window size after preprocessing virtual int numFeatures() const = 0; virtual int maxCatCount() const = 0; }; @@ -1427,7 +1426,7 @@ public: // Slots for representation virtual cv::Mat preprocess(const cv::Mat &image) const = 0; - virtual cv::Size windowSize() const = 0; + virtual cv::Size windowSize(int &dx, int &dy) const = 0; // OpenCV compatibility virtual int numFeatures() const = 0; diff --git a/openbr/plugins/classification/boostedforest.cpp b/openbr/plugins/classification/boostedforest.cpp index e022777..98a0fd4 100644 --- a/openbr/plugins/classification/boostedforest.cpp +++ b/openbr/plugins/classification/boostedforest.cpp @@ -167,9 +167,9 @@ class BoostedForestClassifier : public Classifier return dst; } - Size windowSize() const + Size windowSize(int &dx, int &dy) const { - return representation->preWindowSize(); + return representation->windowSize(dx, dy); } void read(const FileNode &node) diff --git a/openbr/plugins/classification/cascade.cpp b/openbr/plugins/classification/cascade.cpp index 89cb6fa..d5d73fb 100644 --- a/openbr/plugins/classification/cascade.cpp +++ b/openbr/plugins/classification/cascade.cpp @@ -186,9 +186,9 @@ class CascadeClassifier : public Classifier return stages.first()->preprocess(image); } - Size windowSize() const + Size windowSize(int &dx, int &dy) const { - return stages.first()->windowSize(); + return stages.first()->windowSize(dx, dy); } void read(const FileNode &node) diff --git a/openbr/plugins/imgproc/slidingwindow.cpp b/openbr/plugins/imgproc/slidingwindow.cpp index 7507650..e36b9b8 100644 --- a/openbr/plugins/imgproc/slidingwindow.cpp +++ b/openbr/plugins/imgproc/slidingwindow.cpp @@ -99,7 +99,8 @@ class SlidingWindowTransform : public MetaTransform Mat imageBuffer(m.rows + 1, m.cols + 1, CV_8U); for (double factor = 1; ; factor *= scaleFactor) { - Size originalWindowSize(24, 24); + 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)); @@ -120,7 +121,7 @@ class SlidingWindowTransform : public MetaTransform 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(25,25))).clone(); + Mat window = repImage(Rect(Point(x, y), Size(originalWindowSize.width + dx, originalWindowSize.height + dy))).clone(); float gypWeight; int result = classifier->classify(window, gypWeight); @@ -173,7 +174,7 @@ class SlidingWindowTransform : public MetaTransform { (void) stream; - QString path = Globals->sdkPath + "/share/openbr/models/openbrcascades/" + cascadeDir; + QString path = Globals->sdkPath + "/share/openbr/models/openbrcascades/" + model; QtUtils::touchDir(QDir(path)); QString filename = path + "/cascade.xml"; diff --git a/openbr/plugins/representation/haar.cpp b/openbr/plugins/representation/haar.cpp index 8f612e7..6c1d3a7 100644 --- a/openbr/plugins/representation/haar.cpp +++ b/openbr/plugins/representation/haar.cpp @@ -89,8 +89,7 @@ class HaarRepresentation : public Representation } int numFeatures() const { return features.size(); } - Size preWindowSize() const { return Size(winWidth, winHeight); } - Size postWindowSize() const { return Size(winWidth + 1, winHeight + 1); } + Size windowSize(int &dx, int &dy) const { dx = dy = 1; return Size(winWidth, winHeight); } int maxCatCount() const { return 0; } struct Feature diff --git a/openbr/plugins/representation/mblbp.cpp b/openbr/plugins/representation/mblbp.cpp index f58dd2a..2ef19f3 100644 --- a/openbr/plugins/representation/mblbp.cpp +++ b/openbr/plugins/representation/mblbp.cpp @@ -58,9 +58,8 @@ class MBLBPRepresentation : public Representation return result; } + Size windowSize(int &dx, int &dy) const { dx = dy = 1; return Size(winWidth, winHeight); } int numFeatures() const { return features.size(); } - Size preWindowSize() const { return Size(winWidth, winHeight); } - Size postWindowSize() const { return Size(winWidth + 1, winHeight + 1); } int maxCatCount() const { return 256; } struct Feature @@ -77,16 +76,28 @@ class MBLBPRepresentation : public Representation 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); - CV_SUM_OFFSETS( p[0], p[1], p[4], p[5], tr, offset ) + calcOffset(p[0], p[1], p[4], p[5], tr, offset); tr.x += 2*rect.width; - CV_SUM_OFFSETS( p[2], p[3], p[6], p[7], tr, offset ) + calcOffset(p[2], p[3], p[6], p[7], tr, offset); tr.y +=2*rect.height; - CV_SUM_OFFSETS( p[10], p[11], p[14], p[15], tr, offset ) + calcOffset(p[10], p[11], p[14], p[15], tr, offset); tr.x -= 2*rect.width; - CV_SUM_OFFSETS( p[8], p[9], p[12], p[13], tr, offset ) + calcOffset(p[8], p[9], p[12], p[13], tr, offset); } inline uchar MBLBPRepresentation::Feature::calc(const Mat &img) const