Commit 66160b306237c41a1d5a6dd8673254fbbe38df32
1 parent
a8dad531
Procrustes transform: R matrix calculated
Showing
2 changed files
with
112 additions
and
2 deletions
openbr/plugins/regions.cpp
| @@ -201,9 +201,11 @@ class RectFromPointsTransform : public UntrainableTransform | @@ -201,9 +201,11 @@ class RectFromPointsTransform : public UntrainableTransform | ||
| 201 | Q_PROPERTY(QList<int> indices READ get_indices WRITE set_indices RESET reset_indices STORED false) | 201 | Q_PROPERTY(QList<int> indices READ get_indices WRITE set_indices RESET reset_indices STORED false) |
| 202 | Q_PROPERTY(double padding READ get_padding WRITE set_padding RESET reset_padding STORED false) | 202 | Q_PROPERTY(double padding READ get_padding WRITE set_padding RESET reset_padding STORED false) |
| 203 | Q_PROPERTY(double aspectRatio READ get_aspectRatio WRITE set_aspectRatio RESET reset_aspectRatio STORED false) | 203 | Q_PROPERTY(double aspectRatio READ get_aspectRatio WRITE set_aspectRatio RESET reset_aspectRatio STORED false) |
| 204 | + Q_PROPERTY(bool crop READ get_crop WRITE set_crop RESET reset_crop STORED false); | ||
| 204 | BR_PROPERTY(QList<int>, indices, QList<int>()) | 205 | BR_PROPERTY(QList<int>, indices, QList<int>()) |
| 205 | BR_PROPERTY(double, padding, 0) | 206 | BR_PROPERTY(double, padding, 0) |
| 206 | BR_PROPERTY(double, aspectRatio, 1.0) | 207 | BR_PROPERTY(double, aspectRatio, 1.0) |
| 208 | + BR_PROPERTY(bool, crop, true) | ||
| 207 | 209 | ||
| 208 | void project(const Template &src, Template &dst) const | 210 | void project(const Template &src, Template &dst) const |
| 209 | { | 211 | { |
| @@ -236,7 +238,8 @@ class RectFromPointsTransform : public UntrainableTransform | @@ -236,7 +238,8 @@ class RectFromPointsTransform : public UntrainableTransform | ||
| 236 | double deltaHeight = width/aspectRatio - height; | 238 | double deltaHeight = width/aspectRatio - height; |
| 237 | height += deltaHeight; | 239 | height += deltaHeight; |
| 238 | 240 | ||
| 239 | - 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))); | 241 | + 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))); |
| 242 | + else dst.m() = src.m(); | ||
| 240 | } | 243 | } |
| 241 | }; | 244 | }; |
| 242 | 245 |
openbr/plugins/stasm4.cpp
| 1 | #include <stasm_lib.h> | 1 | #include <stasm_lib.h> |
| 2 | #include <stasmcascadeclassifier.h> | 2 | #include <stasmcascadeclassifier.h> |
| 3 | -#include <opencv2/objdetect/objdetect.hpp> | 3 | +#include <opencv2/opencv.hpp> |
| 4 | #include "openbr_internal.h" | 4 | #include "openbr_internal.h" |
| 5 | +#include "openbr/core/qtutils.h" | ||
| 6 | +#include "openbr/core/opencvutils.h" | ||
| 7 | +#include <QString> | ||
| 8 | +#include <Eigen/SVD> | ||
| 5 | 9 | ||
| 6 | using namespace cv; | 10 | using namespace cv; |
| 7 | 11 | ||
| @@ -79,6 +83,109 @@ class StasmTransform : public UntrainableTransform | @@ -79,6 +83,109 @@ class StasmTransform : public UntrainableTransform | ||
| 79 | 83 | ||
| 80 | BR_REGISTER(Transform, StasmTransform) | 84 | BR_REGISTER(Transform, StasmTransform) |
| 81 | 85 | ||
| 86 | +#include <iostream> | ||
| 87 | + | ||
| 88 | +/*! | ||
| 89 | + * \ingroup transforms | ||
| 90 | + * \brief Wraps STASM key point detector | ||
| 91 | + * \author Scott Klum \cite sklum | ||
| 92 | + */ | ||
| 93 | +class ProcrustesTransform : public Transform | ||
| 94 | +{ | ||
| 95 | + Q_OBJECT | ||
| 96 | + | ||
| 97 | + Q_PROPERTY(QString principalShapePath READ get_principalShapePath WRITE set_principalShapePath RESET reset_principalShapePath STORED false) | ||
| 98 | + BR_PROPERTY(QString, principalShapePath, QString()) | ||
| 99 | + | ||
| 100 | + Eigen::MatrixXf meanShape; | ||
| 101 | + | ||
| 102 | + void train(const TemplateList &data) | ||
| 103 | + { | ||
| 104 | + QList< QList<cv::Point2f> > normalizedPoints; | ||
| 105 | + | ||
| 106 | + // Normalize all sets of points | ||
| 107 | + foreach (br::Template datum, data) { | ||
| 108 | + QList<cv::Point2f> points = OpenCVUtils::toPoints(datum.file.points()); | ||
| 109 | + | ||
| 110 | + if (points.empty()) { | ||
| 111 | + continue; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + cv::Scalar mean = cv::mean(points.toVector().toStdVector()); | ||
| 115 | + for (int i = 0; i < points.size(); i++) { | ||
| 116 | + points[i].x -= mean[0]; | ||
| 117 | + points[i].y -= mean[1]; | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + float norm = cv::norm(points.toVector().toStdVector()); | ||
| 121 | + for (int i = 0; i < points.size(); i++) { | ||
| 122 | + points[i].x /= norm; | ||
| 123 | + points[i].y /= norm; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + normalizedPoints.append(points); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + // Determine mean shape | ||
| 130 | + Eigen::MatrixXf shapeTest(normalizedPoints[0].size(), 2); | ||
| 131 | + | ||
| 132 | + cv::Mat shapeBuffer(normalizedPoints[0].size(), 2, CV_32F); | ||
| 133 | + | ||
| 134 | + for (int i = 0; i < normalizedPoints[0].size(); i++) { | ||
| 135 | + | ||
| 136 | + double x = 0; | ||
| 137 | + double y = 0; | ||
| 138 | + | ||
| 139 | + for (int j = 0; j < normalizedPoints.size(); j++) { | ||
| 140 | + x += normalizedPoints[j][i].x; | ||
| 141 | + y += normalizedPoints[j][i].y; | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + x /= (double)normalizedPoints.size(); | ||
| 145 | + y /= (double)normalizedPoints.size(); | ||
| 146 | + | ||
| 147 | + shapeBuffer.at<float>(i,0) = x; | ||
| 148 | + shapeBuffer.at<float>(i,1) = y; | ||
| 149 | + | ||
| 150 | + shapeTest(i,0) = x; | ||
| 151 | + shapeTest(i,1) = y; | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + meanShape = shapeTest; | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + void project(const Template &src, Template &dst) const | ||
| 158 | + { | ||
| 159 | + QList<QPointF> points = src.file.points(); | ||
| 160 | + | ||
| 161 | + cv::Scalar mean = cv::mean(OpenCVUtils::toPoints(points).toVector().toStdVector()); | ||
| 162 | + | ||
| 163 | + for (int i = 0; i < points.size(); i++) points[i] -= QPointF(mean[0],mean[1]); | ||
| 164 | + | ||
| 165 | + float norm = cv::norm(OpenCVUtils::toPoints(points).toVector().toStdVector()); | ||
| 166 | + | ||
| 167 | + Eigen::MatrixXf srcPoints(points.size(), 2); | ||
| 168 | + for (int i = 0; i < points.size(); i++) { | ||
| 169 | + srcPoints(i,0) = points[i].x()/norm; | ||
| 170 | + srcPoints(i,1) = points[i].y()/norm; | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + Eigen::JacobiSVD<Eigen::MatrixXf> svd(srcPoints.transpose()*meanShape, Eigen::ComputeThinU | Eigen::ComputeThinV); | ||
| 174 | + | ||
| 175 | + Eigen::MatrixXf R = svd.matrixU()*svd.matrixV().transpose(); | ||
| 176 | + | ||
| 177 | + std::cout << R(1,0) << std::endl; | ||
| 178 | + // Determine transformation matrix | ||
| 179 | + | ||
| 180 | + // Apply transformation matrix | ||
| 181 | + //dst.file.setPoints(meanShape);*/ | ||
| 182 | + dst.m() = src.m(); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | +}; | ||
| 186 | + | ||
| 187 | +BR_REGISTER(Transform, ProcrustesTransform) | ||
| 188 | + | ||
| 82 | } // namespace br | 189 | } // namespace br |
| 83 | 190 | ||
| 84 | #include "stasm4.moc" | 191 | #include "stasm4.moc" |