Ask Your Question
1

How to pass a ArrayList<MatOfPoint> for a jni C ++ function?

asked 2015-08-13 16:40:02 -0600

jefferson-sep gravatar image

updated 2017-08-22 08:30:37 -0600

I am doing a project in Java with Android jni C ++. I have a function in C ++ with the following parameters:

C ++ function:

void rectify (vector <Point2f> & corners, Mat & img) {...}

In JAVA, call would be:

Mat image = Highgui.imread("img.png");
List <MatOfPoint> cornners =  new ArrayList<MatOfPoint>();;
Point b = new Point (real_x2, real_y2);
MatOfPoint ma = new MatOfPoint (b);
cornners.add(ma);
rectfy(image.getNativeObjAddr(), cornners)

public native "??" rectfy(long mat, "??" matofpoint);

With that, I wonder how will the function C ++ jni:

JNIEXPORT ?? JNICALL Java_ImageProcessingActivity_rectfy (JNIEnv * jobject, ?? cornners, inputMatAddress jlong)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-08-13 19:45:57 -0600

Yolteotl gravatar image

Hi,

You should take a look to Calib3d JNI interface. For example the findFundamentalMat() function :

public static Mat findFundamentalMat(MatOfPoint2f points1, MatOfPoint2f points2)
{
    Mat points1_mat = points1;
    Mat points2_mat = points2;
    Mat retVal = new Mat(findFundamentalMat_2(points1_mat.nativeObj, points2_mat.nativeObj));

    return retVal;
}
 [...]
 private static native long findFundamentalMat_2(long points1_mat_nativeObj, long points2_mat_nativeObj);

And it's native part :

JNIEXPORT jlong JNICALL Java_org_opencv_calib3d_Calib3d_findFundamentalMat_12 (JNIEnv*, jclass, jlong, jlong);

JNIEXPORT jlong JNICALL Java_org_opencv_calib3d_Calib3d_findFundamentalMat_12
  (JNIEnv* env, jclass , jlong points1_mat_nativeObj, jlong points2_mat_nativeObj)
{
    static const char method_name[] = "calib3d::findFundamentalMat_12()";
    try {
        LOGD("%s", method_name);
        std::vector<Point2f> points1;
        Mat& points1_mat = *((Mat*)points1_mat_nativeObj);
        Mat_to_vector_Point2f( points1_mat, points1 );
        std::vector<Point2f> points2;
        Mat& points2_mat = *((Mat*)points2_mat_nativeObj);
        Mat_to_vector_Point2f( points2_mat, points2 );
        ::Mat _retval_ = cv::findFundamentalMat( points1, points2 );
        return (jlong) new ::Mat(_retval_);
    } catch(const std::exception &e) {
        throwJavaException(env, &e, method_name);
    } catch (...) {
        throwJavaException(env, 0, method_name);
    }
    return 0;
}

So, this should do the trick :

   JNIEXPORT void JNICALL Java_ImageProcessingActivity_rectfy (JNIEnv * jobject, jlong corners, inputMatAddress jlong){
                std::vector<Point2f> corners_vector;
                Mat& corners_mat = *((Mat*)corners);
                Mat_to_vector_Point2f( corners_mat_mat, corners_vector );
[...]
}
edit flag offensive delete link more

Comments

Thanks, but "Function 'Mat_to_vector_Point2f' could not be resolved", so what i need include on my .cpp ? Or i need implement this function ? @Yolteotl

jefferson-sep gravatar imagejefferson-sep ( 2015-08-13 20:12:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-08-13 16:40:02 -0600

Seen: 2,808 times

Last updated: Aug 13 '15