从本地代码中获取关键点回来[英] Getting keypoint back from native code

本文是小编为大家收集整理的关于从本地代码中获取关键点回来的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用opencv-sdk-android. 我希望我的本机代码应返回按键点向量.使用这样的代码是正确的吗?

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

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

我的本​​质代码是

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;
}
}

如果没有,请建议我一些实现目标的方法.是opencv的新手.

推荐答案

我遇到了相同的问题,我用这件代码解决了它.

首先,在Java代码中,我声明了这样的函数FindFeatures:

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

我的本​​机代码是:

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;
}

我希望它对您有帮助...

本文地址:https://www.itbaoku.cn/post/102777.html

问题描述

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.

推荐答案

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