From 40f8c19a2bc41f4b0993db43cb67e2407c9df7c4 Mon Sep 17 00:00:00 2001 From: Brendan Klare Date: Tue, 15 Oct 2013 17:42:41 -0400 Subject: [PATCH] Ensure training sample is within image boundaries --- openbr/plugins/regions.cpp | 30 ++++++++++++++++++++++++++++++ openbr/plugins/slidingwindow.cpp | 5 +++++ 2 files changed, 35 insertions(+), 0 deletions(-) diff --git a/openbr/plugins/regions.cpp b/openbr/plugins/regions.cpp index bd850aa..4216d94 100644 --- a/openbr/plugins/regions.cpp +++ b/openbr/plugins/regions.cpp @@ -54,6 +54,36 @@ class RectRegionsTransform : public UntrainableTransform } }; +/*! + * \ingroup transforms + * \brief Subdivide matrix into a fixed number of rectangular subregions. + * \author Brendan Klare \cite bklare + */ +class FixedRegionsTransform : public UntrainableTransform +{ + Q_OBJECT + Q_PROPERTY(int nHorizontal READ get_nHorizontal WRITE set_nHorizontal RESET reset_nHorizontal STORED false) + Q_PROPERTY(int nVertical READ get_nVertical WRITE set_nVertical RESET reset_nVertical STORED false) + Q_PROPERTY(float widthScaleStep READ get_widthScaleStep WRITE set_widthScaleStep RESET reset_widthScaleStep STORED false) + Q_PROPERTY(float heightScaleStep READ get_heightScaleStep WRITE set_heightScaleStep RESET reset_heightScaleStep STORED false) + BR_PROPERTY(int, nHorizontal, 5) + BR_PROPERTY(int, nVertical, 5) + BR_PROPERTY(float, widthScaleStep, .5) + BR_PROPERTY(float, heightScaleStep, .5) + + void project(const Template &src, Template &dst) const + { + const int widthStep = this->widthStep == -1 ? width : this->widthStep; + const int heightStep = this->heightStep == -1 ? height : this->heightStep; + const Mat &m = src; + const int xMax = m.cols - width; + const int yMax = m.rows - height; + for (int x=0; x <= xMax; x += widthStep) + for (int y=0; y <= yMax; y += heightStep) + dst += m(Rect(x, y, width, height)); + } +}; + BR_REGISTER(Transform, RectRegionsTransform) /*! diff --git a/openbr/plugins/slidingwindow.cpp b/openbr/plugins/slidingwindow.cpp index 7e22db5..db60d08 100644 --- a/openbr/plugins/slidingwindow.cpp +++ b/openbr/plugins/slidingwindow.cpp @@ -49,6 +49,11 @@ private: QList posRects = OpenCVUtils::toRects(tmpl.file.rects()); QList negRects; 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; + } + + QString buffer; Template pos(tmpl.file, Mat(tmpl, posRect)); full += pos; -- libgit2 0.21.4