Commit 35767e9f452622d189bcefeedb655decc502b7b9

Authored by Josh Klontz
1 parent c1ace94d

basic code cleanup

openbr/plugins/imgproc/slidingwindow.cpp
... ... @@ -91,50 +91,44 @@ class SlidingWindowTransform : public MetaTransform
91 91 continue;
92 92 }
93 93  
  94 + // SlidingWindow assumes that all matricies in a template represent
  95 + // different channels of the same image!
  96 + const Size imageSize = t.m().size();
94 97 const int minSize = t.file.get<int>("MinSize", this->minSize);
95   - Size minObjectSize(minSize, minSize);
96   - Size maxObjectSize;
97   -
98   - Mat m = t.m();
99 98 QList<Rect> rects;
100 99 QList<float> confidences;
101 100  
102   - if (maxObjectSize.height == 0 || maxObjectSize.width == 0)
103   - maxObjectSize = m.size();
  101 + int dx, dy;
  102 + const Size originalWindowSize = classifier->windowSize(&dx, &dy);
104 103  
105 104 for (double factor = 1; ; factor *= scaleFactor) {
106   - int dx, dy;
107   - Size originalWindowSize = classifier->windowSize(&dx, &dy);
108   -
109   - Size windowSize(cvRound(originalWindowSize.width*factor), cvRound(originalWindowSize.height*factor) );
110   - Size scaledImageSize(cvRound(m.cols/factor ), cvRound(m.rows/factor));
111   - Size processingRectSize(scaledImageSize.width - originalWindowSize.width, scaledImageSize.height - originalWindowSize.height);
  105 + const Size windowSize(cvRound(originalWindowSize.width*factor), cvRound(originalWindowSize.height*factor));
  106 + const Size scaledImageSize(cvRound(imageSize.width/factor), cvRound(imageSize.height/factor));
  107 + const Size processingRectSize(scaledImageSize.width - originalWindowSize.width, scaledImageSize.height - originalWindowSize.height);
112 108  
113 109 if (processingRectSize.width <= 0 || processingRectSize.height <= 0)
114 110 break;
115   - if (windowSize.width > maxObjectSize.width || windowSize.height > maxObjectSize.height)
116   - break;
117   - if (windowSize.width < minObjectSize.width || windowSize.height < minObjectSize.height)
  111 + if (windowSize.width < minSize || windowSize.height < minSize)
118 112 continue;
119 113  
120   - Template scaleBuffer(t.file);
121   - for (int i=0; i<t.size(); i++) {
  114 + Template rep(t.file);
  115 + foreach (const Mat &m, t) {
122 116 Mat scaledImage;
123   - resize(t[i], scaledImage, scaledImageSize, 0, 0, CV_INTER_LINEAR);
124   - scaleBuffer.append(scaledImage);
  117 + resize(m, scaledImage, scaledImageSize, 0, 0, CV_INTER_LINEAR);
  118 + rep.append(scaledImage);
125 119 }
  120 + rep = classifier->preprocess(rep);
126 121  
127   - Template rep(t.file);
128   - rep = classifier->preprocess(scaleBuffer);
  122 + // Pre-allocate the window to avoid constructing this every iteration
  123 + Template window(t.file);
  124 + for (int i=0; i<rep.size(); i++)
  125 + window.append(Mat());
129 126  
130   - int step = factor > 2. ? 1 : 2;
  127 + const int step = factor > 2.0 ? 1 : 2;
131 128 for (int y = 0; y < processingRectSize.height; y += step) {
132 129 for (int x = 0; x < processingRectSize.width; x += step) {
133   - Template window(t.file);
134   - for (int i=0; i<rep.size(); i++) {
135   - Mat w = rep[i](Rect(Point(x, y), Size(originalWindowSize.width + dx, originalWindowSize.height + dy))).clone();
136   - window.append(w);
137   - }
  130 + for (int i=0; i<rep.size(); i++)
  131 + window[i] = rep[i](Rect(Point(x, y), Size(originalWindowSize.width + dx, originalWindowSize.height + dy))).clone();
138 132  
139 133 float confidence = 0;
140 134 int result = classifier->classify(window, false, &confidence);
... ... @@ -155,12 +149,12 @@ class SlidingWindowTransform : public MetaTransform
155 149 OpenCVUtils::group(rects, confidences, confidenceThreshold, minNeighbors, eps);
156 150  
157 151 if (!enrollAll && rects.empty()) {
158   - rects.append(Rect(0, 0, m.cols, m.rows));
  152 + rects.append(Rect(0, 0, imageSize.width, imageSize.height));
159 153 confidences.append(-std::numeric_limits<float>::max());
160 154 }
161 155  
162 156 for (int j=0; j<rects.size(); j++) {
163   - Template u(t.file, m);
  157 + Template u = t;
164 158 u.file.set("Confidence", confidences[j]);
165 159 const QRectF rect = OpenCVUtils::fromRect(rects[j]);
166 160 u.file.appendRect(rect);
... ...