提交 4deec6f0 编写于 作者: A Andrey Pavlenko

partial vector<> support in Java API generator

上级 cc447448
......@@ -106,7 +106,14 @@ type_dict = {
"size_t" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },
"__int64" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },
"double[]": { "j_type" : "double[]", "jn_type" : "double[]", "jni_type" : "jdoubleArray", "suffix" : "_3D" },
"vector_Point": { "j_type" : "java.util.ArrayList<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point> %(n)s", "suffix" : "J" },
"vector_Point": { "j_type" : "java.util.List<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point> %(n)s", "suffix" : "J" },
"vector_Mat" : { "j_type" : "java.util.List<Mat>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Mat> %(n)s", "suffix" : "J" },
"vector_KeyPoint" : { "j_type" : "java.util.List<KeyPoint>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<KeyPoint> %(n)s", "suffix" : "J" },
"vector_Rect" : { "j_type" : "java.util.List<Rect>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Rect> %(n)s", "suffix" : "J" },
"vector_uchar" : { "j_type" : "java.util.List<Byte>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<uchar> %(n)s", "suffix" : "J" },
"vector_int" : { "j_type" : "java.util.List<Integer>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<int> %(n)s", "suffix" : "J" },
"vector_float" : { "j_type" : "java.util.List<Float>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<float> %(n)s", "suffix" : "J" },
"vector_double" : { "j_type" : "java.util.List<Double>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<double> %(n)s", "suffix" : "J" },
# "complex" : { j_type : "?", jn_args : (("", ""),), jn_name : "", jni_var : "", jni_name : "", "suffix" : "?" },
"Mat" : { "j_type" : "Mat", "jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),),
"jni_var" : "Mat& %(n)s = *((Mat*)%(n)s_nativeObj)",
......@@ -320,7 +327,7 @@ class JavaWrapperGenerator(object):
def add_func(self, decl):
ffi = FuncFamilyInfo(decl)
if ffi.jname in setManualFunctions :
print "Found function, which is ported manually: " + ffi.jname
print "Found function, which is ported manually: " + ffi.jname
return None
func_map = self.funcs
classname = ffi.funcs[0].classname
......@@ -380,7 +387,7 @@ class JavaWrapperGenerator(object):
//Manual ported functions
// C++: minMaxLoc(Mat src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray())
// C++: minMaxLoc(Mat src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray())
//javadoc: minMaxLoc
public static class MinMaxLocResult {
public double minVal;
......@@ -513,9 +520,9 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
if (result == NULL) {
return NULL; /* out of memory error thrown */
}
Mat& src = *((Mat*)src_nativeObj);
double minVal, maxVal;
Point minLoc, maxLoc;
if (mask_nativeObj != 0) {
......@@ -524,7 +531,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
} else {
minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc);
}
jdouble fill[6];
fill[0]=minVal;
fill[1]=maxVal;
......@@ -532,7 +539,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
fill[3]=minLoc.y;
fill[4]=maxLoc.x;
fill[5]=maxLoc.y;
env->SetDoubleArrayRegion(result, 0, 6, fill);
return result;
......@@ -657,16 +664,16 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
jni_args.append ( ArgInfo([ "__int64", "%s_mat_nativeObj" % a.name, "", [], "" ]) )
c_prologue.append( type_dict[a.ctype]["jni_var"] % {"n" : a.name} + ";" )
if "I" in a.out or not a.out:
j_prologue.append( "Mat %s_mat = null;/*%s_to_Mat(%s);*/" % (a.name, a.ctype, a.name) )
j_prologue.append( "Mat %s_mat = utils.%s_to_Mat(%s);" % (a.name, a.ctype, a.name) )
c_prologue.append( "// %s_out -> %s" % (a.name, a.name) )
else:
j_prologue.append( "Mat %s_mat = new Mat();" % a.name )
if "O" in a.out:
j_epilogue.append("/*Mat_to_%s(%s_mat, %s);*/" % (a.ctype, a.name, a.name))
j_epilogue.append("utils.Mat_to_%s(%s_mat, %s);" % (a.ctype, a.name, a.name))
c_epilogue.append( "// %s -> %s_out" % (a.name, a.name) )
else:
fields = type_dict[a.ctype].get("jn_args") or []
fields = type_dict[a.ctype].get("jn_args")
if fields: # complex type
for f in fields:
jn_args.append ( ArgInfo([ f[0], a.name + f[1], "", [], "" ]) )
......@@ -682,8 +689,17 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
jni_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )
#jni_args.append ( ArgInfo([ "int", "%s_out_length" % a.name, "", [], "" ]) )
j_prologue.append( "double[] %s_out = new double[%i];" % (a.name, len(type_dict[a.ctype].get("jn_args", [1]))) )
j_epilogue.append("/*%s.set(%s_out);*/" % (a.name, a.name))
c_epilogue.append( "/* %s_to_doubles(%s, %s_out); */" % (a.ctype, a.name, a.name) )
if fields:
j_epilogue.append("%s.set(%s_out);" % (a.name, a.name))
c_epilogue.append( \
"jdouble tmp_%(n)s[%(cnt)i] = {%(args)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, %(cnt)i, tmp_%(n)s);" %
{ "n" : a.name, "cnt" : len(fields), "args" : ", ".join([a.name + f[1] for f in fields]) } )
else:
j_epilogue.append("/* NYI: %s.set(%s_out); */" % (a.name, a.name))
c_epilogue.append( \
"jdouble tmp_%(n)s[1] = {%(n)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, 1, tmp_%(n)s);" %
{ "n" : a.name } )
# java part:
# private java NATIVE method decl
......
#include "utils.h"
using namespace cv;
\ No newline at end of file
#include <jni.h>
#include "opencv2/core/core.hpp"
package org.opencv;
import java.util.List;
public class utils {
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(1, count, CvType.CV_64FC2); //Point can be saved into double[2]
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);
} else {
res = new Mat();
}
return res;
}
public static void Mat_to_vector_Point(Mat m, List<Point> pts) {
if(pts == null)
return;
int cols = m.cols();
if(!CvType.CV_64FC2.equals(m.type()) || m.rows()!=1 || cols%2!=0)
return;
pts.clear();
double[] buff = new double[cols];
m.get(0, 0, buff);
for(int i=0; i<cols/2; i++) {
pts.add( new Point(buff[i*2], buff[i*2+1]) );
}
}
public static Mat vector_Mat_to_Mat(List<Mat> mats) {
Mat res;
int count = (mats!=null) ? mats.size() : 0;
if(count>0){
res = new Mat(1, count, CvType.CV_32SC2);
int[] buff = new int[count*2];
for(int i=0; i<count; i++) {
long addr = mats.get(i).nativeObj;
buff[i*2] = (int)(addr >> 32);
buff[i*2+1] = (int)(addr & 0xffffffff);
}
res.put(0, 0, buff);
} else {
res = new Mat();
}
return res;
}
public static void Mat_to_vector_Mat(Mat m, List<Mat> mats) {
if(mats == null)
return;
int cols = m.cols();
if(!CvType.CV_32SC2.equals(m.type()) || m.rows()!=1 || cols%2!=0)
return;
mats.clear();
int[] buff = new int[cols];
m.get(0, 0, buff);
for(int i=0; i<cols/2; i++) {
long addr = (((long)buff[i*2])<<32) | ((long)buff[i*2+1]);
mats.add( new Mat(addr) );
}
}
public static void Mat_to_vector_KeyPoint(Mat kp_mat, List<features2d.KeyPoint> kps) {
// TODO Auto-generated method stub
}
public static Mat vector_float_to_Mat(List<Float> fs) {
// TODO Auto-generated method stub
return null;
}
public static void Mat_to_vector_float(Mat m, List<Float> fs) {
// TODO Auto-generated method stub
}
public static Mat vector_uchar_to_Mat(List<Byte> bs) {
// TODO Auto-generated method stub
return null;
}
public static Mat vector_int_to_Mat(List<Integer> is) {
// TODO Auto-generated method stub
return null;
}
public static void Mat_to_vector_int(Mat m, List<Integer> is) {
// TODO Auto-generated method stub
}
public static Mat vector_Rect_to_Mat(List<Rect> rs) {
// TODO Auto-generated method stub
return null;
}
public static void Mat_to_vector_Rect(Mat m, List<Rect> rs) {
// TODO Auto-generated method stub
}
public static Mat vector_double_to_Mat(List<Double> ds) {
// TODO Auto-generated method stub
return null;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册