Commit f4e28f87 authored by Andrey Pavlenko's avatar Andrey Pavlenko

Java API: new converters,…

Java API: new converters, findFundamentalMat/cornerSubPix/findHomography/solvePnP/solvePnPRansac and their tests are updated
parent ce2f4c6a
......@@ -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);
......
......@@ -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);
}
......
......@@ -179,6 +179,11 @@ type_dict = {
# "complex" : { j_type : "?", jn_args : (("", ""),), jn_name : "", jni_var : "", jni_name : "", "suffix" : "?" },
"vector_Point": { "j_type" : "List<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point> %(n)s", "suffix" : "J" },
"vector_Point2f": { "j_type" : "List<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point2f> %(n)s", "suffix" : "J" },
"vector_Point2d": { "j_type" : "List<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point2d> %(n)s", "suffix" : "J" },
"vector_Point3i": { "j_type" : "List<Point3>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point3i> %(n)s", "suffix" : "J" },
"vector_Point3f": { "j_type" : "List<Point3>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point3f> %(n)s", "suffix" : "J" },
"vector_Point3d": { "j_type" : "List<Point3>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point3d> %(n)s", "suffix" : "J" },
"vector_Mat" : { "j_type" : "List<Mat>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Mat> %(n)s", "suffix" : "J" },
"vector_KeyPoint" : { "j_type" : "List<KeyPoint>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<KeyPoint> %(n)s", "suffix" : "J" },
"vector_Rect" : { "j_type" : "List<Rect>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Rect> %(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
......
......@@ -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<int>& 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<int>) mat;
}
......@@ -33,7 +33,7 @@ void vector_int_to_Mat(vector<int>& v_int, Mat& mat)
void Mat_to_vector_double(Mat& mat, vector<double>& 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<double>) mat;
}
......@@ -48,7 +48,7 @@ void vector_double_to_Mat(vector<double>& v_double, Mat& mat)
void Mat_to_vector_float(Mat& mat, vector<float>& 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<float>) mat;
}
......@@ -63,7 +63,7 @@ void vector_float_to_Mat(vector<float>& v_float, Mat& mat)
void Mat_to_vector_uchar(Mat& mat, vector<uchar>& 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<uchar>) mat;
}
......@@ -73,7 +73,7 @@ void Mat_to_vector_uchar(Mat& mat, vector<uchar>& v_uchar)
void Mat_to_vector_Rect(Mat& mat, vector<Rect>& 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<Rect>) mat;
}
......@@ -87,22 +87,88 @@ void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)
void Mat_to_vector_Point(Mat& mat, vector<Point>& 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<Point>) mat;
}
//vector_Point2f
void Mat_to_vector_Point2f(Mat& mat, vector<Point2f>& v_point)
{
v_point.clear();
CHECK_MAT(mat.type()==CV_32FC2 && mat.cols==1);
v_point = (vector<Point2f>) mat;
}
//vector_Point2d
void Mat_to_vector_Point2d(Mat& mat, vector<Point2d>& v_point)
{
v_point.clear();
CHECK_MAT(mat.type()==CV_64FC2 && mat.cols==1);
v_point = (vector<Point2d>) mat;
}
//vector_Point3i
void Mat_to_vector_Point3i(Mat& mat, vector<Point3i>& v_point)
{
v_point.clear();
CHECK_MAT(mat.type()==CV_32SC3 && mat.cols==1);
v_point = (vector<Point3i>) mat;
}
//vector_Point3f
void Mat_to_vector_Point3f(Mat& mat, vector<Point3f>& v_point)
{
v_point.clear();
CHECK_MAT(mat.type()==CV_32FC3 && mat.cols==1);
v_point = (vector<Point3f>) mat;
}
//vector_Point3d
void Mat_to_vector_Point3d(Mat& mat, vector<Point3d>& v_point)
{
v_point.clear();
CHECK_MAT(mat.type()==CV_64FC3 && mat.cols==1);
v_point = (vector<Point3d>) mat;
}
void vector_Point_to_Mat(vector<Point>& v_point, Mat& mat)
{
mat = Mat(v_point);
}
void vector_Point2f_to_Mat(vector<Point2f>& v_point, Mat& mat)
{
mat = Mat(v_point);
}
void vector_Point2d_to_Mat(vector<Point2d>& v_point, Mat& mat)
{
mat = Mat(v_point);
}
void vector_Point3i_to_Mat(vector<Point3i>& v_point, Mat& mat)
{
mat = Mat(v_point);
}
void vector_Point3f_to_Mat(vector<Point3f>& v_point, Mat& mat)
{
mat = Mat(v_point);
}
void vector_Point3d_to_Mat(vector<Point3d>& v_point, Mat& mat)
{
mat = Mat(v_point);
}
//vector_KeyPoint
void Mat_to_vector_KeyPoint(Mat& mat, vector<KeyPoint>& 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<mat.rows; i++)
{
Vec<double, 7> v = mat.at< Vec<double, 7> >(i, 0);
......@@ -138,6 +204,8 @@ void Mat_to_vector_Mat(cv::Mat& mat, std::vector<cv::Mat>& 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");
}
}
......
......@@ -29,8 +29,18 @@ void vector_Rect_to_Mat(std::vector<cv::Rect>& v_rect, cv::Mat& mat);
void Mat_to_vector_Point(cv::Mat& mat, std::vector<cv::Point>& v_point);
void Mat_to_vector_Point2f(cv::Mat& mat, std::vector<cv::Point2f>& v_point);
void Mat_to_vector_Point2d(cv::Mat& mat, std::vector<cv::Point2d>& v_point);
void Mat_to_vector_Point3i(cv::Mat& mat, std::vector<cv::Point3i>& v_point);
void Mat_to_vector_Point3f(cv::Mat& mat, std::vector<cv::Point3f>& v_point);
void Mat_to_vector_Point3d(cv::Mat& mat, std::vector<cv::Point3d>& v_point);
void vector_Point_to_Mat(std::vector<cv::Point>& v_point, cv::Mat& mat);
void vector_Point2f_to_Mat(std::vector<cv::Point2f>& v_point, cv::Mat& mat);
void vector_Point2d_to_Mat(std::vector<cv::Point2d>& v_point, cv::Mat& mat);
void vector_Point3i_to_Mat(std::vector<cv::Point3i>& v_point, cv::Mat& mat);
void vector_Point3f_to_Mat(std::vector<cv::Point3f>& v_point, cv::Mat& mat);
void vector_Point3d_to_Mat(std::vector<cv::Point3d>& v_point, cv::Mat& mat);
void Mat_to_vector_KeyPoint(cv::Mat& mat, std::vector<cv::KeyPoint>& v_kp);
......
......@@ -12,74 +12,220 @@ import org.opencv.features2d.KeyPoint;
public class Converters {
public static Mat vector_Point_to_Mat(List<Point> 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<count; i++) {
Point p = pts.get(i);
buff[i*2] = (int)p.x;
buff[i*2+1] = (int)p.y;
}
res.put(0, 0, buff);
} else {
res = new Mat();
}
return res;
return vector_Point_to_Mat(pts, CvType.CV_32S);
}
public static Mat vector_Point2f_to_Mat(List<Point> pts) {
return vector_Point_to_Mat(pts, CvType.CV_32F);
}
public static Mat vector_Point2d_to_Mat(List<Point> pts) {
return vector_Point_to_Mat(pts, CvType.CV_64F);
}
public static Mat vector_Point_to_Mat(List<Point> 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<count; i++) {
Point p = pts.get(i);
buff[i*2] = (float)p.x;
buff[i*2+1] = (float)p.y;
switch (typeDepth) {
case CvType.CV_32S:
{
res = new Mat(count, 1, CvType.CV_32SC2);
int[] buff = new int[count*2];
for(int i=0; i<count; i++) {
Point p = pts.get(i);
buff[i*2] = (int)p.x;
buff[i*2+1] = (int)p.y;
}
res.put(0, 0, buff);
}
break;
case CvType.CV_32F:
{
res = new Mat(count, 1, CvType.CV_32FC2);
float[] buff = new float[count*2];
for(int i=0; i<count; i++) {
Point p = pts.get(i);
buff[i*2] = (float)p.x;
buff[i*2+1] = (float)p.y;
}
res.put(0, 0, buff);
}
break;
case CvType.CV_64F:
{
res = new Mat(count, 1, CvType.CV_64FC2);
double[] buff = new double[count*2];
for(int i=0; i<count; i++) {
Point p = pts.get(i);
buff[i*2] = p.x;
buff[i*2+1] = p.y;
}
res.put(0, 0, buff);
}
break;
default:
throw new IllegalArgumentException("'typeDepth' can be CV_32S, CV_32F or CV_64F");
}
res.put(0, 0, buff);
} else {
res = new Mat();
}
return res;
}
public static Mat vector_Point3i_to_Mat(List<Point3> pts) {
return vector_Point3_to_Mat(pts, CvType.CV_32S);
}
public static Mat vector_Point3f_to_Mat(List<Point3> pts) {
return vector_Point3_to_Mat(pts, CvType.CV_32F);
}
public static Mat vector_Point3d_to_Mat(List<Point3> pts) {
return vector_Point3_to_Mat(pts, CvType.CV_64F);
}
public static Mat vector_Point3_to_Mat(List<Point3> 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<count; i++) {
Point3 p = pts.get(i);
buff[i*3] = (float)p.x;
buff[i*3+1] = (float)p.y;
buff[i*3+2] = (float)p.z;
switch (typeDepth){
case CvType.CV_32S:
{
res = new Mat(count, 1, CvType.CV_32SC3);
int[] buff = new int[count*3];
for(int i=0; i<count; i++) {
Point3 p = pts.get(i);
buff[i*3] = (int)p.x;
buff[i*3+1] = (int)p.y;
buff[i*3+2] = (int)p.z;
}
res.put(0, 0, buff);
}
break;
case CvType.CV_32F:
{
res = new Mat(count, 1, CvType.CV_64FC3);
float[] buff = new float[count*3];
for(int i=0; i<count; i++) {
Point3 p = pts.get(i);
buff[i*3] = (float)p.x;
buff[i*3+1] = (float)p.y;
buff[i*3+2] = (float)p.z;
}
res.put(0, 0, buff);
}
break;
case CvType.CV_64F:
{
res = new Mat(count, 1, CvType.CV_64FC3);
double[] buff = new double[count*3];
for(int i=0; i<count; i++) {
Point3 p = pts.get(i);
buff[i*3] = p.x;
buff[i*3+1] = p.y;
buff[i*3+2] = p.z;
}
res.put(0, 0, buff);
}
break;
default:
throw new IllegalArgumentException("'typeDepth' can be CV_32S, CV_32F or CV_64F");
}
res.put(0, 0, buff);
} else {
res = new Mat();
}
return res;
}
public static void Mat_to_vector_Point2f(Mat m, List<Point> pts) {
Mat_to_vector_Point(m, pts);
}
public static void Mat_to_vector_Point2d(Mat m, List<Point> pts) {
Mat_to_vector_Point(m, pts);
}
public static void Mat_to_vector_Point(Mat m, List<Point> 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<count; i++) {
pts.add( new Point(buff[i*2], buff[i*2+1]) );
}
if(type == CvType.CV_32SC2) {
int[] buff = new int[2*count];
m.get(0, 0, buff);
for(int i=0; i<count; i++) {
pts.add( new Point(buff[i*2], buff[i*2+1]) );
}
} else if(type == CvType.CV_32FC2){
float[] buff = new float[2*count];
m.get(0, 0, buff);
for(int i=0; i<count; i++) {
pts.add( new Point(buff[i*2], buff[i*2+1]) );
}
} else if(type == CvType.CV_64FC2){
double[] buff = new double[2*count];
m.get(0, 0, buff);
for(int i=0; i<count; i++) {
pts.add( new Point(buff[i*2], buff[i*2+1]) );
}
} else {
throw new java.lang.IllegalArgumentException(
"Input Mat should be of CV_32SC2, CV_32FC2 or CV_64FC2 type\n" + m );
}
}
public static void Mat_to_vector_Point3i(Mat m, List<Point3> pts) {
Mat_to_vector_Point3(m, pts);
}
public static void Mat_to_vector_Point3f(Mat m, List<Point3> pts) {
Mat_to_vector_Point3(m, pts);
}
public static void Mat_to_vector_Point3d(Mat m, List<Point3> pts) {
Mat_to_vector_Point3(m, pts);
}
public static void Mat_to_vector_Point3(Mat m, List<Point3> 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<count; i++) {
pts.add( new Point3(buff[i*3], buff[i*3+1], buff[i*3+2]) );
}
} else if(type == CvType.CV_32FC3){
float[] buff = new float[3*count];
m.get(0, 0, buff);
for(int i=0; i<count; i++) {
pts.add( new Point3(buff[i*3], buff[i*3+1], buff[i*3+2]) );
}
} else if(type == CvType.CV_64FC3){
double[] buff = new double[3*count];
m.get(0, 0, buff);
for(int i=0; i<count; i++) {
pts.add( new Point3(buff[i*3], buff[i*3+1], buff[i*3+2]) );
}
} else {
throw new java.lang.IllegalArgumentException(
"Input Mat should be of CV_32SC3, CV_32FC3 or CV_64FC3 type\n" + m );
}
}
public static Mat vector_Mat_to_Mat(List<Mat> mats) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment