From 92afe9e40a1c1e44f4b87cc30d38b13b342bdee3 Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Wed, 10 Aug 2011 14:09:22 +0000 Subject: [PATCH] Java API: fixed input List> arguments --- .../src/org/opencv/test/core/MatTest.java | 15 +- .../src/org/opencv/test/utils/ConvertersTest.java | 26 +- modules/java/gen_java.py | 11 +- modules/java/src/cpp/converters.cpp | 337 +++++++++++---------- modules/java/src/java/core+Mat.java | 2 +- modules/java/src/java/utils+Converters.java | 31 +- 6 files changed, 215 insertions(+), 207 deletions(-) diff --git a/modules/java/android_test/src/org/opencv/test/core/MatTest.java b/modules/java/android_test/src/org/opencv/test/core/MatTest.java index e01dd18..1c6ee94 100644 --- a/modules/java/android_test/src/org/opencv/test/core/MatTest.java +++ b/modules/java/android_test/src/org/opencv/test/core/MatTest.java @@ -1,6 +1,7 @@ package org.opencv.test.core; import org.opencv.core.Core; +import org.opencv.core.CvException; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Range; @@ -175,11 +176,19 @@ public class MatTest extends OpenCVTestCase { } public void testDiagMat() { - dst = Mat.diag(gray255); + Mat diagVector = new Mat(matSize, 1, CvType.CV_32F, new Scalar(1)); - truth = new Mat(1, matSize, CvType.CV_8U, new Scalar(255)); + dst = Mat.diag(diagVector); - assertMatEqual(truth, dst); + assertMatEqual(grayE_32f, dst, EPS); + } + + public void testDiagMat_sqrMatrix() { + try { + dst = Mat.diag(gray255); + } catch (CvException e) { + // expected + } } public void testDot() { diff --git a/modules/java/android_test/src/org/opencv/test/utils/ConvertersTest.java b/modules/java/android_test/src/org/opencv/test/utils/ConvertersTest.java index f34303b..fae1b90 100644 --- a/modules/java/android_test/src/org/opencv/test/utils/ConvertersTest.java +++ b/modules/java/android_test/src/org/opencv/test/utils/ConvertersTest.java @@ -1,9 +1,5 @@ package org.opencv.test.utils; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.Point; @@ -12,9 +8,11 @@ import org.opencv.core.Rect; import org.opencv.features2d.DMatch; import org.opencv.features2d.KeyPoint; import org.opencv.test.OpenCVTestCase; -import org.opencv.test.OpenCVTestRunner; import org.opencv.utils.Converters; +import java.util.ArrayList; +import java.util.List; + public class ConvertersTest extends OpenCVTestCase { public void testMat_to_vector_char() { @@ -481,17 +479,6 @@ public class ConvertersTest extends OpenCVTestCase { public void testVector_vector_char_to_Mat() { fail("Not yet implemented"); - List> llb = new ArrayList>(); - byte value1 = 1; - byte value2 = 2; - byte value3 = 3; - byte value4 = 4; - llb.add(Arrays.asList(new Byte(value1), new Byte(value2), new Byte(value3), new Byte(value4))); - - dst = Converters.vector_vector_char_to_Mat(llb); - - OpenCVTestRunner.Log(dst.toString()); - OpenCVTestRunner.Log(dst.dump()); } public void testVector_vector_DMatch_to_Mat() { @@ -503,13 +490,6 @@ public class ConvertersTest extends OpenCVTestCase { } public void testVector_vector_Point_to_Mat() { - List> points = new ArrayList>(); - points.add(Arrays.asList(new Point(1, 1), new Point(7, 1), new Point(7, 6), new Point(1, 6), - new Point(5, 5), new Point(8, 9))); - - dst = Converters.vector_vector_Point_to_Mat(points); - // TODO: returns random dst matrix - // assertMatEqual(); fail("Not yet implemented"); } diff --git a/modules/java/gen_java.py b/modules/java/gen_java.py index 2694509..562499a 100644 --- a/modules/java/gen_java.py +++ b/modules/java/gen_java.py @@ -1137,7 +1137,12 @@ extern "C" { c_prologue.append( type_dict[a.ctype]["jni_var"] % {"n" : a.name} + ";" ) c_prologue.append( "Mat& %(n)s_mat = *((Mat*)%(n)s_mat_nativeObj)" % {"n" : a.name} + ";" ) if "I" in a.out or not a.out: - j_prologue.append( "Mat %(n)s_mat = Converters.%(t)s_to_Mat(%(n)s);" % {"n" : a.name, "t" : a.ctype} ) + if a.ctype.startswith("vector_vector_"): + self.classes[fi.classname or self.Module].imports.add("java.util.ArrayList") + j_prologue.append( "List %(n)s_tmplm = new ArrayList((%(n)s != null) ? %(n)s.size() : 0);" % {"n" : a.name } ) + j_prologue.append( "Mat %(n)s_mat = Converters.%(t)s_to_Mat(%(n)s, %(n)s_tmplm);" % {"n" : a.name, "t" : a.ctype} ) + else: + j_prologue.append( "Mat %(n)s_mat = Converters.%(t)s_to_Mat(%(n)s);" % {"n" : a.name, "t" : a.ctype} ) c_prologue.append( "Mat_to_%(t)s( %(n)s_mat, %(n)s );" % {"n" : a.name, "t" : a.ctype} ) else: j_prologue.append( "Mat %s_mat = new Mat();" % a.name ) @@ -1246,8 +1251,8 @@ extern "C" { ret = ret, \ ret_val = ret_val, \ tail = tail, \ - prologue = " ".join(j_prologue), \ - epilogue = " ".join(j_epilogue), \ + prologue = "\n ".join(j_prologue), \ + epilogue = "\n ".join(j_epilogue), \ static=static, \ j_type=type_dict[fi.ctype]["j_type"], \ j_name=fi.jname, \ diff --git a/modules/java/src/cpp/converters.cpp b/modules/java/src/cpp/converters.cpp index d5f6fb9..40cdd3b 100644 --- a/modules/java/src/cpp/converters.cpp +++ b/modules/java/src/cpp/converters.cpp @@ -17,14 +17,14 @@ using namespace cv; void Mat_to_vector_int(Mat& mat, vector& v_int) { - v_int.clear(); - CHECK_MAT(mat.type()==CV_32SC1 && mat.cols==1); - v_int = (vector) mat; + v_int.clear(); + CHECK_MAT(mat.type()==CV_32SC1 && mat.cols==1); + v_int = (vector) mat; } void vector_int_to_Mat(vector& v_int, Mat& mat) { - mat = Mat(v_int, true); + mat = Mat(v_int, true); } @@ -32,14 +32,14 @@ void vector_int_to_Mat(vector& v_int, Mat& mat) void Mat_to_vector_double(Mat& mat, vector& v_double) { - v_double.clear(); - CHECK_MAT(mat.type()==CV_64FC1 && mat.cols==1); - v_double = (vector) mat; + v_double.clear(); + CHECK_MAT(mat.type()==CV_64FC1 && mat.cols==1); + v_double = (vector) mat; } void vector_double_to_Mat(vector& v_double, Mat& mat) { - mat = Mat(v_double, true); + mat = Mat(v_double, true); } @@ -47,14 +47,14 @@ void vector_double_to_Mat(vector& v_double, Mat& mat) void Mat_to_vector_float(Mat& mat, vector& v_float) { - v_float.clear(); - CHECK_MAT(mat.type()==CV_32FC1 && mat.cols==1); - v_float = (vector) mat; + v_float.clear(); + CHECK_MAT(mat.type()==CV_32FC1 && mat.cols==1); + v_float = (vector) mat; } void vector_float_to_Mat(vector& v_float, Mat& mat) { - mat = Mat(v_float, true); + mat = Mat(v_float, true); } @@ -62,26 +62,26 @@ void vector_float_to_Mat(vector& v_float, Mat& mat) void Mat_to_vector_uchar(Mat& mat, vector& v_uchar) { - v_uchar.clear(); - CHECK_MAT(mat.type()==CV_8UC1 && mat.cols==1); - v_uchar = (vector) mat; + v_uchar.clear(); + CHECK_MAT(mat.type()==CV_8UC1 && mat.cols==1); + v_uchar = (vector) mat; } void vector_uchar_to_Mat(vector& v_uchar, Mat& mat) { - mat = Mat(v_uchar, true); + mat = Mat(v_uchar, true); } void Mat_to_vector_char(Mat& mat, vector& v_char) { - v_char.clear(); - CHECK_MAT(mat.type()==CV_8SC1 && mat.cols==1); - v_char = (vector) mat; + v_char.clear(); + CHECK_MAT(mat.type()==CV_8SC1 && mat.cols==1); + v_char = (vector) mat; } void vector_char_to_Mat(vector& v_char, Mat& mat) { - mat = Mat(v_char, true); + mat = Mat(v_char, true); } @@ -89,95 +89,95 @@ void vector_char_to_Mat(vector& v_char, Mat& mat) void Mat_to_vector_Rect(Mat& mat, vector& v_rect) { - v_rect.clear(); - CHECK_MAT(mat.type()==CV_32SC4 && mat.cols==1); - v_rect = (vector) mat; + v_rect.clear(); + CHECK_MAT(mat.type()==CV_32SC4 && mat.cols==1); + v_rect = (vector) mat; } void vector_Rect_to_Mat(vector& v_rect, Mat& mat) { - mat = Mat(v_rect, true); + mat = Mat(v_rect, true); } //vector_Point void Mat_to_vector_Point(Mat& mat, vector& v_point) { - v_point.clear(); - CHECK_MAT(mat.type()==CV_32SC2 && mat.cols==1); - v_point = (vector) mat; + v_point.clear(); + CHECK_MAT(mat.type()==CV_32SC2 && mat.cols==1); + v_point = (vector) mat; } //vector_Point2f void Mat_to_vector_Point2f(Mat& mat, vector& v_point) { - v_point.clear(); - CHECK_MAT(mat.type()==CV_32FC2 && mat.cols==1); - v_point = (vector) mat; + v_point.clear(); + CHECK_MAT(mat.type()==CV_32FC2 && mat.cols==1); + v_point = (vector) mat; } //vector_Point2d void Mat_to_vector_Point2d(Mat& mat, vector& v_point) { - v_point.clear(); - CHECK_MAT(mat.type()==CV_64FC2 && mat.cols==1); - v_point = (vector) mat; + v_point.clear(); + CHECK_MAT(mat.type()==CV_64FC2 && mat.cols==1); + v_point = (vector) mat; } //vector_Point3i void Mat_to_vector_Point3i(Mat& mat, vector& v_point) { - v_point.clear(); - CHECK_MAT(mat.type()==CV_32SC3 && mat.cols==1); - v_point = (vector) mat; + v_point.clear(); + CHECK_MAT(mat.type()==CV_32SC3 && mat.cols==1); + v_point = (vector) mat; } //vector_Point3f void Mat_to_vector_Point3f(Mat& mat, vector& v_point) { - v_point.clear(); - CHECK_MAT(mat.type()==CV_32FC3 && mat.cols==1); - v_point = (vector) mat; + v_point.clear(); + CHECK_MAT(mat.type()==CV_32FC3 && mat.cols==1); + v_point = (vector) mat; } //vector_Point3d void Mat_to_vector_Point3d(Mat& mat, vector& v_point) { - v_point.clear(); - CHECK_MAT(mat.type()==CV_64FC3 && mat.cols==1); - v_point = (vector) mat; + v_point.clear(); + CHECK_MAT(mat.type()==CV_64FC3 && mat.cols==1); + v_point = (vector) mat; } void vector_Point_to_Mat(vector& v_point, Mat& mat) { - mat = Mat(v_point, true); + mat = Mat(v_point, true); } void vector_Point2f_to_Mat(vector& v_point, Mat& mat) { - mat = Mat(v_point, true); + mat = Mat(v_point, true); } void vector_Point2d_to_Mat(vector& v_point, Mat& mat) { - mat = Mat(v_point, true); + mat = Mat(v_point, true); } void vector_Point3i_to_Mat(vector& v_point, Mat& mat) { - mat = Mat(v_point, true); + mat = Mat(v_point, true); } void vector_Point3f_to_Mat(vector& v_point, Mat& mat) { - mat = Mat(v_point, true); + mat = Mat(v_point, true); } void vector_Point3d_to_Mat(vector& v_point, Mat& mat) { - mat = Mat(v_point, true); + mat = Mat(v_point, true); } @@ -186,56 +186,57 @@ void Mat_to_vector_KeyPoint(Mat& mat, vector& v_kp) { v_kp.clear(); CHECK_MAT(mat.type()==CV_64FC(7) && mat.cols==1); - for(int i=0; i v = mat.at< Vec >(i, 0); - KeyPoint kp((float)v[0], (float)v[1], (float)v[2], (float)v[3], (float)v[4], (int)v[5], (int)v[6]); - v_kp.push_back(kp); - } + for(int i=0; i v = mat.at< Vec >(i, 0); + KeyPoint kp((float)v[0], (float)v[1], (float)v[2], (float)v[3], (float)v[4], (int)v[5], (int)v[6]); + v_kp.push_back(kp); + } return; } void vector_KeyPoint_to_Mat(vector& v_kp, Mat& mat) { - int count = v_kp.size(); - mat.create(count, 1, CV_64FC(7)); - for(int i=0; i >(i, 0) = Vec(kp.pt.x, kp.pt.y, kp.size, kp.angle, kp.response, kp.octave, kp.class_id); - } + int count = v_kp.size(); + mat.create(count, 1, CV_64FC(7)); + for(int i=0; i >(i, 0) = Vec(kp.pt.x, kp.pt.y, kp.size, kp.angle, kp.response, kp.octave, kp.class_id); + } } //vector_Mat void Mat_to_vector_Mat(cv::Mat& mat, std::vector& v_mat) { - v_mat.clear(); - if(mat.type() == CV_32SC2 && mat.cols == 1) - { - for(int i=0; i a = mat.at< Vec >(i, 0); - long long addr = (((long long)a[0])<<32) | a[1]; - Mat& m = *( (Mat*) addr ); - v_mat.push_back(m); - } - } else { - LOGD("Mat_to_vector_Mat() FAILED: mat.type() == CV_32SC2 && mat.cols == 1"); - } + v_mat.clear(); + if(mat.type() == CV_32SC2 && mat.cols == 1) + { + v_mat.reserve(mat.rows); + for(int i=0; i a = mat.at< Vec >(i, 0); + long long addr = (((long long)a[0])<<32) | a[1]; + Mat& m = *( (Mat*) addr ); + v_mat.push_back(m); + } + } else { + LOGD("Mat_to_vector_Mat() FAILED: mat.type() == CV_32SC2 && mat.cols == 1"); + } } void vector_Mat_to_Mat(std::vector& v_mat, cv::Mat& mat) { - int count = v_mat.size(); - mat.create(count, 1, CV_32SC2); - for(int i=0; i >(i, 0) = Vec(addr>>32, addr&0xffffffff); - } + int count = v_mat.size(); + mat.create(count, 1, CV_32SC2); + for(int i=0; i >(i, 0) = Vec(addr>>32, addr&0xffffffff); + } } //vector_DMatch @@ -243,137 +244,137 @@ void Mat_to_vector_DMatch(Mat& mat, vector& v_dm) { v_dm.clear(); CHECK_MAT(mat.type()==CV_64FC4 && mat.cols==1); - for(int i=0; i v = mat.at< Vec >(i, 0); - DMatch dm((int)v[0], (int)v[1], (int)v[2], (float)v[3]); - v_dm.push_back(dm); - } + for(int i=0; i v = mat.at< Vec >(i, 0); + DMatch dm((int)v[0], (int)v[1], (int)v[2], (float)v[3]); + v_dm.push_back(dm); + } return; } void vector_DMatch_to_Mat(vector& v_dm, Mat& mat) { - int count = v_dm.size(); - mat.create(count, 1, CV_64FC4); - for(int i=0; i >(i, 0) = Vec(dm.queryIdx, dm.trainIdx, dm.imgIdx, dm.distance); - } + int count = v_dm.size(); + mat.create(count, 1, CV_64FC4); + for(int i=0; i >(i, 0) = Vec(dm.queryIdx, dm.trainIdx, dm.imgIdx, dm.distance); + } } void Mat_to_vector_vector_Point(Mat& mat, vector< vector< Point > >& vv_pt) { - vector vm; - vm.reserve( mat.rows ); - Mat_to_vector_Mat(mat, vm); - for(size_t i=0; i vpt; - Mat_to_vector_Point(vm[i], vpt); - vv_pt.push_back(vpt); - } + vector vm; + vm.reserve( mat.rows ); + Mat_to_vector_Mat(mat, vm); + for(size_t i=0; i vpt; + Mat_to_vector_Point(vm[i], vpt); + vv_pt.push_back(vpt); + } } void Mat_to_vector_vector_KeyPoint(Mat& mat, vector< vector< KeyPoint > >& vv_kp) { - vector vm; - vm.reserve( mat.rows ); - Mat_to_vector_Mat(mat, vm); - for(size_t i=0; i vkp; - Mat_to_vector_KeyPoint(vm[i], vkp); - vv_kp.push_back(vkp); - } + vector vm; + vm.reserve( mat.rows ); + Mat_to_vector_Mat(mat, vm); + for(size_t i=0; i vkp; + Mat_to_vector_KeyPoint(vm[i], vkp); + vv_kp.push_back(vkp); + } } void vector_vector_KeyPoint_to_Mat(vector< vector< KeyPoint > >& vv_kp, Mat& mat) { - vector vm; - vm.reserve( vv_kp.size() ); - for(size_t i=0; i vm; + vm.reserve( vv_kp.size() ); + for(size_t i=0; i >& vv_dm) { - vector vm; - vm.reserve( mat.rows ); - Mat_to_vector_Mat(mat, vm); - for(size_t i=0; i vdm; - Mat_to_vector_DMatch(vm[i], vdm); - vv_dm.push_back(vdm); - } + vector vm; + vm.reserve( mat.rows ); + Mat_to_vector_Mat(mat, vm); + for(size_t i=0; i vdm; + Mat_to_vector_DMatch(vm[i], vdm); + vv_dm.push_back(vdm); + } } void vector_vector_DMatch_to_Mat(vector< vector< DMatch > >& vv_dm, Mat& mat) { - vector vm; - vm.reserve( vv_dm.size() ); - for(size_t i=0; i vm; + vm.reserve( vv_dm.size() ); + for(size_t i=0; i >& vv_ch) { - vector vm; - vm.reserve( mat.rows ); - Mat_to_vector_Mat(mat, vm); - for(size_t i=0; i vch; - Mat_to_vector_char(vm[i], vch); - vv_ch.push_back(vch); - } + vector vm; + vm.reserve( mat.rows ); + Mat_to_vector_Mat(mat, vm); + for(size_t i=0; i vch; + Mat_to_vector_char(vm[i], vch); + vv_ch.push_back(vch); + } } void vector_vector_char_to_Mat(vector< vector< char > >& vv_ch, Mat& mat) { - vector vm; - vm.reserve( vv_ch.size() ); - for(size_t i=0; i vm; + vm.reserve( vv_ch.size() ); + for(size_t i=0; i >& vv_pt, Mat& mat) { - vector vm; - vm.reserve( vv_pt.size() ); - for(size_t i=0; i vm; + vm.reserve( vv_pt.size() ); + for(size_t i=0; i& v_vec, Mat& mat) { - mat = Mat(v_vec, true); + mat = Mat(v_vec, true); } void vector_Vec6f_to_Mat(vector& v_vec, Mat& mat) { - mat = Mat(v_vec, true); + mat = Mat(v_vec, true); } diff --git a/modules/java/src/java/core+Mat.java b/modules/java/src/java/core+Mat.java index 6a8c8df..a839d09 100644 --- a/modules/java/src/java/core+Mat.java +++ b/modules/java/src/java/core+Mat.java @@ -108,7 +108,7 @@ public class Mat { public Mat(Mat m, Rect roi) { - nativeObj = n_Mat(m.nativeObj, roi.x, roi.y, roi.width, roi.height); + nativeObj = n_Mat(m.nativeObj, roi.x, roi.x + roi.width, roi.y, roi.y + roi.height); return; } diff --git a/modules/java/src/java/utils+Converters.java b/modules/java/src/java/utils+Converters.java index b871f35..21bb92b 100644 --- a/modules/java/src/java/utils+Converters.java +++ b/modules/java/src/java/utils+Converters.java @@ -469,12 +469,11 @@ public class Converters { } } - // vector_vector_Point - public static Mat vector_vector_Point_to_Mat(List> pts) { + // vector_vector_Point + public static Mat vector_vector_Point_to_Mat(List> pts, List mats) { Mat res; int lCount = (pts != null) ? pts.size() : 0; if (lCount > 0) { - List mats = new ArrayList(lCount); for (List lpt : pts) mats.add(vector_Point_to_Mat(lpt)); res = vector_Mat_to_Mat(mats); @@ -484,12 +483,28 @@ public class Converters { return res; } + // vector_vector_Point2f + public static void Mat_to_vector_vector_Point2f(Mat m, List> pts) { + if (pts == null) + throw new java.lang.IllegalArgumentException("Output List can't be null"); + + if (m == null) + throw new java.lang.IllegalArgumentException("Input Mat can't be null"); + + List mats = new ArrayList(m.rows()); + Mat_to_vector_Mat(m, mats); + List pt = new ArrayList(); + for (Mat mi : mats) { + Mat_to_vector_Point2f(mi, pt); + pts.add(pt); + } + } + // vector_vector_KeyPoint - public static Mat vector_vector_KeyPoint_to_Mat(List> kps) { + public static Mat vector_vector_KeyPoint_to_Mat(List> kps, List mats) { Mat res; int lCount = (kps != null) ? kps.size() : 0; if (lCount > 0) { - List mats = new ArrayList(lCount); for (List lkp : kps) mats.add(vector_KeyPoint_to_Mat(lkp)); res = vector_Mat_to_Mat(mats); @@ -569,11 +584,10 @@ public class Converters { } // vector_vector_DMatch - public static Mat vector_vector_DMatch_to_Mat(List> lldm) { + public static Mat vector_vector_DMatch_to_Mat(List> lldm, List mats) { Mat res; int lCount = (lldm != null) ? lldm.size() : 0; if (lCount > 0) { - List mats = new ArrayList(lCount); for (List ldm : lldm) mats.add(vector_DMatch_to_Mat(ldm)); res = vector_Mat_to_Mat(mats); @@ -600,11 +614,10 @@ public class Converters { } // vector_vector_char - public static Mat vector_vector_char_to_Mat(List> llb) { + public static Mat vector_vector_char_to_Mat(List> llb, List mats) { Mat res; int lCount = (llb != null) ? llb.size() : 0; if (lCount > 0) { - List mats = new ArrayList(lCount); for (List lb : llb) mats.add(vector_char_to_Mat(lb)); res = vector_Mat_to_Mat(mats); -- 2.7.4