Confusion between OpenCv4Android and C++ data types

后端 未结 1 568
醉话见心
醉话见心 2021-01-05 05:04

I am trying to write some applications using OpenCv4Android for Android devices. Earlier, i was using Android NDK and C++ native codes. But that technique wasn\'t much lucid

相关标签:
1条回答
  • 2021-01-05 05:48

    Quoting Andrey Pavlenko:

    MatOfXxx classes (e.g. MatOfPoint) were introduced to avoid redundant copying of intermediate data between Java and native memory. E.g. you can get some large set of Points as the result of one OpenCV function and then pass it to another one.

    In C++ we use std::vector for this. But use of ArrayList in Java caused copying all the Points data from native OpenCV level to Java when returning these Points and copying them back when calling the next OpenCV function using them. So for the sake of efficiency we switched to use of MatOfPoint class in such cases that is a kind of Mat of 1n or n1 dimensions that keeps a Point in each element (i.e. of type CV_32SC2 or CV_64FC2).

    As you may know, Mat keeps all the data on native level, so such objects can be passed between OpenCV calls without data copying. But if in your Java code at some point you need direct access to actual Points data there are toArray() and fromArray methods to explicit transfer data to/from Java.

    For example, to create a MatOfPoint2f containing the points corresponding to ones from existing MatOfKeyPoint you need:

    • load KeyPoints to Java via MatOfKeyPoint.toArray()
    • iterate through KeyPoint[] and create a corresponding Point[] (all of cv::Point, cv::Point2f and cv::Point2d are represented as org.opencv.core.Point in Java)
    • use MatOfPoint2f.fromArray() or c-tor MatOfPoint2f(Point...pa)to put your Points to native level

    As for the C++ vs Java types correspondence:

    vector<Point>    : MatOfPoint
    vector<Point2f>  : MatOfPoint2f
    vector<Point3i>  : MatOfPoint3
    vector<Point3f>  : MatOfPoint3f
    vector<KeyPoint> : MatOfKeyPoint
    vector<DMatch>   : MatOfDMatch
    vector<Rect>     : MatOfRect
    vector<uchar>    : MatOfByte
    vector<char>     : MatOfByte
    vector<int>      : MatOfInt
    vector<float>    : MatOfFloat
    vector<double>   : MatOfDouble
    vector<Vec4i>    : MatOfInt4
    vector<Vec4f>    : MatOfFloat4
    
    0 讨论(0)
提交回复
热议问题