Added the function minMaxLoc to JavaAPI as a manual ported function.

Added test for the function.
This commit is contained in:
Leonid Beynenson 2011-07-14 08:36:48 +00:00
parent ab1ff12a71
commit 510ca53687
2 changed files with 117 additions and 1 deletions

View 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");
}
}
}

View File

@ -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")