From 5d82ee4ecf58e65cdbf5eb0fd008f993b894978e Mon Sep 17 00:00:00 2001 From: Scott Klum Date: Wed, 3 Jul 2013 14:03:53 -0400 Subject: [PATCH] Delauney transform: started; Procrusted transform: suboptimal, but functionally done --- openbr/plugins/landmarks.cpp | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ openbr/plugins/regions.cpp | 8 ++++++-- openbr/plugins/stasm4.cpp | 104 +------------------------------------------------------------------------------------------------------- 3 files changed, 178 insertions(+), 105 deletions(-) create mode 100644 openbr/plugins/landmarks.cpp diff --git a/openbr/plugins/landmarks.cpp b/openbr/plugins/landmarks.cpp new file mode 100644 index 0000000..ee18150 --- /dev/null +++ b/openbr/plugins/landmarks.cpp @@ -0,0 +1,171 @@ +#include +#include "openbr_internal.h" +#include "openbr/core/qtutils.h" +#include "openbr/core/opencvutils.h" +#include +#include + +using namespace std; +using namespace cv; + +namespace br +{ + +/*! + * \ingroup transforms + * \brief Wraps STASM key point detector + * \author Scott Klum \cite sklum + */ +class ProcrustesTransform : public Transform +{ + Q_OBJECT + + Q_PROPERTY(QString principalShapePath READ get_principalShapePath WRITE set_principalShapePath RESET reset_principalShapePath STORED false) + BR_PROPERTY(QString, principalShapePath, QString()) + + Eigen::MatrixXf meanShape; + Mat shapeMat; + + void train(const TemplateList &data) + { + QList< QList > normalizedPoints; + + // Normalize all sets of points + foreach (br::Template datum, data) { + QList points = OpenCVUtils::toPoints(datum.file.points()); + + if (points.empty()) { + continue; + } + + cv::Scalar mean = cv::mean(points.toVector().toStdVector()); + for (int i = 0; i < points.size(); i++) { + points[i].x -= mean[0]; + points[i].y -= mean[1]; + } + + float norm = cv::norm(points.toVector().toStdVector()); + for (int i = 0; i < points.size(); i++) { + points[i].x /= (norm); + points[i].y /= (norm); + } + + normalizedPoints.append(points); + } + + // Determine mean shape + Eigen::MatrixXf shapeTest(normalizedPoints[0].size(), 2); + + cv::Mat shapeBuffer(normalizedPoints[0].size(), 2, CV_32F); + + for (int i = 0; i < normalizedPoints[0].size(); i++) { + + double x = 0; + double y = 0; + + for (int j = 0; j < normalizedPoints.size(); j++) { + x += normalizedPoints[j][i].x; + y += normalizedPoints[j][i].y; + } + + x /= (double)normalizedPoints.size(); + y /= (double)normalizedPoints.size(); + + shapeBuffer.at(i,0) = x; + shapeBuffer.at(i,1) = y; + + shapeTest(i,0) = x; + shapeTest(i,1) = y; + } + + meanShape = shapeTest; + } + + void project(const Template &src, Template &dst) const + { + QList points = src.file.points(); + + cv::Scalar mean = cv::mean(OpenCVUtils::toPoints(points).toVector().toStdVector()); + + for (int i = 0; i < points.size(); i++) points[i] -= QPointF(mean[0],mean[1]); + + float norm = cv::norm(OpenCVUtils::toPoints(points).toVector().toStdVector()); + + 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; + } + + Eigen::JacobiSVD svd(srcPoints.transpose()*meanShape, Eigen::ComputeThinU | Eigen::ComputeThinV); + + Eigen::MatrixXf R = svd.matrixU()*svd.matrixV().transpose(); + + Eigen::MatrixXf dstPoints = srcPoints*R; + + points.clear(); + + for (int i = 0; i < dstPoints.rows(); i++) points.append(QPointF(dstPoints(i,0),dstPoints(i,1))); + + dst.file.setPoints(points); + } + +}; + +BR_REGISTER(Transform, ProcrustesTransform) + +/*! + * \ingroup transforms + * \brief Wraps STASM key point detector + * \author Scott Klum \cite sklum + */ +class DelauneyTransform : public UntrainableTransform +{ + Q_OBJECT + + Q_PROPERTY(bool draw READ get_draw WRITE set_draw RESET reset_draw STORED false) + BR_PROPERTY(bool, draw, false) + + void project(const Template &src, Template &dst) const + { + dst = src; + + Subdiv2D subdiv(Rect(0,0,src.m().cols,src.m().rows)); + + foreach(const cv::Point2f& point, OpenCVUtils::toPoints(src.file.points())) subdiv.insert(point); + + vector triangleList; + subdiv.getTriangleList(triangleList); + vector pt(3); + + Scalar delaunay_color(0, 0, 0); + + if (draw) { + for(size_t i = 0; i < triangleList.size(); ++i) { + Vec6f t = triangleList[i]; + + pt[0] = Point(cvRound(t[0]), cvRound(t[1])); + pt[1] = Point(cvRound(t[2]), cvRound(t[3])); + pt[2] = Point(cvRound(t[4]), cvRound(t[5])); + bool outside = true; + for (int i = 0; i < 3; i++) { + if(pt[i].x > dst.m().cols || pt[i].y > dst.m().rows || pt[i].x <= 0 || pt[i].y <= 0) + outside = false; + } + if (outside) { + line(dst.m(), pt[0], pt[1], delaunay_color, 1); + line(dst.m(), pt[1], pt[2], delaunay_color, 1); + line(dst.m(), pt[2], pt[0], delaunay_color, 1); + } + } + } + } + +}; + +BR_REGISTER(Transform, DelauneyTransform) + +} // namespace br + +#include "landmarks.moc" diff --git a/openbr/plugins/regions.cpp b/openbr/plugins/regions.cpp index 791f2e5..26d2dc0 100644 --- a/openbr/plugins/regions.cpp +++ b/openbr/plugins/regions.cpp @@ -201,7 +201,7 @@ class RectFromPointsTransform : public UntrainableTransform Q_PROPERTY(QList indices READ get_indices WRITE set_indices RESET reset_indices STORED false) Q_PROPERTY(double padding READ get_padding WRITE set_padding RESET reset_padding STORED false) Q_PROPERTY(double aspectRatio READ get_aspectRatio WRITE set_aspectRatio RESET reset_aspectRatio STORED false) - Q_PROPERTY(bool crop READ get_crop WRITE set_crop RESET reset_crop STORED false); + Q_PROPERTY(bool crop READ get_crop WRITE set_crop RESET reset_crop STORED false) BR_PROPERTY(QList, indices, QList()) BR_PROPERTY(double, padding, 0) BR_PROPERTY(double, aspectRatio, 1.0) @@ -220,13 +220,15 @@ class RectFromPointsTransform : public UntrainableTransform int maxX, maxY; maxX = maxY = -std::numeric_limits::max(); + QList points; + foreach(int index, indices) { if (src.file.points().size() > index) { if (src.file.points()[index].x() < minX) minX = src.file.points()[index].x(); if (src.file.points()[index].x() > maxX) maxX = src.file.points()[index].x(); if (src.file.points()[index].y() < minY) minY = src.file.points()[index].y(); if (src.file.points()[index].y() > maxY) maxY = src.file.points()[index].y(); - dst.file.appendPoint(src.file.points()[index]); + points.append(src.file.points()[index]); } } @@ -238,6 +240,8 @@ class RectFromPointsTransform : public UntrainableTransform double deltaHeight = width/aspectRatio - height; height += deltaHeight; + dst.file.setPoints(points); + if (crop) dst.m() = src.m()(Rect(std::max(0.0, minX - deltaWidth/2.0), std::max(0.0, minY - deltaHeight/2.0), std::min((double)src.m().cols, width), std::min((double)src.m().rows, height))); else dst.m() = src.m(); } diff --git a/openbr/plugins/stasm4.cpp b/openbr/plugins/stasm4.cpp index cb184c2..3191edf 100644 --- a/openbr/plugins/stasm4.cpp +++ b/openbr/plugins/stasm4.cpp @@ -7,6 +7,7 @@ #include #include +using namespace std; using namespace cv; namespace br @@ -83,109 +84,6 @@ class StasmTransform : public UntrainableTransform BR_REGISTER(Transform, StasmTransform) -#include - -/*! - * \ingroup transforms - * \brief Wraps STASM key point detector - * \author Scott Klum \cite sklum - */ -class ProcrustesTransform : public Transform -{ - Q_OBJECT - - Q_PROPERTY(QString principalShapePath READ get_principalShapePath WRITE set_principalShapePath RESET reset_principalShapePath STORED false) - BR_PROPERTY(QString, principalShapePath, QString()) - - Eigen::MatrixXf meanShape; - - void train(const TemplateList &data) - { - QList< QList > normalizedPoints; - - // Normalize all sets of points - foreach (br::Template datum, data) { - QList points = OpenCVUtils::toPoints(datum.file.points()); - - if (points.empty()) { - continue; - } - - cv::Scalar mean = cv::mean(points.toVector().toStdVector()); - for (int i = 0; i < points.size(); i++) { - points[i].x -= mean[0]; - points[i].y -= mean[1]; - } - - float norm = cv::norm(points.toVector().toStdVector()); - for (int i = 0; i < points.size(); i++) { - points[i].x /= norm; - points[i].y /= norm; - } - - normalizedPoints.append(points); - } - - // Determine mean shape - Eigen::MatrixXf shapeTest(normalizedPoints[0].size(), 2); - - cv::Mat shapeBuffer(normalizedPoints[0].size(), 2, CV_32F); - - for (int i = 0; i < normalizedPoints[0].size(); i++) { - - double x = 0; - double y = 0; - - for (int j = 0; j < normalizedPoints.size(); j++) { - x += normalizedPoints[j][i].x; - y += normalizedPoints[j][i].y; - } - - x /= (double)normalizedPoints.size(); - y /= (double)normalizedPoints.size(); - - shapeBuffer.at(i,0) = x; - shapeBuffer.at(i,1) = y; - - shapeTest(i,0) = x; - shapeTest(i,1) = y; - } - - meanShape = shapeTest; - } - - void project(const Template &src, Template &dst) const - { - QList points = src.file.points(); - - cv::Scalar mean = cv::mean(OpenCVUtils::toPoints(points).toVector().toStdVector()); - - for (int i = 0; i < points.size(); i++) points[i] -= QPointF(mean[0],mean[1]); - - float norm = cv::norm(OpenCVUtils::toPoints(points).toVector().toStdVector()); - - Eigen::MatrixXf srcPoints(points.size(), 2); - for (int i = 0; i < points.size(); i++) { - srcPoints(i,0) = points[i].x()/norm; - srcPoints(i,1) = points[i].y()/norm; - } - - Eigen::JacobiSVD svd(srcPoints.transpose()*meanShape, Eigen::ComputeThinU | Eigen::ComputeThinV); - - Eigen::MatrixXf R = svd.matrixU()*svd.matrixV().transpose(); - - std::cout << R(1,0) << std::endl; - // Determine transformation matrix - - // Apply transformation matrix - //dst.file.setPoints(meanShape);*/ - dst.m() = src.m(); - } - -}; - -BR_REGISTER(Transform, ProcrustesTransform) - } // namespace br #include "stasm4.moc" -- libgit2 0.21.4