Commit 65a1c5ee037504176fda5c26aabaab0ea47ec8f2

Authored by Scott Klum
1 parent 33ca8bf2

Simplified barycentric logic

openbr/plugins/landmarks.cpp
... ... @@ -21,11 +21,7 @@ class ProcrustesTransform : public Transform
21 21 {
22 22 Q_OBJECT
23 23  
24   - Q_PROPERTY(float normReduction READ get_normReduction WRITE set_normReduction RESET reset_normReduction STORED false)
25   - Q_PROPERTY(bool center READ get_center WRITE set_center RESET reset_center STORED false)
26 24 Q_PROPERTY(bool warp READ get_warp WRITE set_warp RESET reset_warp STORED false)
27   - BR_PROPERTY(float, normReduction, 1)
28   - BR_PROPERTY(bool, center, true)
29 25 BR_PROPERTY(bool, warp, true)
30 26  
31 27 Eigen::MatrixXf meanShape;
... ... @@ -53,10 +49,7 @@ class ProcrustesTransform : public Transform
53 49  
54 50 // Remove scale component
55 51 float norm = cv::norm(OpenCVUtils::toPoints(points).toVector().toStdVector());
56   - for (int i = 0; i < points.size(); i++) {
57   - points[i] /= (norm/normReduction);
58   - if (center) points[i] += QPointF(datum.m().cols/2,datum.m().rows/2);
59   - }
  52 + for (int i = 0; i < points.size(); i++) points[i] /= norm;
60 53  
61 54 normalizedPoints.append(points);
62 55 }
... ... @@ -106,8 +99,7 @@ class ProcrustesTransform : public Transform
106 99 Eigen::MatrixXf srcMat(points.size(), 2);
107 100 float norm = cv::norm(OpenCVUtils::toPoints(points).toVector().toStdVector());
108 101 for (int i = 0; i < points.size(); i++) {
109   - points[i] /= (norm/normReduction);
110   - if (center) points[i] += QPointF(src.m().cols/2,src.m().rows/2);
  102 + points[i] /= norm;
111 103 srcMat(i,0) = points[i].x();
112 104 srcMat(i,1) = points[i].y();
113 105 }
... ... @@ -120,6 +112,7 @@ class ProcrustesTransform : public Transform
120 112 if (warp) {
121 113 Eigen::MatrixXf dstMat = srcMat*R;
122 114 for (int i = 0; i < dstMat.rows(); i++) {
  115 + qDebug() << QPointF(dstMat(i,0),dstMat(i,1));
123 116 dst.file.appendPoint(QPointF(dstMat(i,0),dstMat(i,1)));
124 117 }
125 118 }
... ... @@ -156,10 +149,14 @@ class DelaunayTransform : public UntrainableTransform
156 149 {
157 150 Q_OBJECT
158 151  
159   - Q_PROPERTY(float normReduction READ get_normReduction WRITE set_normReduction RESET reset_normReduction STORED false)
  152 + Q_PROPERTY(float scaleFactor READ get_scaleFactor WRITE set_scaleFactor RESET reset_scaleFactor STORED false)
  153 + Q_PROPERTY(QString widthCrop READ get_widthCrop WRITE set_widthCrop RESET reset_widthCrop STORED false)
  154 + Q_PROPERTY(QString heightCrop READ get_heightCrop WRITE set_heightCrop RESET reset_heightCrop STORED false)
160 155 Q_PROPERTY(bool warp READ get_warp WRITE set_warp RESET reset_warp STORED false)
161 156 Q_PROPERTY(bool draw READ get_draw WRITE set_draw RESET reset_draw STORED false)
162   - BR_PROPERTY(float, normReduction, 1)
  157 + BR_PROPERTY(float, scaleFactor, 1)
  158 + BR_PROPERTY(QString, widthCrop, QString())
  159 + BR_PROPERTY(QString, heightCrop, QString())
163 160 BR_PROPERTY(bool, warp, true)
164 161 BR_PROPERTY(bool, draw, false)
165 162  
... ... @@ -184,6 +181,7 @@ class DelaunayTransform : public UntrainableTransform
184 181  
185 182 for (int i = 0; i < points.size(); i++) {
186 183 if (points[i].x() < 0 || points[i].y() < 0 || points[i].y() >= src.m().rows || points[i].x() >= src.m().cols) {
  184 + qDebug() << points[i] << src.m().rows << src.m().cols;
187 185 dst = src;
188 186 qWarning("Delauney triangulation failed because points lie on boundary.");
189 187 return;
... ... @@ -205,7 +203,7 @@ class DelaunayTransform : public UntrainableTransform
205 203  
206 204 bool inside = true;
207 205 for (int j = 0; j < 3; j++) {
208   - if (pt[j].x > src.m().cols || pt[j].y > src.m().rows || pt[j].x <= 0 || pt[j].y <= 0) inside = false;
  206 + if (pt[j].x > src.m().cols || pt[j].y > src.m().rows || pt[j].x < 0 || pt[j].y < 0) inside = false;
209 207 }
210 208  
211 209 if (inside) validTriangles.append(QList<Point>()<< pt[0] << pt[1] << pt[2]);
... ... @@ -221,8 +219,6 @@ class DelaunayTransform : public UntrainableTransform
221 219 }
222 220 }
223 221  
224   - bool warp = true;
225   -
226 222 if (warp) {
227 223 Eigen::MatrixXf R(2,2);
228 224 R(0,0) = src.file.get<float>("Procrustes_0_0");
... ... @@ -243,9 +239,9 @@ class DelaunayTransform : public UntrainableTransform
243 239 for (int i = 0; i < validTriangles.size(); i++) {
244 240 Eigen::MatrixXf srcMat(validTriangles[i].size(), 2);
245 241  
246   - for (int j = 0; j < validTriangles[i].size(); j++) {
247   - srcMat(j,0) = (validTriangles[i][j].x-mean[0])/(norm/normReduction)+src.m().cols/2;
248   - srcMat(j,1) = (validTriangles[i][j].y-mean[1])/(norm/normReduction)+src.m().rows/2;
  242 + for (int j = 0; j < 3; j++) {
  243 + srcMat(j,0) = (validTriangles[i][j].x-mean[0])/norm;
  244 + srcMat(j,1) = (validTriangles[i][j].y-mean[1])/norm;
249 245 }
250 246  
251 247 Eigen::MatrixXf dstMat = srcMat*R;
... ... @@ -255,8 +251,10 @@ class DelaunayTransform : public UntrainableTransform
255 251  
256 252 Point2f dstPoints[3];
257 253 for (int j = 0; j < 3; j++) {
258   - dstPoints[j] = Point2f(dstMat(j,0),dstMat(j,1));
259   - mappedPoints.append(dstPoints[j]);
  254 + // Scale and shift destination points
  255 + Point2f warpedPoint = Point2f(dstMat(j,0)*scaleFactor+src.m().cols/2,dstMat(j,1)*scaleFactor+src.m().rows/2);
  256 + dstPoints[j] = warpedPoint;
  257 + mappedPoints.append(warpedPoint);
260 258 }
261 259  
262 260 Mat buffer(src.m().rows,src.m().cols,src.m().type());
... ... @@ -274,7 +272,7 @@ class DelaunayTransform : public UntrainableTransform
274 272  
275 273 Mat output(src.m().rows,src.m().cols,src.m().type());
276 274  
277   - // Optimize
  275 + // Optimization needed
278 276 if (i > 0) {
279 277 Mat overlap;
280 278 bitwise_and(dst.m(),mask,overlap);
... ... @@ -294,10 +292,10 @@ class DelaunayTransform : public UntrainableTransform
294 292  
295 293 Rect boundingBox = boundingRect(mappedPoints.toVector().toStdVector());
296 294  
297   - boundingBox.x += 0; //boundingBox.width * .05;
298   - boundingBox.y += boundingBox.height * .1; // 0.025 for nose, .05 for mouth, .10 for brow
299   - boundingBox.width *= 1;//.975;
300   - boundingBox.height *= .80; // .975 for nose, .95 for mouth, .925 for brow
  295 + boundingBox.x += boundingBox.width * QtUtils::toPoint(widthCrop).x();
  296 + boundingBox.y += boundingBox.height * QtUtils::toPoint(heightCrop).x();
  297 + boundingBox.width *= 1-QtUtils::toPoint(widthCrop).y();
  298 + boundingBox.height *= 1-QtUtils::toPoint(heightCrop).y();
301 299  
302 300 qDebug() << boundingBox;
303 301  
... ...
openbr/plugins/regions.cpp
... ... @@ -230,6 +230,7 @@ class RectFromPointsTransform : public UntrainableTransform
230 230 if (src.file.points()[index].y() > maxY) maxY = src.file.points()[index].y();
231 231 points.append(src.file.points()[index]);
232 232 }
  233 + else qFatal("Incorrect indices");
233 234 }
234 235  
235 236 double width = maxX-minX;
... ...