Commit 1f448ca7f549878ca5baf67cc5e52373be00efd4
1 parent
6f2dd0bf
Added ShapeAxisRatio
Showing
1 changed file
with
61 additions
and
0 deletions
openbr/plugins/imgproc/shapeaxisratio.cpp
0 → 100644
| 1 | +#include "openbr/plugins/openbr_internal.h" | ||
| 2 | +#include "openbr/core/opencvutils.h" | ||
| 3 | +#include "openbr/core/eigenutils.h" | ||
| 4 | + | ||
| 5 | +#include <opencv2/contrib/contrib.hpp> | ||
| 6 | + | ||
| 7 | +#include <Eigen/Dense> | ||
| 8 | + | ||
| 9 | +using namespace Eigen; | ||
| 10 | +using namespace cv; | ||
| 11 | + | ||
| 12 | +namespace br | ||
| 13 | +{ | ||
| 14 | + | ||
| 15 | +class ShapeAxisRatioTransform : public UntrainableTransform | ||
| 16 | +{ | ||
| 17 | + Q_OBJECT | ||
| 18 | + | ||
| 19 | + void project(const Template &src, Template &dst) const | ||
| 20 | + { | ||
| 21 | + dst = src; | ||
| 22 | + | ||
| 23 | + Mat mask = src.file.get<Mat>("Mask"); | ||
| 24 | + Mat masked; | ||
| 25 | + src.m().copyTo(masked, mask); | ||
| 26 | + | ||
| 27 | + Mat indices; | ||
| 28 | + findNonZero(masked,indices); | ||
| 29 | + | ||
| 30 | + dst.m() = Mat(1,1,CV_32FC1); | ||
| 31 | + | ||
| 32 | + if (indices.total() > 0) { | ||
| 33 | + MatrixXd data(indices.total(),2); | ||
| 34 | + | ||
| 35 | + for (size_t i=0; i<indices.total(); i++) { | ||
| 36 | + data(i,0) = indices.at<Point>(i).y; | ||
| 37 | + data(i,1) = indices.at<Point>(i).x; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + MatrixXd centered = data.rowwise() - data.colwise().mean(); | ||
| 41 | + MatrixXd cov = (centered.adjoint() * centered) / double(data.rows() - 1); | ||
| 42 | + | ||
| 43 | + SelfAdjointEigenSolver<Eigen::MatrixXd> eSolver(cov); | ||
| 44 | + MatrixXd D = eSolver.eigenvalues(); | ||
| 45 | + | ||
| 46 | + if (eSolver.info() == Success) | ||
| 47 | + dst.m().at<float>(0,0) = D(0)/D(1); | ||
| 48 | + else | ||
| 49 | + dst.file.fte = true; | ||
| 50 | + } else { | ||
| 51 | + dst.file.fte = true; | ||
| 52 | + qWarning("No mask content for %s.",qPrintable(src.file.baseName())); | ||
| 53 | + } | ||
| 54 | + } | ||
| 55 | +}; | ||
| 56 | + | ||
| 57 | +BR_REGISTER(Transform, ShapeAxisRatioTransform) | ||
| 58 | + | ||
| 59 | +} // namespace br | ||
| 60 | + | ||
| 61 | +#include "imgproc/shapeaxisratio.moc" |