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