From d240b48a3fa0cc4583b3fe8b36ec9e208746138e Mon Sep 17 00:00:00 2001 From: Scott Klum Date: Thu, 11 Jul 2013 21:01:56 -0400 Subject: [PATCH] More updates for procrustes and delaunay --- openbr/core/core.cpp | 1 - openbr/core/plot.cpp | 6 +++--- openbr/plugins/format.cpp | 4 ++-- openbr/plugins/landmarks.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- openbr/plugins/stasm4.cpp | 2 +- 5 files changed, 60 insertions(+), 30 deletions(-) diff --git a/openbr/core/core.cpp b/openbr/core/core.cpp index 383ce07..5f97d70 100644 --- a/openbr/core/core.cpp +++ b/openbr/core/core.cpp @@ -167,7 +167,6 @@ struct AlgorithmCore } const float speed = 1000 * Globals->totalSteps / Globals->startTime.elapsed() / std::max(1, abs(Globals->parallelism)); - qDebug() << Globals->startTime.elapsed(); if (!Globals->quiet && (Globals->totalSteps > 1)) fprintf(stderr, "\rSPEED=%.1e SIZE=%.4g FAILURES=%d/%d \n", speed, totalBytes/totalCount, failureCount, totalCount); diff --git a/openbr/core/plot.cpp b/openbr/core/plot.cpp index aaf7a15..a300a11 100644 --- a/openbr/core/plot.cpp +++ b/openbr/core/plot.cpp @@ -247,8 +247,8 @@ float Evaluate(const Mat &simmat, const Mat &mask, const QString &csv) } // Write FAR/TAR Bar Chart (BC) - lines.append(qPrintable(QString("BC,0.001,%1").arg(QString::number(result = getTAR(operatingPoints, 0.001), 'f', 3)))); - lines.append(qPrintable(QString("BC,0.01,%1").arg(QString::number(getTAR(operatingPoints, 0.01), 'f', 3)))); + lines.append(qPrintable(QString("BC,0.001,%1").arg(QString::number(getTAR(operatingPoints, 0.001), 'f', 3)))); + lines.append(qPrintable(QString("BC,0.01,%1").arg(QString::number(result = getTAR(operatingPoints, 0.01), 'f', 3)))); // Write SD & KDE points = qMin(qMin(Max_Points, genuines.size()), impostors.size()); @@ -287,7 +287,7 @@ float Evaluate(const Mat &simmat, const Mat &mask, const QString &csv) } if (!csv.isEmpty()) QtUtils::writeFile(csv, lines); - qDebug("TAR @ FAR = 0.001: %.3f\nRetrieval Rate @ Rank = %d: %.3f", result, Report_Retrieval, reportRetrievalRate); + qDebug("TAR @ FAR = 0.01: %.3f\nRetrieval Rate @ Rank = %d: %.3f", result, Report_Retrieval, reportRetrievalRate); return result; } diff --git a/openbr/plugins/format.cpp b/openbr/plugins/format.cpp index 1dabd73..7a1294f 100644 --- a/openbr/plugins/format.cpp +++ b/openbr/plugins/format.cpp @@ -659,8 +659,8 @@ class xmlFormat : public Format QDomDocument doc(fileName); QFile f(fileName); - if (!f.open(QIODevice::ReadOnly)) qFatal("Unable to open %s for reading.", qPrintable(file.flat())); - if (!doc.setContent(&f)) qFatal("Unable to parse %s.", qPrintable(file.flat())); + if (!f.open(QIODevice::ReadOnly)) qWarning("Unable to open %s for reading.", qPrintable(file.flat())); + if (!doc.setContent(&f)) qWarning("Unable to parse %s.", qPrintable(file.flat())); f.close(); QDomElement docElem = doc.documentElement(); diff --git a/openbr/plugins/landmarks.cpp b/openbr/plugins/landmarks.cpp index b86ed3a..2a26973 100644 --- a/openbr/plugins/landmarks.cpp +++ b/openbr/plugins/landmarks.cpp @@ -81,6 +81,12 @@ class ProcrustesTransform : public Transform QList points = src.file.points(); QList rects = src.file.rects(); + if (points.empty() || rects.empty()) { + dst = src; + qWarning("Points/rects are empty."); + return; + } + if (rects.size() > 1) qWarning("More than one rect in training data templates."); points.append(rects[0].topLeft()); @@ -95,8 +101,8 @@ class ProcrustesTransform : public Transform Eigen::MatrixXf srcPoints(points.size(), 2); for (int i = 0; i < points.size(); i++) { - srcPoints(i,0) = points[i].x()/(norm/150.)+50; - srcPoints(i,1) = points[i].y()/(norm/150.)+50; + srcPoints(i,0) = points[i].x()/(norm/100.)+50; + srcPoints(i,1) = points[i].y()/(norm/100.)+50; } Eigen::JacobiSVD svd(srcPoints.transpose()*meanShape, Eigen::ComputeThinU | Eigen::ComputeThinV); @@ -140,11 +146,18 @@ class DelaunayTransform : public UntrainableTransform void project(const Template &src, Template &dst) const { + Subdiv2D subdiv(Rect(0,0,src.m().cols,src.m().rows)); QList points = OpenCVUtils::toPoints(src.file.points()); QList rects = src.file.rects(); + if (points.empty() || rects.empty()) { + dst = src; + qWarning("Points/rects are empty."); + return; + } + if (rects.size() > 1) qWarning("More than one rect in training data templates."); points.append(OpenCVUtils::toPoint(rects[0].topLeft())); @@ -176,7 +189,6 @@ class DelaunayTransform : public UntrainableTransform if (inside) { count++; - qDebug() << count << pt[0] << pt[1] << pt[2] << "Area" << contourArea(pt); QList triangleBuffer; @@ -209,38 +221,36 @@ class DelaunayTransform : public UntrainableTransform mean[0] = src.file.get("Procrustes_mean_0"); mean[1] = src.file.get("Procrustes_mean_1"); - qDebug() << mean[0] << mean[1]; - float norm = src.file.get("Procrustes_norm"); - qDebug() << norm; - dst.m() = Mat::zeros(src.m().rows,src.m().cols,src.m().type()); - foreach(const QList &triangle, validTriangles) { - Eigen::MatrixXf srcPoints(triangle.size(), 2); + QList mappedPoints; - for (int j = 0; j < triangle.size(); j++) { - srcPoints(j,0) = (triangle[j].x-mean[0])/(norm/150.)+50; - srcPoints(j,1) = (triangle[j].y-mean[1])/(norm/150.)+50; + for (int i = 0; i < validTriangles.size(); i++) { + Eigen::MatrixXf srcMat(validTriangles[i].size(), 2); + + for (int j = 0; j < validTriangles[i].size(); j++) { + srcMat(j,0) = (validTriangles[i][j].x-mean[0])/(norm/100.)+50; + srcMat(j,1) = (validTriangles[i][j].y-mean[1])/(norm/100.)+50; } - Eigen::MatrixXf dstMat = srcPoints*R; + Eigen::MatrixXf dstMat = srcMat*R; + + Point2f srcPoints[3]; + for (int j = 0; j < 3; j++) srcPoints[j] = validTriangles[i][j]; - Point2f test[3]; - test[0] = triangle[0]; - test[1] = triangle[1]; - test[2] = triangle[2]; Point2f dstPoints[3]; - dstPoints[0] = Point2f(dstMat(0,0),dstMat(0,1)); - dstPoints[1] = Point2f(dstMat(1,0),dstMat(1,1)); - dstPoints[2] = Point2f(dstMat(2,0),dstMat(2,1)); + for (int j = 0; j < 3; j++) { + dstPoints[j] = Point2f(dstMat(j,0),dstMat(j,1)); + mappedPoints.append(dstPoints[j]); + } Mat buffer(src.m().rows,src.m().cols,src.m().type()); - warpAffine(src.m(), buffer, getAffineTransform(test, dstPoints), Size(src.m().cols,src.m().rows)); + warpAffine(src.m(), buffer, getAffineTransform(srcPoints, dstPoints), Size(src.m().cols,src.m().rows)); - Mat mask = Mat::zeros(src.m().rows, src.m().cols, CV_8U); + Mat mask = Mat::zeros(src.m().rows, src.m().cols, CV_8UC1); Point maskPoints[1][3]; maskPoints[0][0] = dstPoints[0]; maskPoints[0][1] = dstPoints[1]; @@ -251,10 +261,31 @@ class DelaunayTransform : public UntrainableTransform Mat output(src.m().rows,src.m().cols,src.m().type()); + // Optimize + if (i > 0) { + Mat overlap; + bitwise_and(dst.m(),mask,overlap); + for (int j = 0; j < overlap.rows; j++) { + for (int k = 0; k < overlap.cols; k++) { + if (overlap.at(k,j) != 0) { + mask.at(k,j) = 0; + } + } + } + } + bitwise_and(buffer,mask,output); - dst.m() += output; + dst.m() += output; // Need to xor stuff out } + + Rect boundingBox = boundingRect(mappedPoints.toVector().toStdVector()); + boundingBox.x += boundingBox.width * .025; + boundingBox.y += boundingBox.height * .025; // 0.025 for nose, .05 for mouth, .10 for brow + boundingBox.width *= .975; + boundingBox.height *= .975; // .975 for nose, .95 for mouth, .925 for brow + + dst.m() = Mat(dst.m(), boundingBox); } } diff --git a/openbr/plugins/stasm4.cpp b/openbr/plugins/stasm4.cpp index addc469..810903c 100644 --- a/openbr/plugins/stasm4.cpp +++ b/openbr/plugins/stasm4.cpp @@ -39,7 +39,7 @@ class StasmInitializer : public Initializer Globals->abbreviations.insert("RectFromStasmEyes","RectFromPoints([29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47],0.125,6.0)+Resize(44,164)"); Globals->abbreviations.insert("RectFromStasmBrow","RectFromPoints([17, 18, 19, 20, 21, 22, 23, 24],0.15,6)+Resize(28,132)"); Globals->abbreviations.insert("RectFromStasmNose","RectFromPoints([48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58],0.15,1.25)"); - Globals->abbreviations.insert("RectFromStasmMouth","RectFromPoints([59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76],0.3,3.0)+Resize(28,68)"); + Globals->abbreviations.insert("RectFromStasmMouth","RectFromPoints([59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76],0.3,2.5)"); } }; -- libgit2 0.21.4