From 579ffbf97e35527becf95f56571dce46e1182ab1 Mon Sep 17 00:00:00 2001 From: utibenkei Date: Sat, 31 Dec 2016 01:54:07 +0900 Subject: [PATCH] Added Java wrapping for tracking module --- .../misc/java/src/java/core+MatOfRect2d.java | 81 ++++++++++++++ .../core/misc/java/src/java/core+Rect2d.java | 100 ++++++++++++++++++ modules/java/generator/gen_java.py | 6 +- modules/java/generator/src/cpp/converters.cpp | 13 +++ modules/java/generator/src/cpp/converters.h | 3 + .../generator/src/java/utils+Converters.java | 37 +++++++ 6 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 modules/core/misc/java/src/java/core+MatOfRect2d.java create mode 100644 modules/core/misc/java/src/java/core+Rect2d.java diff --git a/modules/core/misc/java/src/java/core+MatOfRect2d.java b/modules/core/misc/java/src/java/core+MatOfRect2d.java new file mode 100644 index 0000000000..71c4b1aef6 --- /dev/null +++ b/modules/core/misc/java/src/java/core+MatOfRect2d.java @@ -0,0 +1,81 @@ +package org.opencv.core; + +import java.util.Arrays; +import java.util.List; + + +public class MatOfRect2d extends Mat { + // 64FC4 + private static final int _depth = CvType.CV_64F; + private static final int _channels = 4; + + public MatOfRect2d() { + super(); + } + + protected MatOfRect2d(long addr) { + super(addr); + if( !empty() && checkVector(_channels, _depth) < 0 ) + throw new IllegalArgumentException("Incompatible Mat"); + //FIXME: do we need release() here? + } + + public static MatOfRect2d fromNativeAddr(long addr) { + return new MatOfRect2d(addr); + } + + public MatOfRect2d(Mat m) { + super(m, Range.all()); + if( !empty() && checkVector(_channels, _depth) < 0 ) + throw new IllegalArgumentException("Incompatible Mat"); + //FIXME: do we need release() here? + } + + public MatOfRect2d(Rect2d...a) { + super(); + fromArray(a); + } + + public void alloc(int elemNumber) { + if(elemNumber>0) + super.create(elemNumber, 1, CvType.makeType(_depth, _channels)); + } + + public void fromArray(Rect2d...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) { + Rect2d ap[] = lr.toArray(new Rect2d[0]); + fromArray(ap); + } + + public List toList() { + Rect2d[] ar = toArray(); + return Arrays.asList(ar); + } +} diff --git a/modules/core/misc/java/src/java/core+Rect2d.java b/modules/core/misc/java/src/java/core+Rect2d.java new file mode 100644 index 0000000000..cb83a97727 --- /dev/null +++ b/modules/core/misc/java/src/java/core+Rect2d.java @@ -0,0 +1,100 @@ +package org.opencv.core; + +//javadoc:Rect2d_ +public class Rect2d { + + public double x, y, width, height; + + public Rect2d(double x, double y, double width, double height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + public Rect2d() { + this(0, 0, 0, 0); + } + + public Rect2d(Point p1, Point p2) { + x = (double) (p1.x < p2.x ? p1.x : p2.x); + y = (double) (p1.y < p2.y ? p1.y : p2.y); + width = (double) (p1.x > p2.x ? p1.x : p2.x) - x; + height = (double) (p1.y > p2.y ? p1.y : p2.y) - y; + } + + public Rect2d(Point p, Size s) { + this((double) p.x, (double) p.y, (double) s.width, (double) s.height); + } + + public Rect2d(double[] vals) { + set(vals); + } + + public void set(double[] vals) { + if (vals != null) { + x = vals.length > 0 ? (double) vals[0] : 0; + y = vals.length > 1 ? (double) vals[1] : 0; + width = vals.length > 2 ? (double) vals[2] : 0; + height = vals.length > 3 ? (double) vals[3] : 0; + } else { + x = 0; + y = 0; + width = 0; + height = 0; + } + } + + public Rect2d clone() { + return new Rect2d(x, y, width, height); + } + + public Point tl() { + return new Point(x, y); + } + + public Point br() { + return new Point(x + width, y + height); + } + + public Size size() { + return new Size(width, height); + } + + public double area() { + return width * height; + } + + public boolean contains(Point p) { + return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(height); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(width); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(x); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(y); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Rect2d)) return false; + Rect2d it = (Rect2d) obj; + return x == it.x && y == it.y && width == it.width && height == it.height; + } + + @Override + public String toString() { + return "{" + x + ", " + y + ", " + width + "x" + height + "}"; + } +} diff --git a/modules/java/generator/gen_java.py b/modules/java/generator/gen_java.py index fc8cc1440b..3c8da0b450 100755 --- a/modules/java/generator/gen_java.py +++ b/modules/java/generator/gen_java.py @@ -207,6 +207,7 @@ type_dict = { "vector_KeyPoint" : { "j_type" : "MatOfKeyPoint", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "std::vector %(n)s", "suffix" : "J" }, "vector_DMatch" : { "j_type" : "MatOfDMatch", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "std::vector %(n)s", "suffix" : "J" }, "vector_Rect" : { "j_type" : "MatOfRect", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "std::vector %(n)s", "suffix" : "J" }, + "vector_Rect2d" : { "j_type" : "MatOfRect2d", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "std::vector %(n)s", "suffix" : "J" }, "vector_uchar" : { "j_type" : "MatOfByte", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "std::vector %(n)s", "suffix" : "J" }, "vector_char" : { "j_type" : "MatOfByte", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "std::vector %(n)s", "suffix" : "J" }, "vector_int" : { "j_type" : "MatOfInt", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "std::vector %(n)s", "suffix" : "J" }, @@ -261,6 +262,9 @@ type_dict = { "Rect" : { "j_type" : "Rect", "jn_args" : (("int", ".x"), ("int", ".y"), ("int", ".width"), ("int", ".height")), "jni_var" : "Rect %(n)s(%(n)s_x, %(n)s_y, %(n)s_width, %(n)s_height)", "jni_type" : "jdoubleArray", "suffix" : "IIII"}, + "Rect2d" : { "j_type" : "Rect2d", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".width"), ("double", ".height")), + "jni_var" : "Rect %(n)s(%(n)s_x, %(n)s_y, %(n)s_width, %(n)s_height)", "jni_type" : "jdoubleArray", + "suffix" : "DDDD"}, "Size" : { "j_type" : "Size", "jn_args" : (("double", ".width"), ("double", ".height")), "jni_var" : "Size %(n)s((int)%(n)s_width, (int)%(n)s_height)", "jni_type" : "jdoubleArray", "suffix" : "DD"}, @@ -825,7 +829,7 @@ class ClassInfo(GeneralInfo): j_type = type_dict[ctype]['j_type'] elif ctype in ("Algorithm"): j_type = ctype - if j_type in ( "CvType", "Mat", "Point", "Point3", "Range", "Rect", "RotatedRect", "Scalar", "Size", "TermCriteria", "Algorithm" ): + if j_type in ( "CvType", "Mat", "Point", "Point3", "Range", "Rect", "Rect2d", "RotatedRect", "Scalar", "Size", "TermCriteria", "Algorithm" ): self.imports.add("org.opencv.core." + j_type) if j_type == 'String': self.imports.add("java.lang.String") diff --git a/modules/java/generator/src/cpp/converters.cpp b/modules/java/generator/src/cpp/converters.cpp index 3c771ce734..7ce7456525 100644 --- a/modules/java/generator/src/cpp/converters.cpp +++ b/modules/java/generator/src/cpp/converters.cpp @@ -92,6 +92,19 @@ void vector_Rect_to_Mat(std::vector& v_rect, Mat& mat) mat = Mat(v_rect, true); } +//vector_Rect2d + +void Mat_to_vector_Rect2d(Mat& mat, std::vector& v_rect) +{ + v_rect.clear(); + CHECK_MAT(mat.type()==CV_64FC4 && mat.cols==1); + v_rect = (std::vector) mat; +} + +void vector_Rect2d_to_Mat(std::vector& v_rect, Mat& mat) +{ + mat = Mat(v_rect, true); +} //vector_Point void Mat_to_vector_Point(Mat& mat, std::vector& v_point) diff --git a/modules/java/generator/src/cpp/converters.h b/modules/java/generator/src/cpp/converters.h index 257f9449e3..25077b10c2 100644 --- a/modules/java/generator/src/cpp/converters.h +++ b/modules/java/generator/src/cpp/converters.h @@ -19,6 +19,9 @@ void vector_char_to_Mat(std::vector& v_char, cv::Mat& mat); void Mat_to_vector_Rect(cv::Mat& mat, std::vector& v_rect); void vector_Rect_to_Mat(std::vector& v_rect, cv::Mat& mat); +void Mat_to_vector_Rect2d(cv::Mat& mat, std::vector& v_rect); +void vector_Rect2d_to_Mat(std::vector& v_rect, cv::Mat& mat); + void Mat_to_vector_Point(cv::Mat& mat, std::vector& v_point); void Mat_to_vector_Point2f(cv::Mat& mat, std::vector& v_point); diff --git a/modules/java/generator/src/java/utils+Converters.java b/modules/java/generator/src/java/utils+Converters.java index bd3bb64927..c0575a6665 100644 --- a/modules/java/generator/src/java/utils+Converters.java +++ b/modules/java/generator/src/java/utils+Converters.java @@ -14,6 +14,7 @@ import org.opencv.core.MatOfPoint3f; import org.opencv.core.Point; import org.opencv.core.Point3; import org.opencv.core.Rect; +import org.opencv.core.Rect2d; import org.opencv.core.DMatch; import org.opencv.core.KeyPoint; @@ -435,6 +436,42 @@ public class Converters { } } + public static Mat vector_Rect2d_to_Mat(List rs) { + Mat res; + int count = (rs != null) ? rs.size() : 0; + if (count > 0) { + res = new Mat(count, 1, CvType.CV_64FC4); + double[] buff = new double[4 * count]; + for (int i = 0; i < count; i++) { + Rect2d r = rs.get(i); + buff[4 * i] = r.x; + buff[4 * i + 1] = r.y; + buff[4 * i + 2] = r.width; + buff[4 * i + 3] = r.height; + } + res.put(0, 0, buff); + } else { + res = new Mat(); + } + return res; + } + + public static void Mat_to_vector_Rect2d(Mat m, List rs) { + if (rs == null) + throw new java.lang.IllegalArgumentException("rs == null"); + int count = m.rows(); + if (CvType.CV_64FC4 != m.type() || m.cols() != 1) + throw new java.lang.IllegalArgumentException( + "CvType.CV_64FC4 != m.type() || m.rows()!=1\n" + m); + + rs.clear(); + double[] buff = new double[4 * count]; + m.get(0, 0, buff); + for (int i = 0; i < count; i++) { + rs.add(new Rect2d(buff[4 * i], buff[4 * i + 1], buff[4 * i + 2], buff[4 * i + 3])); + } + } + public static Mat vector_KeyPoint_to_Mat(List kps) { Mat res; int count = (kps != null) ? kps.size() : 0;