简体   繁体   English

OpenCv4Android和C ++数据类型之间的混淆

[英]Confusion between OpenCv4Android and C++ data types

I am trying to write some applications using OpenCv4Android for Android devices. 我正在尝试使用适用于Android设备的OpenCv4Android编写一些应用程序。 Earlier, i was using Android NDK and C++ native codes. 之前,我使用的是Android NDK和C ++本机代码。 But that technique wasn't much lucid. 但是这种技术并不是很清楚。 So i switched over to latest Java API coming along with OpenCv 2.4.4 version. 因此,我切换到了OpenCv 2.4.4版本附带的最新Java API。

I was able to write simple programs and run samples. 我能够编写简单的程序并运行示例。 But, while i tried to write some codes for advanced problems like - Model POSE estimation, Camera calibration routines etc, I came across this very strange confusion. 但是,尽管我尝试编写一些高级问题代码,例如-模型姿势估计,相机校准例程等,但遇到了这种非常奇怪的混乱。 Some of the data types whose names are very intuitive in C++ API doesn't really fit in in their Java counterpart. 在C ++ API中名称非常直观的某些数据类型实际上并不适合它们的Java对应类型。 Hence, I am facing terrible difficulty to port my functionality from C++ to Java. 因此,我面临将我的功能从C ++移植到Java的巨大困难。 I am facing utter confusion in these functions 我在这些功能上面临着彻底的困惑

  • Point2f (in C++ ) - MatOfPoint2f (in Java) Point2f(在C ++中)-MatOfPoint2f(在Java中)
  • Point3f (in C++) - MatOfPoint3f (in Java) Point3f(在C ++中)-MatOfPoint3f(在Java中)
  • Point2 (in Java) Point2(在Java中)
  • Point3 (in Java) Point3(在Java中)

Please help me understand the terms used in OpenCv Java and its analogy with C++. 请帮助我了解OpenCv Java中使用的术语及其与C ++的类比。

Also, please suggest me some reference where, clear and crisp description of these terms are given (i tried watching the help provided along, but it didn't help me much, as it was somewhat similar for both C++ and Java). 另外,请给我建议一些参考,在其中给出了这些术语的清晰明了的描述(我尝试观察提供的帮助,但并没有太大帮助,因为C ++和Java都有些相似)。

Quoting Andrey Pavlenko: 引用安德烈·帕夫连科的话:

MatOfXxx classes (eg MatOfPoint) were introduced to avoid redundant copying of intermediate data between Java and native memory. 引入了MatOfXxx类(例如MatOfPoint)是为了避免Java和本机内存之间中间数据的冗余复制。 Eg you can get some large set of Points as the result of one OpenCV function and then pass it to another one. 例如,作为一个OpenCV函数的结果,您可以获得一些大的积分,然后将其传递给另一个。

In C++ we use std::vector for this. 在C ++中,我们为此使用std :: vector。 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. 但是在Java中使用ArrayList会导致在返回这些Point时将所有Points数据从本机OpenCV级别复制到Java,并在使用它们调用下一个OpenCV函数时将它们复制回。 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 (ie of type CV_32SC2 or CV_64FC2). 因此,为了提高效率,我们在这种情况下改用MatOfPoint类,这是一种尺寸为1n或n1的Mat,它在每个元素中保留一个Point(即CV_32SC2或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. 如您所知,Mat将所有数据保留在本机级别,因此可以在OpenCV调用之间传递此类对象,而无需复制数据。 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. 但是,如果在某些时候需要在Java代码中直接访问实际的Points数据,则可以使用toArray()和fromArray方法来显式向Java传输数据或从Java传输数据。

For example, to create a MatOfPoint2f containing the points corresponding to ones from existing MatOfKeyPoint you need: 例如,要创建一个MatOfPoint2f,其中包含与现有MatOfKeyPoint中的点相对应的点,您需要:

  • load KeyPoints to Java via MatOfKeyPoint.toArray() 通过MatOfKeyPoint.toArray()将KeyPoints加载到Java
  • 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) 遍历KeyPoint []并创建一个对应的Point [](所有cv :: Point,cv :: Point2f和cv :: Point2d在Java中都表示为org.opencv.core.Point)
  • use MatOfPoint2f.fromArray() or c-tor MatOfPoint2f(Point...pa)to put your Points to native level 使用MatOfPoint2f.fromArray()或c-tor MatOfPoint2f(Point ... pa)将您的Point设为本机级别

As for the C++ vs Java types correspondence: 至于C ++与Java类型的对应关系:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM