From f4e28f87d8d6bcf52dcdd86b58c9b2bcf770656a Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Tue, 2 Aug 2011 13:10:23 +0000 Subject: [PATCH] Java API: new converters, findFundamentalMat/cornerSubPix/findHomography/solvePnP/solvePnPRansac and their tests are updated --- .../src/org/opencv/test/calib3d/calib3dTest.java | 10 +- .../src/org/opencv/test/imgproc/imgprocTest.java | 4 +- modules/java/gen_java.py | 18 +- modules/java/src/cpp/converters.cpp | 84 +++++++- modules/java/src/cpp/converters.h | 10 + modules/java/src/java/Converters.java | 224 +++++++++++++++++---- 6 files changed, 289 insertions(+), 61 deletions(-) diff --git a/modules/java/android_test/src/org/opencv/test/calib3d/calib3dTest.java b/modules/java/android_test/src/org/opencv/test/calib3d/calib3dTest.java index 3ac115e..9fb677f 100644 --- a/modules/java/android_test/src/org/opencv/test/calib3d/calib3dTest.java +++ b/modules/java/android_test/src/org/opencv/test/calib3d/calib3dTest.java @@ -261,7 +261,7 @@ public class calib3dTest extends OpenCVTestCase { pts2.add(new Point(x, y)); } - Mat fm = Calib3d.findFundamentalMat(Converters.vector_Point2f_to_Mat(pts1), Converters.vector_Point2f_to_Mat(pts2)); + Mat fm = Calib3d.findFundamentalMat(pts1, pts2); truth = new Mat(3,3,CvType.CV_64F); truth.put(0, 0, 0, -0.5, -0.5, 0.5, 0, 0, 0.5, 0, 0); @@ -296,9 +296,7 @@ public class calib3dTest extends OpenCVTestCase { transformedPoints.add(new Point(y, x)); } - Mat hmg = Calib3d.findHomography( - Converters.vector_Point2f_to_Mat(originalPoints), - Converters.vector_Point2f_to_Mat(transformedPoints)); + Mat hmg = Calib3d.findHomography(originalPoints, transformedPoints); truth = new Mat(3, 3, CvType.CV_64F); truth.put(0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1); @@ -543,9 +541,7 @@ public class calib3dTest extends OpenCVTestCase { Mat rvec = new Mat(); Mat tvec = new Mat(); - Calib3d.solvePnP(Converters.vector_Point3f_to_Mat(points3d), - Converters.vector_Point2f_to_Mat(points2d), intrinsics, - new Mat(), rvec, tvec); + Calib3d.solvePnP(points3d, points2d, intrinsics, new Mat(), rvec, tvec); Mat truth_rvec = new Mat(3, 1, CvType.CV_64F); truth_rvec.put(0, 0, 0, Math.PI / 2, 0); diff --git a/modules/java/android_test/src/org/opencv/test/imgproc/imgprocTest.java b/modules/java/android_test/src/org/opencv/test/imgproc/imgprocTest.java index 5b5e786..5abcde5 100644 --- a/modules/java/android_test/src/org/opencv/test/imgproc/imgprocTest.java +++ b/modules/java/android_test/src/org/opencv/test/imgproc/imgprocTest.java @@ -554,9 +554,7 @@ public class imgprocTest extends OpenCVTestCase { Size zeroZone = new Size(-1, -1); TermCriteria criteria = new TermCriteria(2/*TODO: CV_TERMCRIT_EPS*/, 0, 0.01); - Mat cornersMat = Converters.vector_Point2f_to_Mat(corners); - Imgproc.cornerSubPix(img, cornersMat, winSize, zeroZone, criteria); - Converters.Mat_to_vector_Point(cornersMat, corners); + Imgproc.cornerSubPix(img, corners, winSize, zeroZone, criteria); assertPointEquals(truthPosition, corners.get(0), weakEPS); } diff --git a/modules/java/gen_java.py b/modules/java/gen_java.py index d5e24f3..e2a2af5 100644 --- a/modules/java/gen_java.py +++ b/modules/java/gen_java.py @@ -179,6 +179,11 @@ type_dict = { # "complex" : { j_type : "?", jn_args : (("", ""),), jn_name : "", jni_var : "", jni_name : "", "suffix" : "?" }, "vector_Point": { "j_type" : "List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_Point2f": { "j_type" : "List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_Point2d": { "j_type" : "List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_Point3i": { "j_type" : "List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_Point3f": { "j_type" : "List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_Point3d": { "j_type" : "List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, "vector_Mat" : { "j_type" : "List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, "vector_KeyPoint" : { "j_type" : "List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, "vector_Rect" : { "j_type" : "List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, @@ -200,13 +205,13 @@ type_dict = { "Point2d" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")), "jni_var" : "Point2d %(n)s(%(n)s_x, %(n)s_y)", "jni_type" : "jdoubleArray", "suffix" : "DD"}, - "Point3i" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")), + "Point3i" : { "j_type" : "Point3", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")), "jni_var" : "Point3i %(n)s((int)%(n)s_x, (int)%(n)s_y, (int)%(n)s_z)", "jni_type" : "jdoubleArray", "suffix" : "DDD"}, - "Point3f" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")), + "Point3f" : { "j_type" : "Point3", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")), "jni_var" : "Point3f %(n)s((float)%(n)s_x, (float)%(n)s_y, (float)%(n)s_z)", "jni_type" : "jdoubleArray", "suffix" : "DDD"}, - "Point3d" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")), + "Point3d" : { "j_type" : "Point3", "jn_args" : (("double", ".x"), ("double", ".y"), ("double", ".z")), "jni_var" : "Point3d %(n)s(%(n)s_x, %(n)s_y, %(n)s_z)", "jni_type" : "jdoubleArray", "suffix" : "DDD"}, "KeyPoint": { "j_type" : "KeyPoint", "jn_args" : (("float", ".x"), ("float", ".y"), ("float", ".size"), @@ -457,7 +462,12 @@ func_arg_fix = { 'randu' : { 'low' : 'double', 'high' : 'double', }, 'randn' : { 'mean' : 'double', 'stddev' : 'double', }, 'inRange' : { 'lowerb' : 'Scalar', 'upperb' : 'Scalar', }, - 'goodFeaturesToTrack' : { 'corners' : 'vector_Point' }, + 'goodFeaturesToTrack' : { 'corners' : 'vector_Point', }, + 'findFundamentalMat' : { 'points1' : 'vector_Point2d', 'points2' : 'vector_Point2d', }, + 'cornerSubPix' : { 'corners' : 'vector_Point2f', }, + 'findHomography' : { 'srcPoints' : 'vector_Point2f', 'dstPoints' : 'vector_Point2f', }, + 'solvePnP' : { 'objectPoints' : 'vector_Point3f', 'imagePoints' : 'vector_Point2f', }, + 'solvePnPRansac' : { 'objectPoints' : 'vector_Point3f', 'imagePoints' : 'vector_Point2f', }, }, # '', i.e. empty class } # func_arg_fix diff --git a/modules/java/src/cpp/converters.cpp b/modules/java/src/cpp/converters.cpp index a2ffb7c..d039a05 100644 --- a/modules/java/src/cpp/converters.cpp +++ b/modules/java/src/cpp/converters.cpp @@ -10,7 +10,7 @@ using namespace cv; -#define CHECK_MAT(cond) if(cond){ LOGD(#cond); return; } +#define CHECK_MAT(cond) if(!(cond)){ LOGD("FAILED: " #cond); return; } // vector_int @@ -18,7 +18,7 @@ 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); + CHECK_MAT(mat.type()==CV_32SC1 && mat.cols==1); v_int = (vector) mat; } @@ -33,7 +33,7 @@ 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); + CHECK_MAT(mat.type()==CV_64FC1 && mat.cols==1); v_double = (vector) mat; } @@ -48,7 +48,7 @@ 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); + CHECK_MAT(mat.type()==CV_32FC1 && mat.cols==1); v_float = (vector) mat; } @@ -63,7 +63,7 @@ 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); + CHECK_MAT(mat.type()==CV_8UC1 && mat.cols==1); v_uchar = (vector) mat; } @@ -73,7 +73,7 @@ void Mat_to_vector_uchar(Mat& mat, vector& v_uchar) void Mat_to_vector_Rect(Mat& mat, vector& v_rect) { v_rect.clear(); - CHECK_MAT(mat.type()!= CV_32SC4 || mat.cols!=1); + CHECK_MAT(mat.type()==CV_32SC4 && mat.cols==1); v_rect = (vector) mat; } @@ -87,22 +87,88 @@ void vector_Rect_to_Mat(vector& v_rect, Mat& mat) void Mat_to_vector_Point(Mat& mat, vector& v_point) { v_point.clear(); - CHECK_MAT(mat.type()!= CV_32SC2 || mat.cols!=1); + 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; +} + +//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; +} + + +//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; +} + +//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; +} + +//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; +} + void vector_Point_to_Mat(vector& v_point, Mat& mat) { mat = Mat(v_point); } +void vector_Point2f_to_Mat(vector& v_point, Mat& mat) +{ + mat = Mat(v_point); +} + +void vector_Point2d_to_Mat(vector& v_point, Mat& mat) +{ + mat = Mat(v_point); +} + +void vector_Point3i_to_Mat(vector& v_point, Mat& mat) +{ + mat = Mat(v_point); +} + +void vector_Point3f_to_Mat(vector& v_point, Mat& mat) +{ + mat = Mat(v_point); +} + +void vector_Point3d_to_Mat(vector& v_point, Mat& mat) +{ + mat = Mat(v_point); +} + //vector_KeyPoint void Mat_to_vector_KeyPoint(Mat& mat, vector& v_kp) { v_kp.clear(); - CHECK_MAT(mat.type()!= CV_64FC(7) || mat.cols!=1); + CHECK_MAT(mat.type()==CV_64FC(7) && mat.cols==1); for(int i=0; i v = mat.at< Vec >(i, 0); @@ -138,6 +204,8 @@ void Mat_to_vector_Mat(cv::Mat& mat, std::vector& v_mat) Mat& m = *( (Mat*) addr ); v_mat.push_back(m); } + } else { + LOGD("Mat_to_vector_Mat() FAILED: mat.type() == CV_32SC2 && mat.cols == 1"); } } diff --git a/modules/java/src/cpp/converters.h b/modules/java/src/cpp/converters.h index b774764..fe2c911 100644 --- a/modules/java/src/cpp/converters.h +++ b/modules/java/src/cpp/converters.h @@ -29,8 +29,18 @@ void vector_Rect_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); +void Mat_to_vector_Point2d(cv::Mat& mat, std::vector& v_point); +void Mat_to_vector_Point3i(cv::Mat& mat, std::vector& v_point); +void Mat_to_vector_Point3f(cv::Mat& mat, std::vector& v_point); +void Mat_to_vector_Point3d(cv::Mat& mat, std::vector& v_point); void vector_Point_to_Mat(std::vector& v_point, cv::Mat& mat); +void vector_Point2f_to_Mat(std::vector& v_point, cv::Mat& mat); +void vector_Point2d_to_Mat(std::vector& v_point, cv::Mat& mat); +void vector_Point3i_to_Mat(std::vector& v_point, cv::Mat& mat); +void vector_Point3f_to_Mat(std::vector& v_point, cv::Mat& mat); +void vector_Point3d_to_Mat(std::vector& v_point, cv::Mat& mat); void Mat_to_vector_KeyPoint(cv::Mat& mat, std::vector& v_kp); diff --git a/modules/java/src/java/Converters.java b/modules/java/src/java/Converters.java index 300eec3..95c0ca9 100644 --- a/modules/java/src/java/Converters.java +++ b/modules/java/src/java/Converters.java @@ -12,74 +12,220 @@ import org.opencv.features2d.KeyPoint; public class Converters { public static Mat vector_Point_to_Mat(List pts) { - Mat res; - int count = (pts!=null) ? pts.size() : 0; - if(count>0){ - res = new Mat(count, 1, CvType.CV_32SC2); - int[] buff = new int[count*2]; - for(int i=0; i pts) { + return vector_Point_to_Mat(pts, CvType.CV_32F); + } + + public static Mat vector_Point2d_to_Mat(List pts) { + return vector_Point_to_Mat(pts, CvType.CV_64F); + } + + public static Mat vector_Point_to_Mat(List pts, int typeDepth) { Mat res; int count = (pts!=null) ? pts.size() : 0; if(count>0){ - res = new Mat(count, 1, CvType.CV_32FC2); - float[] buff = new float[count*2]; - for(int i=0; i pts) { + return vector_Point3_to_Mat(pts, CvType.CV_32S); + } + public static Mat vector_Point3f_to_Mat(List pts) { + return vector_Point3_to_Mat(pts, CvType.CV_32F); + } + + public static Mat vector_Point3d_to_Mat(List pts) { + return vector_Point3_to_Mat(pts, CvType.CV_64F); + } + + public static Mat vector_Point3_to_Mat(List pts, int typeDepth) { Mat res; int count = (pts!=null) ? pts.size() : 0; if(count>0){ - res = new Mat(count, 1, CvType.CV_32FC3); - float[] buff = new float[count*3]; - for(int i=0; i pts) { + Mat_to_vector_Point(m, pts); + } + + public static void Mat_to_vector_Point2d(Mat m, List pts) { + Mat_to_vector_Point(m, pts); + } public static void Mat_to_vector_Point(Mat m, List pts) { if(pts == null) - throw new java.lang.IllegalArgumentException("pts == null"); + throw new java.lang.IllegalArgumentException("Output List can't be null"); int count = m.rows(); - if( CvType.CV_32SC2 != m.type() || m.cols()!=1 ) - throw new java.lang.IllegalArgumentException( - "CvType.CV_32SC2 != m.type() || m.cols()!=1\n" + m ); + int type = m.type(); + if(m.cols() != 1) + throw new java.lang.IllegalArgumentException( "Input Mat should have one column\n" + m ); pts.clear(); - int[] buff = new int[2*count]; - m.get(0, 0, buff); - for(int i=0; i pts) { + Mat_to_vector_Point3(m, pts); + } + public static void Mat_to_vector_Point3f(Mat m, List pts) { + Mat_to_vector_Point3(m, pts); + } + + public static void Mat_to_vector_Point3d(Mat m, List pts) { + Mat_to_vector_Point3(m, pts); + } + public static void Mat_to_vector_Point3(Mat m, List pts) { + if(pts == null) + throw new java.lang.IllegalArgumentException("Output List can't be null"); + int count = m.rows(); + int type = m.type(); + if(m.cols() != 1) + throw new java.lang.IllegalArgumentException( "Input Mat should have one column\n" + m ); + + pts.clear(); + if(type == CvType.CV_32SC3) { + int[] buff = new int[3*count]; + m.get(0, 0, buff); + for(int i=0; i mats) { -- 2.7.4