core(test): refactor Java Eigenvalues/PCA tests

pull/9804/head
Alexander Alekhin 7 years ago
parent d48d2d7fe2
commit 53b5afbba9
  1. 64
      modules/core/misc/java/test/CoreTest.java

@ -394,7 +394,13 @@ public class CoreTest extends OpenCVTestCase {
} }
public void testEigen() { public void testEigen() {
Mat src = new Mat(3, 3, CvType.CV_32FC1, new Scalar(2.0)); Mat src = new Mat(3, 3, CvType.CV_32FC1) {
{
put(0, 0, 2, 0, 0);
put(1, 0, 0, 6, 0);
put(2, 0, 0, 0, 4);
}
};
Mat eigenVals = new Mat(); Mat eigenVals = new Mat();
Mat eigenVecs = new Mat(); Mat eigenVecs = new Mat();
@ -402,18 +408,22 @@ public class CoreTest extends OpenCVTestCase {
Mat expectedEigenVals = new Mat(3, 1, CvType.CV_32FC1) { Mat expectedEigenVals = new Mat(3, 1, CvType.CV_32FC1) {
{ {
put(0, 0, 6, 0, 0); put(0, 0, 6, 4, 2);
}
};
Mat expectedEigenVecs = new Mat(3, 3, CvType.CV_32FC1) {
{
put(0, 0, 0.57735026, 0.57735026, 0.57735032);
put(1, 0, 0.70710677, -0.70710677, 0);
put(2, 0, -0.40824831, -0.40824831, 0.81649661);
} }
}; };
assertMatEqual(eigenVals, expectedEigenVals, EPS); assertMatEqual(eigenVals, expectedEigenVals, EPS);
assertMatEqual(eigenVecs, expectedEigenVecs, EPS);
// check by definition
double eps = 1e-3;
for(int i = 0; i < 3; i++)
{
Mat vec = eigenVecs.row(i).t();
Mat lhs = new Mat(3, 1, CvType.CV_32FC1);
Core.gemm(src, vec, 1.0, new Mat(), 1.0, lhs);
Mat rhs = new Mat(3, 1, CvType.CV_32FC1);
Core.gemm(vec, eigenVals.row(i), 1.0, new Mat(), 1.0, rhs);
assertMatEqual(lhs, rhs, eps);
}
} }
public void testExp() { public void testExp() {
@ -1326,7 +1336,8 @@ public class CoreTest extends OpenCVTestCase {
Mat vectors = new Mat(); Mat vectors = new Mat();
Core.PCACompute(data, mean, vectors); Core.PCACompute(data, mean, vectors);
//System.out.println(mean.dump());
//System.out.println(vectors.dump());
Mat mean_truth = new Mat(1, 4, CvType.CV_32F) { Mat mean_truth = new Mat(1, 4, CvType.CV_32F) {
{ {
put(0, 0, 2, 4, 4, 8); put(0, 0, 2, 4, 4, 8);
@ -1338,7 +1349,21 @@ public class CoreTest extends OpenCVTestCase {
} }
}; };
assertMatEqual(mean_truth, mean, EPS); assertMatEqual(mean_truth, mean, EPS);
assertMatEqual(vectors_truth, vectors, EPS);
// eigenvectors are normalized (length = 1),
// but direction is unknown (v and -v are both eigen vectors)
// so this direct check doesn't work:
// assertMatEqual(vectors_truth, vectors, EPS);
for(int i = 0; i < 3; i++)
{
Mat vec0 = vectors_truth.row(i);
Mat vec1 = vectors.row(i);
Mat vec1_ = new Mat();
Core.subtract(new Mat(1, 4, CvType.CV_32F, new Scalar(0)), vec1, vec1_);
double scale1 = Core.norm(vec0, vec1);
double scale2 = Core.norm(vec0, vec1_);
assertTrue(Math.min(scale1, scale2) < EPS);
}
} }
public void testPCAComputeMatMatMatInt() { public void testPCAComputeMatMatMatInt() {
@ -1365,7 +1390,20 @@ public class CoreTest extends OpenCVTestCase {
} }
}; };
assertMatEqual(mean_truth, mean, EPS); assertMatEqual(mean_truth, mean, EPS);
assertMatEqual(vectors_truth, vectors, EPS); // eigenvectors are normalized (length = 1),
// but direction is unknown (v and -v are both eigen vectors)
// so this direct check doesn't work:
// assertMatEqual(vectors_truth, vectors, EPS);
for(int i = 0; i < 1; i++)
{
Mat vec0 = vectors_truth.row(i);
Mat vec1 = vectors.row(i);
Mat vec1_ = new Mat();
Core.subtract(new Mat(1, 4, CvType.CV_32F, new Scalar(0)), vec1, vec1_);
double scale1 = Core.norm(vec0, vec1);
double scale2 = Core.norm(vec0, vec1_);
assertTrue(Math.min(scale1, scale2) < EPS);
}
} }
public void testPCAProject() { public void testPCAProject() {

Loading…
Cancel
Save