OpenCv4Android和C ++数据类型之间的混淆 [英] Confusion between OpenCv4Android and C++ data types

查看:685
本文介绍了OpenCv4Android和C ++数据类型之间的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写使用OpenCv4Android Android设备的一些应用。此前,我是使用的Andr​​oid NDK和C ++本机codeS。但是,技术是没有太大的清醒。所以,我切换到最新的Java API未来随着OpenCV的2.4.4版本。

我能够编写简单的程序,并运行样品。但是,尽管我试图写一些codeS为先进的问题,如 - 模型姿态估计,摄像机标定程序等,我碰到这个很奇怪的混乱。一些数据类型,它们的名字是非常直观的C ++ API并不真正适合在Java中对应的。因此,我面临的可怕困难端口从C ++到Java我的功能。我面临的鸡犬不宁这些功能

  • Point2f(在C ++) - MatOfPoint2f(在Java中)
  • Point3f(在C ++) - MatOfPoint3f(在Java中)
  • 点2(在Java中)
  • POINT3(在Java中)

请帮助我了解与C ++中的OpenCV Java中使用的术语和类比。

此外,请建议我一些参考的地方,这些术语的明确和清晰的描述给出了(我试过看的帮助下沿设置,但它并没有帮助我很多,因为它是C ++和Java的有点类似)

解决方案

引用安德烈Pavlenko:

  

MatOfXxx类(如MatOfPoint)被引入,以避免冗余   复制Java和本机内存之间的中间数据。例如。您   可以得到一些大点的集合为一体的OpenCV函数的结果   然后将它传递到另一个。

     

在C ++中,我们使用std ::矢量   为了这。但是,使用ArrayList的在Java中引起   从本地OpenCV的水平到Java的时候复制所有点的数据   调用next返回时,这些点并将其复制回来   使用它们OpenCV的功能。因此,为了提高效率,我们切换   使用MatOfPoint类在这种情况下这是一种1n的垫的   或N1的尺寸,保持一个点中的每个元素(即类型   CV_32SC2或CV_64FC2)。

     

正如你可能知道,垫不断的所有数据   母语水平,所以这样的对象可以OpenCV的通话之间传递   没有数据复制。但是,如果在你的Java code在某些时候,你需要   直接访问实际点的数据存在的toArray()和fromArray   方法来明确的将数据传输到/从Java。

     

例如,创建一个MatOfPoint2f   包含对应于那些从现有的分   MatOfKeyPoint你需要:

     
      
  • 通过MatOfKeyPoint.toArray负载关键点到Java()
  •   
  • 在遍历关键点[]和创建一个相应的点[](所有品种的::点,CV :: Point2f和CV ::的Point2D重新presented作为   org.opencv.core.Point在Java中)
  •   
  • 使用MatOfPoint2f.fromArray()或C-器MatOfPoint2f(点...... PA)把您的积分,以母语水平
  •   
     

对于C ++ VS Java类型对应关系:

 矢量<点和GT; :MatOfPoint
矢量< Point2f> :MatOfPoint2f
矢量< Point3i> :MatOfPoint3
矢量< Point3f> :MatOfPoint3f
矢量<关键点> :MatOfKeyPoint
矢量< DMatch> :MatOfDMatch
矢量<矩形> :MatOfRect
矢量< UCHAR> :MatOfByte
矢量<焦炭> :MatOfByte
矢量< int的> :MatOfInt
矢量<浮动> :MatOfFloat
矢量<双> :MatOfDouble
矢量< Vec4i> :MatOfInt4
矢量< Vec4f> :MatOfFloat4
 

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. So i switched over to latest Java API coming along with OpenCv 2.4.4 version.

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. Hence, I am facing terrible difficulty to port my functionality from C++ to Java. I am facing utter confusion in these functions

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

Please help me understand the terms used in OpenCv Java and its analogy with 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).

解决方案

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

这篇关于OpenCv4Android和C ++数据类型之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆