Gunaseelan
Gunaseelan

Reputation: 15535

Passing InputArrayOfArrays from java class to JNI

I have generated following method in JNI cpp file,

JNIEXPORT void JNICALL Java_com_idesign_opencvmaketest_MainActivity_train
        (JNIEnv *env, jobject thisObj, jobjectArray images, jobjectArray labels) {
    Ptr<LBPHFaceRecognizer> model = LBPHFaceRecognizer::create();

    /** make a call to 
    *   CV_WRAP virtual void train(InputArrayOfArrays src, InputArray labels) = 0;
    **/
    model->train(images, labels);
}

Now I am getting

Parameter type mismatch: Types 'const _InputArray' and 'jobjectArray' are not compatible

at images and labels in model->train(images, labels);

So what would be the parameter type for images and labels in MainActivity_train method?

And also how to call this JNI method from Java class with correct parameter type?

I am new to OpenCv and JNI.

Upvotes: 0

Views: 224

Answers (1)

zindarod
zindarod

Reputation: 6468

The jobjectArray is not Mat. The org.opencv.core.Mat class has a getNativeObjAddr() method with return type long, which can be interpreted as a pointer to Mat. More on OpenCV Java API here, example code here.

The method

CV_WRAP virtual void train(InputArrayOfArrays src, InputArray labels)

takes a std::vector<cv::Mat> images as source and a std::vector<int> lables as lables. So as far as I know you need to pass more than one image to your JNI method. See sample here.

JNIEXPORT void JNICALL Java_com_idesign_opencvmaketest_MainActivity_train
        (JNIEnv *env, jobject thisObj, jlong images, jlong labels) {

Mat& matImage  = *(Mat*)images; //to create one Mat image, you need an array of Mat
Mat& matLabels = *(Mat*)labels; // create a Mat from labels

/*
To pass correct parameters, you would do:

std::vector<cv::Mat> vecImages;
vecImages.push_back(matImage);

std::vector<int> vecLabels;
//put your labels to vecLabels here

model->train(vecImages,vecLabels);
*/

Ptr<LBPHFaceRecognizer> model = LBPHFaceRecognizer::create();

/** make a call to 
*   CV_WRAP virtual void train(InputArrayOfArrays src, InputArray labels) = 0;
**/
model->train(matImages, matLabels); // function requires ArrayofMats and ArrayofInts
}

Upvotes: 1

Related Questions