Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK passing parameters to native methods

I am studying OpenCV4Android SDK, in the 2.4.5 version, with the NDK framework, using which I can use native code (written in C/C++) in the Android environment. But I don't exactly understand how parameters are passed from Android to C.

For example, in the 'mixedprocessing' sample, in the directory 'jni' there's a .cpp file named 'jni_part', whose code is:

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <vector>

using namespace std;
using namespace cv;

extern "C" {

  JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba);

  JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba) {
    Mat& mGr  = *(Mat*)addrGray;
    Mat& mRgb = *(Mat*)addrRgba;
    vector<KeyPoint> v;

    FastFeatureDetector detector(50);
    detector.detect(mGr, v);
    for( unsigned int i = 0; i < v.size(); i++ ) {
      const KeyPoint& kp = v[i];
      circle(mRgb, Point(kp.pt.x, kp.pt.y), 10, Scalar(255,0,0,255));
    }
  }

}

In the MainActivity there's the method:

 public native void FindFeatures(long matAddrGr, long matAddrRgba);

So it's passed as parameter the native address of a Mat object, but how does it become a matrix in C? And which features are detected from a FastFeatureDetector object?

like image 407
user140888 Avatar asked Aug 02 '13 09:08

user140888


1 Answers

FindFeatures, in Java, calls its exact equivalent in C/C++:

JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba){
Mat& mGr  = *(Mat*)addrGray;
Mat& mRgb = *(Mat*)addrRgba;
...

This is where it becomes a cv::Mat. (Mat*) casts what is pointed by address addrGray (respectively addrRgba) to a "pointer to a cv::Mat". Then, the value pointed by this newly created pointer is put in mGr (respectively mRgb), which is a cv::Mat.

In other words, you only give to C/C++ an address in the memory, and you have to make sure that what's there actually is a valid cv::Mat.


About your second question, the FAST detector detects points of interest in the image (i.e. points that contain a lot of information). The idea is to be able to identify those points on multiple different images. To simplify, you can consider a feature detected by FAST as a corner in the image.

like image 139
JonasVautherin Avatar answered Sep 20 '22 23:09

JonasVautherin