0

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.

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Gunaseelan Glad to help.
Can you look at this question stackoverflow.com/questions/46233947/… @Zindarod
can you look at this question stackoverflow.com/questions/46280425/… @Zindarod

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.