我正在使用opencv-sdk-android。 我希望我的本机代码应该返回关键点向量。使用这样的代码是否正确..
Vector<KeyPoint> keypoint = FindFeatures(Gray1.getNativeObjAddr(),descriptor.getNativeObjAddr());
和
public native Vector<KeyPoint> FindFeatures(long matAddrGr1, long matAddrGr2);
我的natice代码是
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的新手。
答案 0 :(得分:4)
我遇到了同样的问题,我用这段代码解决了这个问题。
首先在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;
}
我希望对你有帮助......