OpenCV java code pass Point object for native code (C ++)?

I am currently developing an openCV application on Android. So far, my application is written in Java, but there is one function that takes a MatOfPoint object as a parameter that I want to implement in native code (C ++). From openCV Tutorial 2 I know how to pass a Mat object to my own code method, but what about objects of another class, such as Point and MatOfPoint? Any sample code that shows how to do this?

Thank.

+4
source share
1 answer

For the point, I recommend just passing x and y as doubles. For MatOfPoints and others:

OpenCV , Mat.

java, opencv-android-sdk org.opencv.utils.Converters

++, opencv-, opencv-android-sdk, , converters.h converters.cpp jni , ( ).

https://github.com/Itseez/opencv/blob/master/modules/java/generator/src/cpp/converters.cpp

MatOfPoint List , ++ ( ++ MatOfPoint - std::vector < cv:: Point > ; std::vector < std::vector < cv:: Point → )

"" MatOfPoint Mat vector_Point_to_Mat Mat_to_vector_Point ( , MatOfPoint.fromList MatOfPoint )

, MatOfPoint.

Java:

public class YourJavaWrapper {

    static {
        System.loadLibrary("yourlibrary");
    }

    public static MatOfPoint findMostFencyMatOfPoints(List<MatOfPoint> contours){

        List<Mat> contoursTmp = new ArrayList<Mat>(contours.size());
        Mat inputMat = Converters.vector_vector_Point_to_Mat(contours, contoursTmp);
        Mat outputMat = new Mat();

        findMostFencyMatOfPoints(inputMat.nativeObj, outputMat.nativeObj);

        List<Point> pointsTmp = new ArrayList<Point>();
        Converters.Mat_to_vector_Point(outputMat, pointsTmp);
        MatOfPoint matOfInterest = new MatOfPoint();
        matOfInterest.fromList(pointsTmp);
        outputMat.release();

        return matOfInterest;
    }

    private static native void findMostFencyMatOfPoints(long inputMatAddress, long outPutMatAddress);

}

++:

using namespace std;
using namespace cv;

extern "C" JNIEXPORT void JNICALL Java_org_example_yourpackage_YourJavaWrapper_findMostFencyMatOfPoints(JNIEnv*, jobject, jlong inputMatAddress, jlong outPutMatAddress)
{
    cv::Mat& vectorVectorPointMat = *(cv::Mat*) inputMatAddress;
    std::vector< std::vector< cv::Point > > contours;
    Mat_to_vector_vector_Point(vectorVectorPointMat, contours);
    std::vector<cv::Point> fencyVectorOfPoints = findMostFencyVectorOfPoint(contours);
    cv::Mat& largestSquareMat = *(cv::Mat*) outPutMatAddress;
    vector_Point_to_Mat(fencyVectorOfPoints, outPutMatAddress);
}

findMostFencyVectorOfPoint -

+3

Source: https://habr.com/ru/post/1531116/


All Articles