0

I am using opencv-sdk-android. I want that my native code should return keypoint vector. Is it correct to use code like this..

Vector<KeyPoint> keypoint = FindFeatures(Gray1.getNativeObjAddr(),descriptor.getNativeObjAddr());

and

public native Vector<KeyPoint> FindFeatures(long matAddrGr1, long matAddrGr2);

My natice code is

extern "C" {
JNIEXPORT Vector<KeyPoint> JNICALL Java_com_example_xyz_MainActivity_FindFeatures(JNIEnv*, jobject, jlong addrGray1, jlong addrdescrptor);

JNIEXPORT Vector<KeyPoint> JNICALL Java_com_example_xyz_MainActivity_FindFeatures(JNIEnv*, jobject, jlong addrGray1, jlong addrdescrptor)
{
    Mat& mGr1  = *(Mat*)addrGray1;
    Mat& descriptors_1 = *(Mat*)addrdescrptor;
    vector<KeyPoint> keypoint_1;

    //Do some processing here..

   return keypoint_1;
}
}

If not please suggest me some altenative way to achieve it. am new in opencv.

g_p
  • 5,499
  • 3
  • 19
  • 22
  • yes, this should work fine. did you get any errors when you try to implement it ?. – Darshan Dec 05 '13 at 17:16
  • Yes, After calling the native method, application is getting crash. – g_p Dec 05 '13 at 17:41
  • I'm sorry, i did not thoroughly check the code. This would not work because Vector is not a standard JNI [type](http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp9502) . you will have to make use of pointers(*env) to get the data to java side – Darshan Dec 05 '13 at 18:24
  • Can you give me little more detail about how to use (*env) to get this keypoint data. – g_p Dec 05 '13 at 19:15

1 Answers1

4

I had the same problem and I solved it with this piece of code.

First of all in java code I've declared the function FindFeatures like this:

public native KeyPoint[] FindFeatures(long matAddrGr1, long matAddrGr2);

And my native code is:

JNIEXPORT jobjectArray JNICALL   Java_com_example_mipatternrecognition_Reconocimiento_FindFeatures(
    JNIEnv* env, jobject, jlong matAddrGr1, jlong matAddrGr2) {
    Mat& mGr = *(Mat*) matAddrGr1;
    Mat& mRgb = *(Mat*) matAddrGr2;
    vector < KeyPoint > keyPoints_1;

    //Do some processing...

    // Get a class reference for org/opencv/features2d/KeyPoint
    jclass cls = env->FindClass("org/opencv/features2d/KeyPoint");
    // Get the Method ID of the constructor (Float,Float,Float,Float,Float,Integer,Integer)
    jmethodID midInit = env->GetMethodID(cls, "<init>", "(FFFFFII)V");
    // Call back constructor to allocate a new instance
    jobjectArray newKeyPointArr = env->NewObjectArray(keyPoints_1.size(), cls, NULL);

    for (unsigned int i = 0; i < keyPoints_1.size(); i++) {
        jobject newKeyPoint = env->NewObject(cls, midInit, keyPoints_1[i].pt.x,
            keyPoints_1[i].pt.y, keyPoints_1[i].size, keyPoints_1[i].angle,
            keyPoints_1[i].response, keyPoints_1[i].octave,
            keyPoints_1[i].class_id);
        env->SetObjectArrayElement(newKeyPointArr, i, newKeyPoint);
    }

    return newKeyPointArr;
}

I hope it helps to you...

migue02
  • 130
  • 12