Java API: new types and converters are added, jni suffixes changed

This commit is contained in:
Andrey Pavlenko 2011-08-02 14:56:47 +00:00
parent 99b8e2db14
commit 2c5e79507d
4 changed files with 160 additions and 56 deletions

View File

@ -179,18 +179,20 @@ 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_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" },
"vector_uchar" : { "j_type" : "List<Byte>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<uchar> %(n)s", "suffix" : "J" },
"vector_int" : { "j_type" : "List<Integer>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<int> %(n)s", "suffix" : "J" },
"vector_float" : { "j_type" : "List<Float>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<float> %(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_DMatch" : { "j_type" : "List<DMatch>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<DMatch> %(n)s", "suffix" : "J" },
"vector_Rect" : { "j_type" : "List<Rect>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Rect> %(n)s", "suffix" : "J" },
"vector_uchar" : { "j_type" : "List<Byte>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<uchar> %(n)s", "suffix" : "J" },
"vector_char" : { "j_type" : "List<Byte>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<char> %(n)s", "suffix" : "J" },
"vector_int" : { "j_type" : "List<Integer>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<int> %(n)s", "suffix" : "J" },
"vector_float" : { "j_type" : "List<Float>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<float> %(n)s", "suffix" : "J" },
"vector_double" : { "j_type" : "List<Double>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<double> %(n)s", "suffix" : "J" },
"Mat" : { "j_type" : "Mat", "jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),),
@ -220,6 +222,11 @@ type_dict = {
"jni_var" : "KeyPoint %(n)s(%(n)s_x, %(n)s_y, %(n)s_size, %(n)s_angle, %(n)s_response, %(n)s_octave, %(n)s_class_id)",
"jni_type" : "jdoubleArray",
"suffix" : "FFFFFII"},
"DMatch" : { "j_type" : "DMatch", "jn_args" : ( ('int', 'queryIdx'), ('int', 'trainIdx'),
('int', 'imgIdx'), ('float', 'distance'), ),
"jni_var" : "DMatch %(n)s(%(n)s_queryIdx, %(n)s_trainIdx, %(n)s_imgIdx, %(n)s_distance)",
"jni_type" : "jdoubleArray",
"suffix" : "IIIF"},
"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"},
@ -493,6 +500,7 @@ class ClassInfo(object):
self.cname = self.name = self.jname = re.sub(r"^cv\.", "", name)
self.cname =self.cname.replace(".", "::")
self.methods = {}
self.methods_suffixes = {}
self.consts = [] # using a list to save the occurence order
self.private_consts = []
self.imports = set()
@ -536,7 +544,7 @@ class FuncInfo(object):
for m in decl[2]:
if m.startswith("="):
self.jname = m[1:]
self.jn_name = "n_" + self.jname
self.jn_name = self.jname #"n_" + self.jname
self.jni_name= re.sub(r"_", "_1", self.jn_name)
self.static = ["","static"][ "/S" in decl[2] ]
self.ctype = decl[1] or ""
@ -820,7 +828,7 @@ extern "C" {
def gen_func(self, fi, isoverload, prop_name=''):
def gen_func(self, fi, prop_name=''):
j_code = self.java_code[fi.classname or self.Module]["j_code"]
jn_code = self.java_code[fi.classname or self.Module]["jn_code"]
cpp_code = self.cpp_code
@ -868,16 +876,17 @@ extern "C" {
# java args
args = fi.args[:] # copy
if args and args[-1].defval:
isoverload = True
## if args and args[-1].defval:
## isoverload = True
suffix_counter = int( self.classes[fi.classname or self.Module].methods_suffixes.get(fi.jname, -1) )
while True:
suffix_counter = suffix_counter + 1
self.classes[fi.classname or self.Module].methods_suffixes[fi.jname] = suffix_counter
# java native method args
jn_args = []
# jni (cpp) function args
jni_args = [ArgInfo([ "env", "env", "", [], "" ]), ArgInfo([ "cls", "cls", "", [], "" ])]
suffix = "__"
## suffix = "__"
j_prologue = []
j_epilogue = []
c_prologue = []
@ -893,14 +902,14 @@ extern "C" {
# adding 'self'
jn_args.append ( ArgInfo([ "__int64", "nativeObj", "", [], "" ]) )
jni_args.append( ArgInfo([ "__int64", "self", "", [], "" ]) )
suffix += "J"
## suffix += "J"
self.get_imports(fi.classname, fi.ctype)
for a in args:
self.get_imports(fi.classname, a.ctype)
if a.pointer:
suffix += "_3D"
else:
suffix += type_dict[a.ctype].get("suffix") or ""
## if a.pointer:
## suffix += "_3D"
## else:
## suffix += type_dict[a.ctype].get("suffix") or ""
if "vector" in a.ctype: # pass as Mat
jn_args.append ( ArgInfo([ "__int64", "%s_mat.nativeObj" % a.name, "", [], "" ]) )
@ -945,7 +954,7 @@ extern "C" {
jn_code.write( Template(\
" private static native $jn_type $jn_name($jn_args);\n").substitute(\
jn_type = type_dict[fi.ctype].get("jn_type", "double[]"), \
jn_name = fi.jn_name, \
jn_name = fi.jn_name + '_' + `suffix_counter`, \
jn_args = ", ".join(["%s %s" % (type_dict[a.ctype]["jn_type"], a.name.replace(".","_").replace("[","").replace("]","")) for a in jn_args])
) );
@ -1012,7 +1021,7 @@ extern "C" {
j_type=type_dict[fi.ctype]["j_type"], \
j_name=fi.jname, \
j_args=", ".join(["%s %s" % (type_dict[a.ctype]["j_type"], a.name) for a in args]), \
jn_name=fi.jn_name, \
jn_name=fi.jn_name + '_' + `suffix_counter`, \
jn_args_call=", ".join( [a.name for a in jn_args] ),\
)
)
@ -1114,7 +1123,7 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_${clazz}_$fname
rtype = rtype, \
module = self.module, \
clazz = clazz, \
fname = fi.jni_name + ["",suffix][isoverload], \
fname = fi.jni_name + '_1' + `suffix_counter`, \
args = ", ".join(["%s %s" % (type_dict[a.ctype].get("jni_type"), a.name) for a in jni_args]), \
prologue = "\n ".join(c_prologue), \
epilogue = " ".join(c_epilogue), \
@ -1154,27 +1163,27 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_${clazz}_$fname
if ffi.isconstructor:
for fi in ffi.funcs:
fi.jname = ci.jname
fi.jn_name = "n_" + fi.jname
fi.jn_name = fi.jname #"n_" + fi.jname
fi.jni_name= re.sub("_", "_1", fi.jn_name)
self.gen_func(fi, len(ffi.funcs)>1)
self.gen_func(fi)
# other methods
for n, ffi in fflist:
if not ffi.isconstructor:
for fi in ffi.funcs:
self.gen_func(fi, len(ffi.funcs)>1)
self.gen_func(fi)
# props
for pi in ci.props:
# getter
getter_name = name + ".get_" + pi.name
#print getter_name
fi = FuncInfo( [getter_name, pi.ctype, [], []] ) # [ funcname, return_ctype, [modifiers], [args] ]
self.gen_func(fi, getter_name in ci.methods, pi.name)
self.gen_func(fi, pi.name)
if pi.rw:
#setter
setter_name = name + ".set_" + pi.name
#print setter_name
fi = FuncInfo( [ setter_name, "void", [], [ [pi.ctype, pi.name, "", [], ""] ] ] )
self.gen_func(fi, getter_name in ci.methods, pi.name)
self.gen_func(fi, pi.name)
# manual ports
if name in ManualFuncs:

View File

@ -67,6 +67,13 @@ void Mat_to_vector_uchar(Mat& mat, vector<uchar>& v_uchar)
v_uchar = (vector<uchar>) mat;
}
void Mat_to_vector_char(Mat& mat, vector<char>& v_char)
{
v_char.clear();
CHECK_MAT(mat.type()==CV_8SC1 && mat.cols==1);
v_char = (vector<char>) mat;
}
//vector_Rect
@ -220,3 +227,29 @@ void vector_Mat_to_Mat(std::vector<cv::Mat>& v_mat, cv::Mat& mat)
mat.at< Vec<int, 2> >(i, 0) = Vec<int, 2>(addr>>32, addr&0xffffffff);
}
}
//vector_DMatch
void Mat_to_vector_DMatch(Mat& mat, vector<DMatch>& v_dm)
{
v_dm.clear();
CHECK_MAT(mat.type()==CV_64FC4 && mat.cols==1);
for(int i=0; i<mat.rows; i++)
{
Vec<double, 4> v = mat.at< Vec<double, 4> >(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<DMatch>& v_dm, Mat& mat)
{
int count = v_dm.size();
mat.create(count, 1, CV_64FC4);
for(int i=0; i<count; i++)
{
DMatch dm = v_dm[i];
mat.at< Vec<double, 4> >(i, 0) = Vec<double, 4>(dm.queryIdx, dm.trainIdx, dm.imgIdx, dm.distance);
}
}

View File

@ -6,25 +6,18 @@
void Mat_to_vector_int(cv::Mat& mat, std::vector<int>& v_int);
void vector_int_to_Mat(std::vector<int>& v_int, cv::Mat& mat);
void Mat_to_vector_double(cv::Mat& mat, std::vector<double>& v_double);
void vector_double_to_Mat(std::vector<double>& v_double, cv::Mat& mat);
void Mat_to_vector_float(cv::Mat& mat, std::vector<float>& v_float);
void vector_float_to_Mat(std::vector<float>& v_float, cv::Mat& mat);
void Mat_to_vector_uchar(cv::Mat& mat, std::vector<uchar>& v_uchar);
void Mat_to_vector_char(cv::Mat& mat, std::vector<char>& v_char);
void Mat_to_vector_Rect(cv::Mat& mat, std::vector<cv::Rect>& v_rect);
void vector_Rect_to_Mat(std::vector<cv::Rect>& v_rect, cv::Mat& mat);
@ -42,12 +35,11 @@ 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);
void vector_KeyPoint_to_Mat(std::vector<cv::KeyPoint>& v_kp, cv::Mat& mat);
void Mat_to_vector_Mat(cv::Mat& mat, std::vector<cv::Mat>& v_mat);
void vector_Mat_to_Mat(std::vector<cv::Mat>& v_mat, cv::Mat& mat);
void Mat_to_vector_DMatch(cv::Mat& mat, std::vector<cv::DMatch>& v_dm);
void vector_DMatch_to_Mat(std::vector<cv::DMatch>& v_dm, cv::Mat& mat);

View File

@ -7,6 +7,7 @@ import org.opencv.core.CvType;
import org.opencv.core.Point;
import org.opencv.core.Point3;
import org.opencv.core.Rect;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.KeyPoint;
public class Converters {
@ -146,11 +147,11 @@ public class Converters {
}
public static void Mat_to_vector_Point2f(Mat m, List<Point> pts) {
Mat_to_vector_Point(m, 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);
Mat_to_vector_Point(m, pts);
}
public static void Mat_to_vector_Point(Mat m, List<Point> pts) {
if(pts == null)
@ -161,39 +162,39 @@ public class Converters {
throw new java.lang.IllegalArgumentException( "Input Mat should have one column\n" + m );
pts.clear();
if(type == CvType.CV_32SC2) {
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){
} 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){
} 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(
} 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);
Mat_to_vector_Point3(m, pts);
}
public static void Mat_to_vector_Point3f(Mat m, List<Point3> pts) {
Mat_to_vector_Point3(m, 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);
Mat_to_vector_Point3(m, pts);
}
public static void Mat_to_vector_Point3(Mat m, List<Point3> pts) {
if(pts == null)
@ -204,28 +205,28 @@ public class Converters {
throw new java.lang.IllegalArgumentException( "Input Mat should have one column\n" + m );
pts.clear();
if(type == CvType.CV_32SC3) {
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){
} 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){
} 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(
} 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) {
@ -313,6 +314,23 @@ public class Converters {
return res;
}
public static Mat vector_char_to_Mat(List<Byte> bs) {
Mat res;
int count = (bs!=null) ? bs.size() : 0;
if(count>0){
res = new Mat(count, 1, CvType.CV_8SC1);
byte[] buff = new byte[count];
for(int i=0; i<count; i++) {
byte b = bs.get(i);
buff[i] = b;
}
res.put(0, 0, buff);
} else {
res = new Mat();
}
return res;
}
public static Mat vector_int_to_Mat(List<Integer> is) {
Mat res;
int count = (is!=null) ? is.size() : 0;
@ -346,6 +364,22 @@ public class Converters {
}
}
public static void Mat_to_vector_char(Mat m, List<Byte> bs) {
if(bs == null)
throw new java.lang.IllegalArgumentException("Output List can't be null");
int count = m.rows();
if( CvType.CV_8SC1 != m.type() || m.cols()!=1 )
throw new java.lang.IllegalArgumentException(
"CvType.CV_8SC1 != m.type() || m.cols()!=1\n" + m);
bs.clear();
byte[] buff = new byte[count];
m.get(0, 0, buff);
for(int i=0; i<count; i++) {
bs.add( new Byte(buff[i]) );
}
}
public static Mat vector_Rect_to_Mat(List<Rect> rs) {
Mat res;
int count = (rs!=null) ? rs.size() : 0;
@ -408,7 +442,7 @@ public class Converters {
public static void Mat_to_vector_KeyPoint(Mat m, List<KeyPoint> kps) {
if(kps == null)
throw new java.lang.IllegalArgumentException("kps == null");
throw new java.lang.IllegalArgumentException("Output List can't be null");
int count = m.rows();
if( CvType.CV_64FC(7) != m.type() || m.cols()!=1 )
throw new java.lang.IllegalArgumentException(
@ -441,4 +475,40 @@ public class Converters {
return res;
}
public static Mat vector_DMatch_to_Mat(List<DMatch> matches) {
Mat res;
int count = (matches!=null) ? matches.size() : 0;
if(count>0){
res = new Mat(count, 1, CvType.CV_64FC4);
double[] buff = new double[count * 4];
for(int i=0; i<count; i++) {
DMatch m = matches.get(i);
buff[4*i ] = m.queryIdx;
buff[4*i+1] = m.trainIdx;
buff[4*i+2] = m.imgIdx;
buff[4*i+3] = m.distance;
}
res.put(0, 0, buff);
} else {
res = new Mat();
}
return res;
}
public static void Mat_to_vector_DMatch(Mat m, List<DMatch> matches) {
if(matches == null)
throw new java.lang.IllegalArgumentException("Output List can't be 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.cols()!=1\n" + m);
matches.clear();
double[] buff = new double[4*count];
m.get(0, 0, buff);
for(int i=0; i<count; i++) {
matches.add( new DMatch( (int)buff[4*i], (int)buff[4*i+1], (int)buff[4*i+2], (float)buff[4*i+3] ) );
}
}
}