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.

4
  • yes, this should work fine. did you get any errors when you try to implement it ?. Commented Dec 5, 2013 at 17:16
  • Yes, After calling the native method, application is getting crash. Commented Dec 5, 2013 at 17:41
  • I'm sorry, i did not thoroughly check the code. This would not work because Vector<KeyPoint> is not a standard JNI type . you will have to make use of pointers(*env) to get the data to java side Commented Dec 5, 2013 at 18:24
  • Can you give me little more detail about how to use (*env) to get this keypoint data. Commented Dec 5, 2013 at 19:15

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.