【问题标题】:openCV java code pass Point object to native code(C++)?openCV java代码将Point对象传递给本机代码(C++)?
【发布时间】:2014-04-14 16:16:05
【问题描述】:

我目前正在 Android 上开发一个 openCV 应用程序。到目前为止,我的应用程序都是用 Java 编写的,但是有一个函数将 MatOfPoint 对象作为我想在本机代码(C++)中实现的参数。从 openCV Tutorial 2 我知道如何将 Mat 对象传递给本机代码方法,但是其他类的对象,如 Point 和 MatOfPoint 呢?有任何示例代码说明如何操作吗?

谢谢。

【问题讨论】:

    标签: c++ opencv android-ndk java-native-interface


    【解决方案1】:

    对于一点,我建议只传递 x 和 y 作为双打。 对于 MatOfPoints 和其他:

    OpenCV 带有在 Mat 中存储不同类型内容的转换器。

    java端有转换器,是opencv-android-sdk的一部分 org.opencv.utils.Converters

    我在 opencv 存储库中还找到了一个 C++ 转换器,但标头不包含在 opencv-android-sdk 中,而且我无法通过仅添加标头来使其正常工作,因此我复制了 converters.h和 converters.cpp 到我的 jni 文件夹并更正导入以指向正确的位置(但这可能不是必需的)。

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

    此转换器可用于将 MatOfPoint 或 List 以及更多内容传递给您的本机代码,您必须再次将其转换为 c++ 类型(C++ 等价于 MatOfPoint 是 std::vector; List 的等价物是 std::vector>)

    使用此转换器,您可以使用 vector_Point_to_Mat 或 Mat_to_vector_Point 将 MatOfPoint “转换”为 Mat 并返回(返回一个 List,但使用 MatOfPoint.fromList 可以从中得到一个 MatOfPoint)

    这里是一个示例,如何将 List 传递到本机级别并返回一个 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);
    
    }
    

    C++ 代码:

    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 是你的自定义原生函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多