Commit 40f8c19a2bc41f4b0993db43cb67e2407c9df7c4

Authored by Brendan Klare
1 parent e974d909

Ensure training sample is within image boundaries

openbr/plugins/regions.cpp
... ... @@ -54,6 +54,36 @@ class RectRegionsTransform : public UntrainableTransform
54 54 }
55 55 };
56 56  
  57 +/*!
  58 + * \ingroup transforms
  59 + * \brief Subdivide matrix into a fixed number of rectangular subregions.
  60 + * \author Brendan Klare \cite bklare
  61 + */
  62 +class FixedRegionsTransform : public UntrainableTransform
  63 +{
  64 + Q_OBJECT
  65 + Q_PROPERTY(int nHorizontal READ get_nHorizontal WRITE set_nHorizontal RESET reset_nHorizontal STORED false)
  66 + Q_PROPERTY(int nVertical READ get_nVertical WRITE set_nVertical RESET reset_nVertical STORED false)
  67 + Q_PROPERTY(float widthScaleStep READ get_widthScaleStep WRITE set_widthScaleStep RESET reset_widthScaleStep STORED false)
  68 + Q_PROPERTY(float heightScaleStep READ get_heightScaleStep WRITE set_heightScaleStep RESET reset_heightScaleStep STORED false)
  69 + BR_PROPERTY(int, nHorizontal, 5)
  70 + BR_PROPERTY(int, nVertical, 5)
  71 + BR_PROPERTY(float, widthScaleStep, .5)
  72 + BR_PROPERTY(float, heightScaleStep, .5)
  73 +
  74 + void project(const Template &src, Template &dst) const
  75 + {
  76 + const int widthStep = this->widthStep == -1 ? width : this->widthStep;
  77 + const int heightStep = this->heightStep == -1 ? height : this->heightStep;
  78 + const Mat &m = src;
  79 + const int xMax = m.cols - width;
  80 + const int yMax = m.rows - height;
  81 + for (int x=0; x <= xMax; x += widthStep)
  82 + for (int y=0; y <= yMax; y += heightStep)
  83 + dst += m(Rect(x, y, width, height));
  84 + }
  85 +};
  86 +
57 87 BR_REGISTER(Transform, RectRegionsTransform)
58 88  
59 89 /*!
... ...
openbr/plugins/slidingwindow.cpp
... ... @@ -49,6 +49,11 @@ private:
49 49 QList<Rect> posRects = OpenCVUtils::toRects(tmpl.file.rects());
50 50 QList<Rect> negRects;
51 51 foreach (const Rect &posRect, posRects) {
  52 + if (posRect.x + posRect.width >= tmpl.m().cols || posRect.y + posRect.height >= tmpl.m().rows || posRect.x < 0 || posRect.y < 0) {
  53 + continue;
  54 + }
  55 +
  56 + QString buffer;
52 57 Template pos(tmpl.file, Mat(tmpl, posRect));
53 58 full += pos;
54 59  
... ...