Commit c11e9eec8ec440f8fb664ce850e1fd9195740896
Committed by
Brendan Klare
1 parent
17e0d318
Factor out multi-template per detection into new transform
Showing
1 changed file
with
45 additions
and
7 deletions
openbr/plugins/slidingwindow.cpp
| ... | ... | @@ -38,7 +38,7 @@ static float getAspectRatio(const TemplateList &data) |
| 38 | 38 | * Discards negative detections. |
| 39 | 39 | * \author Austin Blanton \cite imaus10 |
| 40 | 40 | */ |
| 41 | -class SlidingWindowTransform : public MetaTransform | |
| 41 | +class SlidingWindowTransform : public Transform | |
| 42 | 42 | { |
| 43 | 43 | Q_OBJECT |
| 44 | 44 | Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false) |
| ... | ... | @@ -93,6 +93,14 @@ private: |
| 93 | 93 | |
| 94 | 94 | void project(const Template &src, Template &dst) const |
| 95 | 95 | { |
| 96 | + float scale = src.file.get<float>("scale", 1); | |
| 97 | + projectHelp(src, dst, windowWidth, windowHeight, scale); | |
| 98 | + } | |
| 99 | + | |
| 100 | + protected: | |
| 101 | + void projectHelp(const Template &src, Template &dst, int windowWidth, int windowHeight, float scale = 1) const | |
| 102 | + { | |
| 103 | + | |
| 96 | 104 | dst = src; |
| 97 | 105 | if (skipProject) { |
| 98 | 106 | dst = src; |
| ... | ... | @@ -100,11 +108,10 @@ private: |
| 100 | 108 | } |
| 101 | 109 | |
| 102 | 110 | dst.file.clearRects(); |
| 103 | - float scale = src.file.get<float>("scale", 1); | |
| 104 | 111 | Template windowTemplate(src.file, src); |
| 105 | 112 | QList<float> confidences = dst.file.getList<float>("Confidences", QList<float>()); |
| 106 | - for (double y = 0; y + windowHeight < src.m().rows; y += stepSize) { | |
| 107 | - for (double x = 0; x + windowWidth < src.m().cols; x += stepSize) { | |
| 113 | + for (float y = 0; y + windowHeight < src.m().rows; y += windowHeight*stepFraction) { | |
| 114 | + for (float x = 0; x + windowWidth < src.m().cols; x += windowWidth*stepFraction) { | |
| 108 | 115 | Mat windowMat(src, Rect(x, y, windowWidth, windowHeight)); |
| 109 | 116 | windowTemplate.replace(0,windowMat); |
| 110 | 117 | Template detect; |
| ... | ... | @@ -113,7 +120,7 @@ private: |
| 113 | 120 | |
| 114 | 121 | // the result will be in the Label |
| 115 | 122 | if (conf > threshold) { |
| 116 | - dst.file.appendRect(QRectF((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale)); | |
| 123 | + dst.file.appendRect(QRectF(x*scale, y*scale, windowWidth*scale, windowHeight*scale)); | |
| 117 | 124 | confidences.append(conf); |
| 118 | 125 | if (takeFirst) |
| 119 | 126 | return; |
| ... | ... | @@ -136,7 +143,8 @@ class IntegralSlidingWindowTransform : public SlidingWindowTransform |
| 136 | 143 | { |
| 137 | 144 | Q_OBJECT |
| 138 | 145 | |
| 139 | - void project(const TemplateList &src, TemplateList &dst) const | |
| 146 | + private: | |
| 147 | + void project(const Template &src, Template &dst) const | |
| 140 | 148 | { |
| 141 | 149 | // TODO: call SlidingWindowTransform::project on multiple scales |
| 142 | 150 | SlidingWindowTransform::projectHelp(src, dst, 24, 24); |
| ... | ... | @@ -212,7 +220,7 @@ static TemplateList cropTrainingSamples(const TemplateList &data, const float as |
| 212 | 220 | * \brief . |
| 213 | 221 | * \author Austin Blanton \cite imaus10 |
| 214 | 222 | */ |
| 215 | -class BuildScalesTransform : public MetaTransform | |
| 223 | +class BuildScalesTransform : public Transform | |
| 216 | 224 | { |
| 217 | 225 | Q_OBJECT |
| 218 | 226 | Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false) |
| ... | ... | @@ -486,6 +494,36 @@ private: |
| 486 | 494 | |
| 487 | 495 | BR_REGISTER(Transform, ConsolidateDetectionsTransform) |
| 488 | 496 | |
| 497 | +/*! | |
| 498 | + * \ingroup transforms | |
| 499 | + * \brief For each rectangle bounding box in TemplateList, a new | |
| 500 | + * template is created. | |
| 501 | + * \author Brendan Klare \cite bklare | |
| 502 | + */ | |
| 503 | +class RectsToTemplatesTransform : public UntrainableMetaTransform | |
| 504 | +{ | |
| 505 | + Q_OBJECT | |
| 506 | + | |
| 507 | +private: | |
| 508 | + void project(const Template &src, Template &dst) const | |
| 509 | + { | |
| 510 | + Template tOut(src.file); | |
| 511 | + QList<float> confidences = src.file.getList<float>("Confidences"); | |
| 512 | + QList<QRectF> rects = src.file.rects(); | |
| 513 | + for (int i = 0; i < rects.size(); i++) { | |
| 514 | + Mat m(src, OpenCVUtils::toRect(rects[i])); | |
| 515 | + Template t(src.file, m); | |
| 516 | + t.file.set("Confidence", confidences[i]); | |
| 517 | + t.file.clearRects(); | |
| 518 | + tOut << t; | |
| 519 | + } | |
| 520 | + dst = tOut; | |
| 521 | + } | |
| 522 | +}; | |
| 523 | + | |
| 524 | +BR_REGISTER(Transform, RectsToTemplatesTransform) | |
| 525 | + | |
| 526 | + | |
| 489 | 527 | } // namespace br |
| 490 | 528 | |
| 491 | 529 | #include "slidingwindow.moc" | ... | ... |