Added the function minMaxLoc to JavaAPI as a manual ported function.
authorLeonid Beynenson <no@email>
Thu, 14 Jul 2011 08:36:48 +0000 (08:36 +0000)
committerLeonid Beynenson <no@email>
Thu, 14 Jul 2011 08:36:48 +0000 (08:36 +0000)
Added test for the function.

modules/java/android_test/src/org/opencv/test/core/coreTest.java
modules/java/gen_java.py

index 162d51c..ed073dc 100644 (file)
@@ -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
+}
index a46fe95..847f256 100644 (file)
@@ -96,6 +96,8 @@ type_dict = {
 \r
 }\r
 \r
+setManualFunctions=set(['minMaxLoc'])\r
+\r
 class ConstInfo(object):\r
     def __init__(self, cname, name, val):\r
 ##        self.name = re.sub(r"^cv\.", "", name).replace(".", "_")\r
@@ -239,6 +241,9 @@ class JavaWrapperGenerator(object):
 \r
     def add_func(self, decl):\r
         ffi = FuncFamilyInfo(decl)\r
+       if ffi.jname in setManualFunctions :\r
+               print "Found function, which is ported manually: " + ffi.jname \r
+               return None\r
         func_map = self.funcs\r
         classname = ffi.funcs[0].classname\r
         if classname:\r
@@ -293,6 +298,43 @@ class JavaWrapperGenerator(object):
             CV_64F = 6,\r
             CV_USRTYPE1 = 7;\r
 \r
+    //Manual ported functions\r
+\r
+    // C++: minMaxLoc(Mat src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray()) \r
+    //javadoc: minMaxLoc\r
+    public static class MinMaxLocResult {\r
+        public double minVal;\r
+        public double maxVal;\r
+        public Point minLoc;\r
+        public Point maxLoc;\r
+\r
+       public MinMaxLocResult() {\r
+           minVal=0; maxVal=0;\r
+           minLoc=new Point();\r
+           maxLoc=new Point();\r
+       }\r
+    }\r
+    public static MinMaxLocResult minMaxLoc(Mat src, Mat mask) {\r
+        MinMaxLocResult res = new MinMaxLocResult();\r
+        long maskNativeObj=0;\r
+        if (mask != null) {\r
+                maskNativeObj=mask.nativeObj;\r
+        }\r
+        double resarr[] = n_minMaxLoc(src.nativeObj, maskNativeObj);\r
+        res.minVal=resarr[0];\r
+        res.maxVal=resarr[1];\r
+        res.minLoc.x=resarr[2];\r
+        res.minLoc.y=resarr[3];\r
+        res.maxLoc.x=resarr[4];\r
+        res.maxLoc.y=resarr[5];\r
+        return res;\r
+    }\r
+    public static MinMaxLocResult minMaxLoc(Mat src) {\r
+        return minMaxLoc(src, null);\r
+    }\r
+    private static native double[] n_minMaxLoc(long src_nativeObj, long mask_nativeObj);\r
+\r
+\r
 """ )\r
 \r
         if module == "imgproc":\r
@@ -375,6 +417,65 @@ class JavaWrapperGenerator(object):
         # step 4: generate code for the classes\r
         self.gen_classes()\r
 \r
+        if module == "core":\r
+            self.cpp_code.write(\\r
+"""\r
+JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLoc\r
+  (JNIEnv* env, jclass cls, jlong src_nativeObj, jlong mask_nativeObj)\r
+{\r
+    try {\r
+#ifdef DEBUG\r
+        LOGD("core::n_1minMaxLoc()");\r
+#endif // DEBUG\r
+\r
+        jdoubleArray result;\r
+        result = env->NewDoubleArray(6);\r
+        if (result == NULL) {\r
+            return NULL; /* out of memory error thrown */\r
+        }\r
+        \r
+        Mat& src = *((Mat*)src_nativeObj);\r
+    \r
+        double minVal, maxVal;\r
+        Point minLoc, maxLoc;\r
+        if (mask_nativeObj != 0) {\r
+            Mat& mask = *((Mat*)mask_nativeObj);\r
+            minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc, mask);\r
+        } else {\r
+            minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc);\r
+        }\r
+            \r
+        jdouble fill[6];\r
+        fill[0]=minVal;\r
+        fill[1]=maxVal;\r
+        fill[2]=minLoc.x;\r
+        fill[3]=minLoc.y;\r
+        fill[4]=maxLoc.x;\r
+        fill[5]=maxLoc.y;\r
+    \r
+        env->SetDoubleArrayRegion(result, 0, 6, fill);\r
+\r
+       return result;\r
+\r
+    } catch(cv::Exception e) {\r
+#ifdef DEBUG\r
+        LOGD("core::n_1minMaxLoc() catched cv::Exception: %s", e.what());\r
+#endif // DEBUG\r
+        jclass je = env->FindClass("org/opencv/CvException");\r
+        if(!je) je = env->FindClass("java/lang/Exception");\r
+        env->ThrowNew(je, e.what());\r
+        return NULL;\r
+    } catch (...) {\r
+#ifdef DEBUG\r
+        LOGD("core::n_1minMaxLoc() catched unknown exception (...)");\r
+#endif // DEBUG\r
+        jclass je = env->FindClass("java/lang/Exception");\r
+        env->ThrowNew(je, "Unknown exception in JNI code {$module::$fname()}");\r
+        return NULL;\r
+    }\r
+}\r
+""")\r
+\r
         # module tail\r
         self.java_code.write("\n\n" + self.jn_code.getvalue() + "\n")\r
         self.java_code.write("}\n")\r