From 53b5afbba92cf5a06d262dbcff3216fd1305bdee Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Mon, 11 Dec 2017 17:03:44 +0300 Subject: [PATCH] core(test): refactor Java Eigenvalues/PCA tests --- modules/core/misc/java/test/CoreTest.java | 64 ++++++++++++++++++----- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/modules/core/misc/java/test/CoreTest.java b/modules/core/misc/java/test/CoreTest.java index 3187e7cd2d..bd05407e20 100644 --- a/modules/core/misc/java/test/CoreTest.java +++ b/modules/core/misc/java/test/CoreTest.java @@ -394,7 +394,13 @@ public class CoreTest extends OpenCVTestCase { } 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 eigenVecs = new Mat(); @@ -402,18 +408,22 @@ public class CoreTest extends OpenCVTestCase { Mat expectedEigenVals = new Mat(3, 1, CvType.CV_32FC1) { { - put(0, 0, 6, 0, 0); - } - }; - 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); + put(0, 0, 6, 4, 2); } }; 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() { @@ -1326,7 +1336,8 @@ public class CoreTest extends OpenCVTestCase { Mat vectors = new Mat(); 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) { { put(0, 0, 2, 4, 4, 8); @@ -1338,7 +1349,21 @@ public class CoreTest extends OpenCVTestCase { } }; 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() { @@ -1365,7 +1390,20 @@ public class CoreTest extends OpenCVTestCase { } }; 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() {