简体   繁体   中英

OpenCV Java API or OpenCV C++ API in Android devices?

I've seen 2 ways of using OpenCV in Android devices:

  1. with Java OpenCV API
  2. with 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.

I guess it has to do more about your application's implementation. Is it a java or a c++ (native) application? 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. 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.

In the mobile application (for android), my imaging system is embedded in a java application. From here, I see processing split into two parts: Image acquisition (via the device camera) and image processing (via OpenCV). Image acquisition is all made in 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.

Interfacing the camera and OpenCV can be a headache indeed, depending mainly on your device capabilities and your SDKs versions. After acquiring the image, processing was relatively painless. All my system was previously debugged on my PC and I knew exactly what to expect. All OpenCV functions behaved as they should, so I had almost no problems with this part.

I had also spent a lot of time working in C++, so that was a factor in deciding what kind of implementation to choose. 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.

The OpenCV Java API is the standard way of using OpenCV with Android. 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 ).

Making a JNI call induces a small overhead, increasing the execution time of your OpenCV function when using the Java API. 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.

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.

OpenCV C++ pros :

  1. Speed
  2. Reusability of existing C++ code
  3. All OpenCV API available

OpenCV Java pros :

  1. Easier to deploy
  2. Easier to implement
  3. Easier to debug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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