简体   繁体   English

Android 设备中的 OpenCV Java API 或 OpenCV C++ API?

[英]OpenCV Java API or OpenCV C++ API in Android devices?

I've seen 2 ways of using OpenCV in Android devices:我见过两种在 Android 设备中使用 OpenCV 的方法:

  1. with Java OpenCV API使用 Java OpenCV API
  2. with C++ OpenCV API使用 C++ OpenCV API

My question is: which one is better, faster, more easy to configure/code?我的问题是:哪个更好、更快、更容易配置/编码? Or, if my question is wrong, what are the pros and cons for each way?或者,如果我的问题是错误的,每种方式的优缺点是什么? I read OpenCV documentation and several articles, but couldn't find the answer for my question.我阅读了 OpenCV 文档和几篇文章,但找不到我的问题的答案。

I guess it has to do more about your application's implementation.我想它必须对您的应用程序的实现做更多的事情。 Is it a java or a c++ (native) application?它是 Java 还是 C++(本机)应用程序? Is image processing an important load of your application?图像处理是应用程序的重要负载吗? Do you need computing speed or implementation speed?您需要计算速度还是执行速度? Anyway, I've used OpenCV (C++ implementation) in java via the JNI and the NDK.无论如何,我已经通过 JNI 和 NDK 在 Java 中使用了 OpenCV(C++ 实现)。 The core of my application is image processing and processing speed is very much in my interest (almost real-time).我的应用程序的核心是图像处理,处理速度非常符合我的兴趣(几乎是实时的)。 Everything is written in C++, as I have both desktop and mobile implementations of my imaging system.一切都是用 C++ 编写的,因为我有我的成像系统的桌面和移动实现。

In the mobile application (for android), my imaging system is embedded in a java application.在移动应用程序(适用于 android)中,我的成像系统嵌入在一个 java 应用程序中。 From here, I see processing split into two parts: Image acquisition (via the device camera) and image processing (via OpenCV).从这里,我看到处理分为两部分:图像采集(通过设备摄像头)和图像处理(通过 OpenCV)。 Image acquisition is all made in java.图像采集全部用java进行。 The work wasn't trivial, as the main problem I found was acquiring RGB frames from the camera (natively YUV), passing them to OpenCV, receiving results from OpenCV and rendering them to the application's surfaceview.这项工作并非微不足道,因为我发现的主要问题是从相机(本地 YUV)获取 RGB 帧,将它们传递给 OpenCV,从 OpenCV 接收结果并将它们渲染到应用程序的表面视图。

Interfacing the camera and OpenCV can be a headache indeed, depending mainly on your device capabilities and your SDKs versions.连接相机和 OpenCV 确实令人头疼,这主要取决于您的设备功能和 SDK 版本。 After acquiring the image, processing was relatively painless.获取图像后,处理相对轻松。 All my system was previously debugged on my PC and I knew exactly what to expect.我所有的系统之前都在我的 PC 上调试过,我确切地知道会发生什么。 All OpenCV functions behaved as they should, so I had almost no problems with this part.所有 OpenCV 函数都按其应有的方式运行,因此我对这部分几乎没有问题。

I had also spent a lot of time working in C++, so that was a factor in deciding what kind of implementation to choose.我也花了很多时间在 C++ 上工作,所以这是决定选择什么样的实现的一个因素。 Now, the application is pretty much setup.现在,该应用程序已基本设置完毕。 I can add new features and test them in my PC and update the mobile port rather quickly.我可以添加新功能并在我的 PC 上测试它们,并相当快地更新移动端口。

The OpenCV Java API is the standard way of using OpenCV with Android. OpenCV Java API 是在 Android 上使用 OpenCV 的标准方式。 It's basically a Java layer on top of the C++ API, where each OpenCV Java function is calling its C++ equivalent through JNI ( Java Native Interface ).它基本上是 C++ API 之上的 Java 层,其中每个 OpenCV Java 函数都通过 JNI( Java 本机接口)调用其 C++ 等效项。

Making a JNI call induces a small overhead, increasing the execution time of your OpenCV function when using the Java API.进行 JNI 调用会产生少量开销,从而在使用 Java API 时增加 OpenCV 函数的执行时间。 If your image processing pipeline is making a lot of calls to OpenCV functions (let's say N calls), then the JNI calls overhead will affect N times your pipeline, which can slow down your total computation time.如果您的图像处理管道对 OpenCV 函数进行了大量调用(假设为 N 次调用),那么 JNI 调用开销将影响管道的 N 倍,这会减慢您的总计算时间。

An alternative is to implement your entire image processing pipeline in C++ and call that C++ code from Java, reducing the overhead to only one JNI call.另一种方法是用 C++ 实现整个图像处理管道,并从 Java 调用该 C++ 代码,将开销减少到仅一个 JNI 调用。

OpenCV C++ pros : OpenCV C++ 优点:

  1. Speed速度
  2. Reusability of existing C++ code现有 C++ 代码的可重用性
  3. All OpenCV API available所有 OpenCV API 可用

OpenCV Java pros : OpenCV Java 优点:

  1. Easier to deploy更容易部署
  2. Easier to implement更容易实施
  3. Easier to debug更容易调试

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

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