From 1c20a7f0087d7f0b764bec8cf9010cd9b2677e53 Mon Sep 17 00:00:00 2001 From: berak Date: Thu, 23 Aug 2018 11:32:26 +0200 Subject: [PATCH] java: add a MatOfRotatedRect class --- .../misc/java/src/java/core+MatOfRotatedRect.java | 86 ++++++++++++++++++++++ modules/core/misc/java/test/RotatedRectTest.java | 22 ++++++ 2 files changed, 108 insertions(+) create mode 100644 modules/core/misc/java/src/java/core+MatOfRotatedRect.java diff --git a/modules/core/misc/java/src/java/core+MatOfRotatedRect.java b/modules/core/misc/java/src/java/core+MatOfRotatedRect.java new file mode 100644 index 0000000..dc8ba14 --- /dev/null +++ b/modules/core/misc/java/src/java/core+MatOfRotatedRect.java @@ -0,0 +1,86 @@ +package org.opencv.core; + +import java.util.Arrays; +import java.util.List; + +import org.opencv.core.RotatedRect; + + + +public class MatOfRotatedRect extends Mat { + // 64FC5 + private static final int _depth = CvType.CV_64F; + private static final int _channels = 5; + + public MatOfRotatedRect() { + super(); + } + + protected MatOfRotatedRect(long addr) { + super(addr); + if( !empty() && checkVector(_channels, _depth) < 0 ) + throw new IllegalArgumentException("Incompatible Mat"); + //FIXME: do we need release() here? + } + + public static MatOfRotatedRect fromNativeAddr(long addr) { + return new MatOfRotatedRect(addr); + } + + public MatOfRotatedRect(Mat m) { + super(m, Range.all()); + if( !empty() && checkVector(_channels, _depth) < 0 ) + throw new IllegalArgumentException("Incompatible Mat"); + //FIXME: do we need release() here? + } + + public MatOfRotatedRect(RotatedRect...a) { + super(); + fromArray(a); + } + + public void alloc(int elemNumber) { + if(elemNumber>0) + super.create(elemNumber, 1, CvType.makeType(_depth, _channels)); + } + + public void fromArray(RotatedRect...a) { + if(a==null || a.length==0) + return; + int num = a.length; + alloc(num); + double buff[] = new double[num * _channels]; + for(int i=0; i lr) { + RotatedRect ap[] = lr.toArray(new RotatedRect[0]); + fromArray(ap); + } + + public List toList() { + RotatedRect[] ar = toArray(); + return Arrays.asList(ar); + } +} diff --git a/modules/core/misc/java/test/RotatedRectTest.java b/modules/core/misc/java/test/RotatedRectTest.java index fc215fc..330caf4 100644 --- a/modules/core/misc/java/test/RotatedRectTest.java +++ b/modules/core/misc/java/test/RotatedRectTest.java @@ -1,11 +1,16 @@ package org.opencv.test.core; +import org.opencv.core.CvType; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.RotatedRect; +import org.opencv.core.MatOfRotatedRect; import org.opencv.core.Size; import org.opencv.test.OpenCVTestCase; +import java.util.Arrays; +import java.util.List; + public class RotatedRectTest extends OpenCVTestCase { private double angle; @@ -188,4 +193,21 @@ public class RotatedRectTest extends OpenCVTestCase { assertEquals(expected, actual); } + public void testMatOfRotatedRect() { + RotatedRect a = new RotatedRect(new Point(1,2),new Size(3,4),5.678); + RotatedRect b = new RotatedRect(new Point(9,8),new Size(7,6),5.432); + MatOfRotatedRect m = new MatOfRotatedRect(a,b,a,b,a,b,a,b); + assertEquals(m.rows(), 8); + assertEquals(m.cols(), 1); + assertEquals(m.type(), CvType.CV_64FC(5)); + RotatedRect[] arr = m.toArray(); + assertTrue(arr[2].angle == 5.678); + assertTrue(arr[3].center.x == 9); + assertTrue(arr[3].size.width == 7); + List li = m.toList(); + assertTrue(li.size() == 8); + RotatedRect rr = li.get(7); + assertTrue(rr.angle == 5.432); + assertTrue(rr.center.y == 8); + } } -- 2.7.4