From 510ca53687f7b60cd8e26b0b87ad0438651651f3 Mon Sep 17 00:00:00 2001 From: Leonid Beynenson Date: Thu, 14 Jul 2011 08:36:48 +0000 Subject: [PATCH] Added the function minMaxLoc to JavaAPI as a manual ported function. Added test for the function. --- .../src/org/opencv/test/core/coreTest.java | 17 ++- modules/java/gen_java.py | 101 ++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/modules/java/android_test/src/org/opencv/test/core/coreTest.java b/modules/java/android_test/src/org/opencv/test/core/coreTest.java index 162d51c97a..ed073dc9a3 100644 --- a/modules/java/android_test/src/org/opencv/test/core/coreTest.java +++ b/modules/java/android_test/src/org/opencv/test/core/coreTest.java @@ -588,6 +588,21 @@ public class coreTest extends OpenCVTestCase { core.min(gray0, gray255, dst); assertMatEqual(gray0, dst); } + public void testMinMaxLoc() { + double minVal=1; + double maxVal=10; + Point minLoc = new Point((int)matSize/4, (int)matSize/2); + Point maxLoc = new Point((int)matSize/2, (int)matSize/4); + gray3.put((int)minLoc.y, (int)minLoc.x, minVal); + gray3.put((int)maxLoc.y, (int)maxLoc.x, maxVal); + + core.MinMaxLocResult mmres = core.minMaxLoc(gray3); + + assertTrue(mmres.minVal==minVal + && mmres.maxVal==maxVal + && mmres.minLoc.equals(minLoc) + && mmres.maxLoc.equals(maxLoc)); + } public void testMulSpectrumsMatMatMatInt() { //TODO: nice example @@ -917,4 +932,4 @@ public class coreTest extends OpenCVTestCase { fail("Not yet implemented"); } -} \ No newline at end of file +} diff --git a/modules/java/gen_java.py b/modules/java/gen_java.py index a46fe95143..847f2569a8 100644 --- a/modules/java/gen_java.py +++ b/modules/java/gen_java.py @@ -96,6 +96,8 @@ type_dict = { } +setManualFunctions=set(['minMaxLoc']) + class ConstInfo(object): def __init__(self, cname, name, val): ## self.name = re.sub(r"^cv\.", "", name).replace(".", "_") @@ -239,6 +241,9 @@ 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 + return None func_map = self.funcs classname = ffi.funcs[0].classname if classname: @@ -293,6 +298,43 @@ class JavaWrapperGenerator(object): CV_64F = 6, CV_USRTYPE1 = 7; + //Manual ported functions + + // 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; + public double maxVal; + public Point minLoc; + public Point maxLoc; + + public MinMaxLocResult() { + minVal=0; maxVal=0; + minLoc=new Point(); + maxLoc=new Point(); + } + } + public static MinMaxLocResult minMaxLoc(Mat src, Mat mask) { + MinMaxLocResult res = new MinMaxLocResult(); + long maskNativeObj=0; + if (mask != null) { + maskNativeObj=mask.nativeObj; + } + double resarr[] = n_minMaxLoc(src.nativeObj, maskNativeObj); + res.minVal=resarr[0]; + res.maxVal=resarr[1]; + res.minLoc.x=resarr[2]; + res.minLoc.y=resarr[3]; + res.maxLoc.x=resarr[4]; + res.maxLoc.y=resarr[5]; + return res; + } + public static MinMaxLocResult minMaxLoc(Mat src) { + return minMaxLoc(src, null); + } + private static native double[] n_minMaxLoc(long src_nativeObj, long mask_nativeObj); + + """ ) if module == "imgproc": @@ -375,6 +417,65 @@ class JavaWrapperGenerator(object): # step 4: generate code for the classes self.gen_classes() + if module == "core": + self.cpp_code.write(\ +""" +JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLoc + (JNIEnv* env, jclass cls, jlong src_nativeObj, jlong mask_nativeObj) +{ + try { +#ifdef DEBUG + LOGD("core::n_1minMaxLoc()"); +#endif // DEBUG + + jdoubleArray result; + result = env->NewDoubleArray(6); + 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) { + Mat& mask = *((Mat*)mask_nativeObj); + minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc, mask); + } else { + minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc); + } + + jdouble fill[6]; + fill[0]=minVal; + fill[1]=maxVal; + fill[2]=minLoc.x; + fill[3]=minLoc.y; + fill[4]=maxLoc.x; + fill[5]=maxLoc.y; + + env->SetDoubleArrayRegion(result, 0, 6, fill); + + return result; + + } catch(cv::Exception e) { +#ifdef DEBUG + LOGD("core::n_1minMaxLoc() catched cv::Exception: %s", e.what()); +#endif // DEBUG + jclass je = env->FindClass("org/opencv/CvException"); + if(!je) je = env->FindClass("java/lang/Exception"); + env->ThrowNew(je, e.what()); + return NULL; + } catch (...) { +#ifdef DEBUG + LOGD("core::n_1minMaxLoc() catched unknown exception (...)"); +#endif // DEBUG + jclass je = env->FindClass("java/lang/Exception"); + env->ThrowNew(je, "Unknown exception in JNI code {$module::$fname()}"); + return NULL; + } +} +""") + # module tail self.java_code.write("\n\n" + self.jn_code.getvalue() + "\n") self.java_code.write("}\n")