Commit 3635375bc81d1ef5e69a74d69c08b0b9894f1aae

Authored by Keyur Patel
1 parent 2cb82045

Added flag to random rects to allow for the random rects to be of the same size …

…and ensure the patches don't contain black pixels
openbr/plugins/metadata/randomrects.cpp
@@ -4,6 +4,9 @@ @@ -4,6 +4,9 @@
4 4
5 #include <openbr/plugins/openbr_internal.h> 5 #include <openbr/plugins/openbr_internal.h>
6 #include <openbr/core/opencvutils.h> 6 #include <openbr/core/opencvutils.h>
  7 +#include "opencv2/imgproc/imgproc.hpp"
  8 +
  9 +using namespace cv;
7 10
8 namespace br 11 namespace br
9 { 12 {
@@ -18,8 +21,15 @@ class RandomRectsTransform : public UntrainableMetaTransform @@ -18,8 +21,15 @@ class RandomRectsTransform : public UntrainableMetaTransform
18 Q_OBJECT 21 Q_OBJECT
19 Q_PROPERTY(int numRects READ get_numRects WRITE set_numRects RESET reset_numRects STORED false) 22 Q_PROPERTY(int numRects READ get_numRects WRITE set_numRects RESET reset_numRects STORED false)
20 Q_PROPERTY(int minSize READ get_minSize WRITE set_minSize RESET reset_minSize STORED false) 23 Q_PROPERTY(int minSize READ get_minSize WRITE set_minSize RESET reset_minSize STORED false)
  24 + Q_PROPERTY(bool sameSize READ get_sameSize WRITE set_sameSize RESET reset_sameSize STORED false)
  25 + Q_PROPERTY(bool filterDarkPatches READ get_filterDarkPatches WRITE set_filterDarkPatches RESET reset_filterDarkPatches STORED false)
  26 + Q_PROPERTY(float bgFgThresh READ get_bgFgThresh WRITE set_bgFgThresh RESET reset_bgFgThresh STORED false)
  27 +
21 BR_PROPERTY(int, numRects, 135) 28 BR_PROPERTY(int, numRects, 135)
22 BR_PROPERTY(int, minSize, 24) 29 BR_PROPERTY(int, minSize, 24)
  30 + BR_PROPERTY(bool, sameSize, false)
  31 + BR_PROPERTY(bool, filterDarkPatches, false)
  32 + BR_PROPERTY(float, bgFgThresh, 0.75)
23 33
24 void project(const Template &, Template &) const 34 void project(const Template &, Template &) const
25 { 35 {
@@ -28,13 +38,28 @@ class RandomRectsTransform : public UntrainableMetaTransform @@ -28,13 +38,28 @@ class RandomRectsTransform : public UntrainableMetaTransform
28 38
29 void project(const TemplateList &src, TemplateList &dst) const 39 void project(const TemplateList &src, TemplateList &dst) const
30 { 40 {
  41 + int size = minSize;
31 foreach (const Template &t, src) { 42 foreach (const Template &t, src) {
32 int maxSize = std::min(t.m().rows, t.m().cols); 43 int maxSize = std::min(t.m().rows, t.m().cols);
33 for (int i = 0; i < numRects; i++) { 44 for (int i = 0; i < numRects; i++) {
34 - int size = (rand() % (maxSize - minSize)) + minSize; 45 +
  46 + if (!sameSize){
  47 + size = (rand() % (maxSize - minSize)) + minSize;
  48 + }
  49 +
35 int x = rand() % (t.m().cols - size); 50 int x = rand() % (t.m().cols - size);
36 int y = rand() % (t.m().rows - size); 51 int y = rand() % (t.m().rows - size);
37 52
  53 + if (filterDarkPatches){
  54 + Mat patch;
  55 + t.m()(cv::Rect(x,y,size,size)).copyTo(patch);
  56 + cv::threshold(patch,patch,5,1,cv::THRESH_BINARY);
  57 + Scalar sumForeground = sum(patch);
  58 + float bgFgRatio = sumForeground[0] / float(minSize * minSize);
  59 + if (bgFgRatio < bgFgThresh)
  60 + continue;
  61 + }
  62 +
38 Template out(t.file, t.m()); 63 Template out(t.file, t.m());
39 out.file.clearRects(); 64 out.file.clearRects();
40 out.file.appendRect(QRect(x,y,size,size)); 65 out.file.appendRect(QRect(x,y,size,size));