Java API: fixing Core tests; minor enchantments in CvVector-s API
authorAndrey Pavlenko <no@email>
Wed, 4 Apr 2012 14:26:43 +0000 (14:26 +0000)
committerAndrey Pavlenko <no@email>
Wed, 4 Apr 2012 14:26:43 +0000 (14:26 +0000)
13 files changed:
modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java
modules/java/android_test/src/org/opencv/test/core/CoreTest.java
modules/java/src/java/core+CvVectorByte.java
modules/java/src/java/core+CvVectorDMatch.java
modules/java/src/java/core+CvVectorDouble.java
modules/java/src/java/core+CvVectorFloat.java
modules/java/src/java/core+CvVectorInt.java
modules/java/src/java/core+CvVectorKeyPoint.java
modules/java/src/java/core+CvVectorPoint.java
modules/java/src/java/core+CvVectorPoint2f.java
modules/java/src/java/core+CvVectorPoint3.java
modules/java/src/java/core+CvVectorPoint3f.java
modules/java/src/java/core+CvVectorRect.java

index fc00915..3a8b268 100644 (file)
@@ -205,6 +205,26 @@ public class OpenCVTestCase extends TestCase {
             assertTrue(Math.abs(list1.get(i).doubleValue() - list2.get(i).doubleValue()) <= epsilon);
     }
 
+    public static <E extends Number> void assertArrayEquals(E[] ar1, E[] ar2, double epsilon) {
+        if (ar1.length != ar2.length) {
+            fail("Arrays have different sizes.");
+        }
+
+        for (int i = 0; i < ar1.length; i++)
+               assertEquals(ar1[i].doubleValue(), ar2[i].doubleValue(), epsilon);
+            //assertTrue(Math.abs(ar1[i].doubleValue() - ar2[i].doubleValue()) <= epsilon);
+    }
+
+    public static void assertArrayEquals(double[] ar1, double[] ar2, double epsilon) {
+        if (ar1.length != ar2.length) {
+            fail("Arrays have different sizes.");
+        }
+
+        for (int i = 0; i < ar1.length; i++)
+               assertEquals(ar1[i], ar2[i], epsilon);
+            //assertTrue(Math.abs(ar1[i].doubleValue() - ar2[i].doubleValue()) <= epsilon);
+    }
+
     public static void assertListMatEquals(List<Mat> list1, List<Mat> list2, double epsilon) {
         if (list1.size() != list2.size()) {
             throw new UnsupportedOperationException();
@@ -223,6 +243,14 @@ public class OpenCVTestCase extends TestCase {
             assertPointEquals(list1.get(i), list2.get(i), epsilon);
     }
 
+    public static void assertArrayPointsEquals(Point[] vp1, Point[] vp2, double epsilon) {
+        if (vp1.length != vp2.length) {
+            fail("Arrays have different sizes.");
+        }
+
+        for (int i = 0; i < vp1.length; i++)
+            assertPointEquals(vp1[i], vp2[i], epsilon);
+    }
     public static void assertListPoint3Equals(List<Point3> list1, List<Point3> list2, double epsilon) {
         if (list1.size() != list2.size()) {
             throw new UnsupportedOperationException();
index bbcb97a..39ba4e5 100644 (file)
@@ -4,6 +4,9 @@ import org.opencv.core.Core;
 import org.opencv.core.Core.MinMaxLocResult;
 import org.opencv.core.CvException;
 import org.opencv.core.CvType;
+import org.opencv.core.CvVectorDouble;
+import org.opencv.core.CvVectorInt;
+import org.opencv.core.CvVectorPoint;
 import org.opencv.core.Mat;
 import org.opencv.core.Point;
 import org.opencv.core.Rect;
@@ -482,11 +485,11 @@ public class CoreTest extends OpenCVTestCase {
         int arcStart = 30;
         int arcEnd = 60;
         int delta = 2;
-        List<Point> pts = new ArrayList<Point>();
+        CvVectorPoint pts = new CvVectorPoint();
 
         Core.ellipse2Poly(center, axes, angle, arcStart, arcEnd, delta, pts);
 
-        List<Point> truth = Arrays.asList(
+        Point truth[] = {
                 new Point(5, 6),
                 new Point(5, 6),
                 new Point(5, 6),
@@ -502,8 +505,9 @@ public class CoreTest extends OpenCVTestCase {
                 new Point(4, 6),
                 new Point(4, 6),
                 new Point(4, 6),
-                new Point(4, 6));
-        assertListPointEquals(truth, pts, EPS);
+                new Point(4, 6)
+        };
+        assertArrayPointsEquals(truth, pts.toArray(new Point[0]), EPS);
     }
 
     public void testEllipseMatPointSizeDoubleDoubleDoubleScalar() {
@@ -617,7 +621,7 @@ public class CoreTest extends OpenCVTestCase {
     }
 
     public void testFillConvexPolyMatListOfPointScalar() {
-        List<Point> polyline = Arrays.asList(new Point(1, 1), new Point(5, 0), new Point(6, 8), new Point(0, 9));
+        CvVectorPoint polyline = new CvVectorPoint(new Point[]{new Point(1, 1), new Point(5, 0), new Point(6, 8), new Point(0, 9)});
 
         Core.fillConvexPoly(gray0, polyline, new Scalar(150));
 
@@ -626,8 +630,8 @@ public class CoreTest extends OpenCVTestCase {
     }
 
     public void testFillConvexPolyMatListOfPointScalarIntInt() {
-        List<Point> polyline1 = Arrays.asList(new Point(2, 1), new Point(5, 1), new Point(5, 7), new Point(2, 7));
-        List<Point> polyline2 = Arrays.asList(new Point(4, 2), new Point(10, 2), new Point(10, 14), new Point(4, 14));
+        CvVectorPoint polyline1 = new CvVectorPoint(new Point(2, 1), new Point(5, 1), new Point(5, 7), new Point(2, 7));
+        CvVectorPoint polyline2 = new CvVectorPoint(new Point(4, 2), new Point(10, 2), new Point(10, 14), new Point(4, 14));
 
         // current implementation of fixed-point version of fillConvexPoly
         // requires image to be at least 2-pixel wider in each direction than
@@ -645,8 +649,8 @@ public class CoreTest extends OpenCVTestCase {
     public void testFillPolyMatListOfListOfPointScalar() {
         int matSize = 10;
         Mat gray0 = Mat.zeros(matSize, matSize, CvType.CV_8U);
-        List<Point> polyline = Arrays.asList(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
-        List<List<Point>> polylines = new ArrayList<List<Point>>();
+        CvVectorPoint polyline = new CvVectorPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
+        List<CvVectorPoint> polylines = new ArrayList<CvVectorPoint>();
         polylines.add(polyline);
 
         Core.fillPoly(gray0, polylines, new Scalar(1));
@@ -671,13 +675,13 @@ public class CoreTest extends OpenCVTestCase {
     }
 
     public void testFillPolyMatListOfListOfPointScalarIntIntPoint() {
-        List<Point> polyline1 = Arrays.asList(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
-        List<Point> polyline2 = Arrays.asList(new Point(0, 3), new Point(0, 7), new Point(3, 0), new Point(6, 7), new Point(6, 3));
+       CvVectorPoint polyline1 = new CvVectorPoint(new Point(1, 4), new Point(1, 8), new Point(4, 1), new Point(7, 8), new Point(7, 4));
+       CvVectorPoint polyline2 = new CvVectorPoint(new Point(0, 3), new Point(0, 7), new Point(3, 0), new Point(6, 7), new Point(6, 3));
 
-        List<List<Point>> polylines1 = new ArrayList<List<Point>>();
+        List<CvVectorPoint> polylines1 = new ArrayList<CvVectorPoint>();
         polylines1.add(polyline1);
 
-        List<List<Point>> polylines2 = new ArrayList<List<Point>>();
+        List<CvVectorPoint> polylines2 = new ArrayList<CvVectorPoint>();
         polylines2.add(polyline2);
 
         Core.fillPoly(gray0, polylines1, new Scalar(1), Core.LINE_8, 0, new Point(0, 0));
@@ -1190,18 +1194,18 @@ public class CoreTest extends OpenCVTestCase {
     }
 
     public void testMeanStdDevMatMatMat() {
-        List<Double> mean   = new ArrayList<Double>();
-        List<Double> stddev = new ArrayList<Double>();
+       CvVectorDouble mean   = new CvVectorDouble();
+       CvVectorDouble stddev = new CvVectorDouble();
 
         Core.meanStdDev(rgbLena, mean, stddev);
 
-        List<Double> expectedMean = Arrays.asList( new Double[]
-               {105.3989906311035, 99.56269836425781, 179.7303047180176} );
-        List<Double> expectedDev = Arrays.asList( new Double[]
-            {33.74205485167219, 52.8734582803278, 49.01569488056406} );
+        double expectedMean[] = new double[]
+               {105.3989906311035, 99.56269836425781, 179.7303047180176};
+        double expectedDev[] = new double[]
+            {33.74205485167219, 52.8734582803278, 49.01569488056406};
         
-        assertListEquals(expectedMean, mean, EPS);
-        assertListEquals(expectedDev, stddev, EPS);
+        assertArrayEquals(expectedMean, mean.toArray(null), EPS);
+        assertArrayEquals(expectedDev, stddev.toArray(null), EPS);
     }
 
     public void testMeanStdDevMatMatMatMat() {
@@ -1210,16 +1214,16 @@ public class CoreTest extends OpenCVTestCase {
         Mat mask = gray0.clone();
         submat = mask.submat(0, mask.rows() / 2, 0, mask.cols() / 2);
         submat.setTo(new Scalar(1));
-        List<Double> mean   = new ArrayList<Double>();
-        List<Double> stddev = new ArrayList<Double>();
+       CvVectorDouble mean   = new CvVectorDouble();
+       CvVectorDouble stddev = new CvVectorDouble();
 
         Core.meanStdDev(grayRnd, mean, stddev, mask);
 
-        List<Double> expectedMean = Arrays.asList( new Double[] {33d} );
-        List<Double> expectedDev = Arrays.asList( new Double[] {0d} );
+        double expectedMean[] = new double[] {33d};
+        double expectedDev[]  = new double[] {0d};
                 
-        assertListEquals(expectedMean, mean, EPS);
-        assertListEquals(expectedDev, stddev, EPS);
+        assertArrayEquals(expectedMean, mean.toArray(null), EPS);
+        assertArrayEquals(expectedDev, stddev.toArray(null), EPS);
     }
 
     public void testMerge() {
@@ -1280,14 +1284,15 @@ public class CoreTest extends OpenCVTestCase {
         rgba0.setTo(new Scalar(10, 20, 30, 40));
         List<Mat> src = Arrays.asList(rgba0);
         List<Mat> dst = Arrays.asList(gray3, gray2, gray1, gray0, getMat(CvType.CV_8UC3, 0, 0, 0));
-        List<Integer> fromTo = Arrays.asList(
-                3, 0,
+        CvVectorInt fromTo = new CvVectorInt(1, new int[]
+                 { 3, 0,
                 3, 1,
                 2, 2,
                 0, 3,
                 2, 4,
                 1, 5,
-                0, 6);
+                0, 6 }
+        );
 
         Core.mixChannels(src, dst, fromTo);
 
@@ -1740,8 +1745,8 @@ public class CoreTest extends OpenCVTestCase {
 
     public void testPolylinesMatListOfListOfPointBooleanScalar() {
         Mat img = gray0;
-        List<List<Point>> polyline = new ArrayList<List<Point>>();
-        polyline.add(Arrays.asList(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
+        List<CvVectorPoint> polyline = new ArrayList<CvVectorPoint>();
+        polyline.add(new CvVectorPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
 
         Core.polylines(img, polyline, true, new Scalar(100));
 
@@ -1754,8 +1759,8 @@ public class CoreTest extends OpenCVTestCase {
 
     public void testPolylinesMatListOfListOfPointBooleanScalarInt() {
         Mat img = gray0;
-        List<List<Point>> polyline = new ArrayList<List<Point>>();
-        polyline.add(Arrays.asList(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
+        List<CvVectorPoint> polyline = new ArrayList<CvVectorPoint>();
+        polyline.add(new CvVectorPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
 
         Core.polylines(img, polyline, true, new Scalar(100), 2);
 
@@ -1764,10 +1769,10 @@ public class CoreTest extends OpenCVTestCase {
 
     public void testPolylinesMatListOfListOfPointBooleanScalarIntIntInt() {
         Mat img = gray0;
-        List<List<Point>> polyline1 = new ArrayList<List<Point>>();
-        polyline1.add(Arrays.asList(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
-        List<List<Point>> polyline2 = new ArrayList<List<Point>>();
-        polyline2.add(Arrays.asList(new Point(2, 2), new Point(14, 2), new Point(14, 12), new Point(2, 12)));
+        List<CvVectorPoint> polyline1 = new ArrayList<CvVectorPoint>();
+        polyline1.add(new CvVectorPoint(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6)));
+        List<CvVectorPoint> polyline2 = new ArrayList<CvVectorPoint>();
+        polyline2.add(new CvVectorPoint(new Point(2, 2), new Point(14, 2), new Point(14, 12), new Point(2, 12)));
 
         Core.polylines(img, polyline1, true, new Scalar(100), 2, Core.LINE_8, 0);
 
index 656eb9f..fdcb7ec 100644 (file)
@@ -7,6 +7,10 @@ public class CvVectorByte extends CvVector {
         super(_d, ch);\r
     }\r
 \r
+    public CvVectorByte() {\r
+        super(_d, 1);\r
+    }\r
+\r
     public CvVectorByte(int ch, long addr) {\r
         super(_d, ch, addr);\r
     }\r
index 5e47436..338782c 100644 (file)
@@ -17,9 +17,9 @@ public class CvVectorDMatch extends CvVectorFloat {
         super(_ch, m);\r
     }\r
 \r
-    public CvVectorDMatch(DMatch[] a) {\r
+    public CvVectorDMatch(DMatch...a) {\r
         super(_ch);\r
-        if(a==null)\r
+        if(a==null || a.length==0)\r
             return;\r
         int cnt = a.length;\r
         create(cnt);\r
index 6721359..a1a20c1 100644 (file)
@@ -7,6 +7,10 @@ public class CvVectorDouble extends CvVector {
         super(_d, ch);\r
     }\r
 \r
+    public CvVectorDouble() {\r
+        super(_d, 1);\r
+    }\r
+\r
     public CvVectorDouble(int ch, long addr) {\r
         super(_d, ch, addr);\r
     }\r
index a0a5091..e8e5b88 100644 (file)
@@ -7,6 +7,10 @@ public class CvVectorFloat extends CvVector {
         super(_d, ch);\r
     }\r
 \r
+    public CvVectorFloat() {\r
+        super(_d, 1);\r
+    }\r
+\r
     public CvVectorFloat(int ch, long addr) {\r
         super(_d, ch, addr);\r
     }\r
index 5993ae2..c9ae89a 100644 (file)
@@ -8,6 +8,10 @@ public class CvVectorInt extends CvVector {
         super(_d, ch);\r
     }\r
 \r
+    public CvVectorInt() {\r
+        super(_d, 1);\r
+    }\r
+\r
     public CvVectorInt(int ch, long addr) {\r
         super(_d, ch, addr);\r
     }\r
index 36b87de..657301a 100644 (file)
@@ -17,9 +17,9 @@ public class CvVectorKeyPoint extends CvVectorFloat {
         super(_ch, m);\r
     }\r
 \r
-    public CvVectorKeyPoint(KeyPoint[] a) {\r
+    public CvVectorKeyPoint(KeyPoint...a) {\r
         super(_ch);\r
-        if(a==null)\r
+        if(a==null || a.length==0)\r
             return;\r
         int cnt = a.length;\r
         create(cnt);\r
index b8c9e26..cde1457 100644 (file)
@@ -15,9 +15,9 @@ public class CvVectorPoint extends CvVectorInt {
         super(_ch, m);\r
     }\r
 \r
-    public CvVectorPoint(Point[] a) {\r
+    public CvVectorPoint(Point...a) {\r
         super(_ch);\r
-        if(a==null)\r
+        if(a==null || a.length==0)\r
             return;\r
         int cnt = a.length;\r
         create(cnt);\r
index 1b1ad11..73f96da 100644 (file)
@@ -15,9 +15,9 @@ public class CvVectorPoint2f extends CvVectorFloat {
         super(_ch, m);\r
     }\r
 \r
-    public CvVectorPoint2f(Point[] a) {\r
+    public CvVectorPoint2f(Point...a) {\r
         super(_ch);\r
-        if(a==null)\r
+        if(a==null || a.length==0)\r
             return;\r
         int cnt = a.length;\r
         create(cnt);\r
index 64cd462..827debb 100644 (file)
@@ -15,9 +15,9 @@ public class CvVectorPoint3 extends CvVectorInt {
         super(_ch, m);\r
     }\r
 \r
-    public CvVectorPoint3(Point3[] a) {\r
+    public CvVectorPoint3(Point3...a) {\r
         super(_ch);\r
-        if(a==null)\r
+        if(a==null || a.length==0)\r
             return;\r
         int cnt = a.length;\r
         create(cnt);\r
index c3132b6..d26854c 100644 (file)
@@ -15,9 +15,9 @@ public class CvVectorPoint3f extends CvVectorFloat {
         super(_ch, m);\r
     }\r
 \r
-    public CvVectorPoint3f(Point3[] a) {\r
+    public CvVectorPoint3f(Point3...a) {\r
         super(_ch);\r
-        if(a==null)\r
+        if(a==null || a.length==0)\r
             return;\r
         int cnt = a.length;\r
         create(cnt);\r
index e6e3688..32e9b4c 100644 (file)
@@ -16,9 +16,9 @@ public class CvVectorRect extends CvVectorInt {
         super(_ch, m);\r
     }\r
 \r
-    public CvVectorRect(Rect[] a) {\r
+    public CvVectorRect(Rect...a) {\r
         super(_ch);\r
-        if(a==null)\r
+        if(a==null || a.length==0)\r
             return;\r
         int cnt = a.length;\r
         create(cnt);\r