Commit 097cfe2070f39d0f2e7be36bc907617b1dd877a1

Authored by Josh Klontz
1 parent 50f7eba5

remove multiscale

openbr/plugins/imgproc/multiscale.cpp deleted
1   -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2   - * Copyright 2012 The MITRE Corporation *
3   - * *
4   - * Licensed under the Apache License, Version 2.0 (the "License"); *
5   - * you may not use this file except in compliance with the License. *
6   - * You may obtain a copy of the License at *
7   - * *
8   - * http://www.apache.org/licenses/LICENSE-2.0 *
9   - * *
10   - * Unless required by applicable law or agreed to in writing, software *
11   - * distributed under the License is distributed on an "AS IS" BASIS, *
12   - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13   - * See the License for the specific language governing permissions and *
14   - * limitations under the License. *
15   - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
16   -
17   -#include <opencv2/imgproc/imgproc.hpp>
18   -
19   -#include <openbr/plugins/openbr_internal.h>
20   -#include <openbr/core/opencvutils.h>
21   -
22   -using namespace cv;
23   -
24   -namespace br
25   -{
26   -
27   -// Find avg aspect ratio
28   -static float getAspectRatio(const TemplateList &data)
29   -{
30   - double tempRatio = 0;
31   - int ratioCnt = 0;
32   -
33   - foreach (const Template &tmpl, data) {
34   - QList<Rect> posRects = OpenCVUtils::toRects(tmpl.file.rects());
35   - foreach (const Rect &posRect, posRects) {
36   - if (posRect.x + posRect.width >= tmpl.m().cols || posRect.y + posRect.height >= tmpl.m().rows || posRect.x < 0 || posRect.y < 0) {
37   - continue;
38   - }
39   - tempRatio += (float)posRect.width / (float)posRect.height;
40   - ratioCnt += 1;
41   - }
42   - }
43   - return tempRatio / (double)ratioCnt;
44   -}
45   -
46   -static TemplateList cropTrainingSamples(const TemplateList &data, const float aspectRatio, const int minSize = 32, const float maxOverlap = 0.5, const int negToPosRatio = 1)
47   -{
48   - TemplateList result;
49   - foreach (const Template &tmpl, data) {
50   - QList<Rect> posRects = OpenCVUtils::toRects(tmpl.file.rects());
51   - QList<Rect> negRects;
52   - for (int i=0; i<posRects.size(); i++) {
53   - Rect &posRect = posRects[i];
54   -
55   - // Adjust for training samples that have different aspect ratios
56   - const int diff = int(posRect.height * aspectRatio) - posRect.width;
57   - posRect.x -= diff / 2;
58   - posRect.width += diff;
59   -
60   - // Ignore samples larger than the image
61   - if ((posRect.x + posRect.width >= tmpl.m().cols) ||
62   - (posRect.y + posRect.height >= tmpl.m().rows) ||
63   - (posRect.x < 0) ||
64   - (posRect.y < 0))
65   - continue;
66   -
67   - result += Template(tmpl.file, Mat(tmpl, posRect));
68   - result.last().file.set("Label", QString("pos"));
69   -
70   - // Add random negative samples
71   - Mat m = tmpl.m();
72   - int sample = 0;
73   - while (sample < negToPosRatio) {
74   - const int x = rand() % m.cols;
75   - const int y = rand() % m.rows;
76   - const int maxWidth = m.cols - x;
77   - const int maxHeight = m.rows - y;
78   - if (maxWidth <= minSize || maxHeight <= minSize)
79   - continue;
80   -
81   - int height;
82   - int width;
83   - if (aspectRatio > (float) maxWidth / (float) maxHeight) {
84   - width = rand() % (maxWidth - minSize) + minSize;
85   - height = qRound(width / aspectRatio);
86   - } else {
87   - height = rand() % (maxHeight - minSize) + minSize;
88   - width = qRound(height * aspectRatio);
89   - }
90   - Rect negRect(x, y, width, height);
91   -
92   - // The negative samples cannot overlap the positive samples at
93   - // all, but they may partially overlap with other negatives.
94   - if (OpenCVUtils::overlaps(posRects, negRect, 0) ||
95   - OpenCVUtils::overlaps(negRects, negRect, maxOverlap))
96   - continue;
97   -
98   - result += Template(tmpl.file, Mat(tmpl, negRect));
99   - result.last().file.set("Label", QString("neg"));
100   - sample++;
101   - }
102   - }
103   - }
104   -
105   - return result;
106   -}
107   -
108   -/*!
109   - * \ingroup transforms
110   - * \brief DOCUMENT ME
111   - * \author Austin Blanton \cite imaus10
112   - */
113   -class BuildScalesTransform : public Transform
114   -{
115   - Q_OBJECT
116   - Q_PROPERTY(br::Transform *transform READ get_transform WRITE set_transform RESET reset_transform STORED false)
117   - Q_PROPERTY(double scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false)
118   - Q_PROPERTY(bool takeLargestScale READ get_takeLargestScale WRITE set_takeLargestScale RESET reset_takeLargestScale STORED false)
119   - Q_PROPERTY(int windowWidth READ get_windowWidth WRITE set_windowWidth RESET reset_windowWidth STORED false)
120   - Q_PROPERTY(int negToPosRatio READ get_negToPosRatio WRITE set_negToPosRatio RESET reset_negToPosRatio STORED false)
121   - Q_PROPERTY(int minSize READ get_minSize WRITE set_minSize RESET reset_minSize STORED false)
122   - Q_PROPERTY(double maxOverlap READ get_maxOverlap WRITE set_maxOverlap RESET reset_maxOverlap STORED false)
123   - Q_PROPERTY(float minScale READ get_minScale WRITE set_minScale RESET reset_minScale STORED false)
124   - BR_PROPERTY(br::Transform *, transform, NULL)
125   - BR_PROPERTY(double, scaleFactor, 0.75)
126   - BR_PROPERTY(bool, takeLargestScale, false)
127   - BR_PROPERTY(int, windowWidth, 24)
128   - BR_PROPERTY(int, negToPosRatio, 1)
129   - BR_PROPERTY(int, minSize, 8)
130   - BR_PROPERTY(double, maxOverlap, 0)
131   - BR_PROPERTY(float, minScale, 1.0)
132   -
133   -private:
134   - float aspectRatio;
135   - int windowHeight;
136   - bool skipProject;
137   -
138   - void train(const TemplateList &data)
139   - {
140   - skipProject = true;
141   - aspectRatio = getAspectRatio(data);
142   - windowHeight = qRound(windowWidth / aspectRatio);
143   - if (transform->trainable) {
144   - TemplateList full;
145   - foreach (const Template &roi, cropTrainingSamples(data, aspectRatio, minSize, maxOverlap, negToPosRatio)) {
146   - Mat resized;
147   - resize(roi, resized, Size(windowWidth, windowHeight));
148   - full += Template(roi.file, resized);
149   - }
150   - full.first().file.set("aspectRatio", aspectRatio);
151   - transform->train(full);
152   - }
153   - }
154   -
155   - void project(const Template &src, Template &dst) const
156   - {
157   - dst = src;
158   - if (skipProject) {
159   - dst = src;
160   - return;
161   - }
162   -
163   - int rows = src.m().rows;
164   - int cols = src.m().cols;
165   - int windowHeight = (int) qRound((float) windowWidth / aspectRatio);
166   -
167   - float startScale;
168   - if ((cols / rows) > aspectRatio)
169   - startScale = qRound((float) rows / (float) windowHeight);
170   - else
171   - startScale = qRound((float) cols / (float) windowWidth);
172   -
173   - for (float scale = startScale; scale >= minScale; scale -= (1.0 - scaleFactor)) {
174   - Template scaleImg(dst.file, Mat());
175   - scaleImg.file.set("scale", scale);
176   - resize(src, scaleImg, Size(qRound(cols / scale), qRound(rows / scale)));
177   - transform->project(scaleImg, dst);
178   - if (takeLargestScale && !dst.file.rects().empty())
179   - return;
180   - }
181   - }
182   -
183   - void store(QDataStream &stream) const
184   - {
185   - transform->store(stream);
186   - stream << aspectRatio << windowHeight;
187   - }
188   - void load(QDataStream &stream)
189   - {
190   - transform->load(stream);
191   - stream >> aspectRatio >> windowHeight;
192   - }
193   -};
194   -
195   -BR_REGISTER(Transform, BuildScalesTransform)
196   -
197   -} // namespace br
198   -
199   -#include "imgproc/multiscale.moc"