OpenCV (JavaCV) vs OpenCV (C/C++ interfaces)

21,220

Solution 1

I'd like to add a couple of things to @ejbs's answer.

First of all, you concerned 2 separate issues:

  1. Java vs. C++ performance
  2. OpenCV vs JavaCV

Java vs. C++ performance is a long, long story. On one hand, C++ programs are compiled to a highly optimized native code. They start quickly and run fast all the time without pausing for garbage collection or other VM duties (as Java do). On other hand, once compiled, program in C++ can't change, no matter on what machine they are run, while Java bytecode is compiled "just-in-time" and is always optimized for processor architecture they run on. In modern world, with so many different devices (and processor architectures) this may be really significant. Moreover, some JVMs (e.g. Oracle Hotspot) can optimize even the code that is already compiled to native code! VM collect data about program execution and from time to time tries to rewrite code in such a way that it is optimized for this specific execution. So in such complicated circumstances the only real way to compare performance of implementations in different programming languages is to just run them and see the result.

OpenCV vs. JavaCV is another story. First you need to understand stack of technologies behind these libraries.

OpenCV was originally created in 1999 in Intel research labs and was written in C. Since that time, it changed the maintainer several times, became open source and reached 3rd version (upcoming release). At the moment, core of the library is written in C++ with popular interface in Python and a number of wrappers in other programming languages.

JavaCV is one of such wrappers. So in most cases when you run program with JavaCV you actually use OpenCV too, just call it via another interface. But JavaCV provides more than just one-to-one wrapper around OpenCV. In fact, it bundles the whole number of image processing libraries, including FFmpeg, OpenKinect and others. (Note, that in C++ you can bind these libraries too).

So, in general it doesn't matter what you are using - OpenCV or JavaCV, you will get just about same performance. It more depends on your main task - is it Java or C++ which is better suited for your needs.

There's one more important point about performance. Using OpenCV (directly or via wrapper) you will sometimes find that OpenCV functions overcome other implementations by several orders. This is because of heavy use of low-level optimizations in its core. For example, OpenCV's filter2D function is SIMD-accelerated and thus can process several sets of data in parallel. And when it comes to computer vision, such optimizations of common functions may easily lead to significant speedup.

Solution 2

JavaCV interfaces to OpenCV, so when you call something OpenCV related there would be some overhead but in general most of the heavy work will still be on the C++ side and therefore there won't be a very large performance penalty.

You would have to do performance benchmarks to find out more.

PS. I'm pretty new here but I'm rather sure that this is not a suitable question for StackOverflow.

Solution 3

i would like to add a few more insights on java as an interface to c++ libraries...

A)developing:

1)while java may be easier to manage large scale projects and compiles extremely fast, it is very very hard, and next to impossible to debug native code from java...

when code crush on native side...or memory leaks( something that happens a lot... ) you feel kind of helpless...

2)unless you build the bindings yourself( not an easy task even with using swig or whatever... ) you are dependent on the good will/health/time of the bindings builder.... so in this case i would prefer the official "desktop java " bindings over javacv...

B)performance.

1) while bindings may be optimized( memory transfer using neobuffer ) as in the javacv case there is still a very very small jni overhead for each native function call - this is meaningless in our case since most opencv functions consume X100000++ cpu cycles compared to this jni overhead...

2) The BIG-PROBLEM ---- stop the world GARBAGE COLLECTIOR( GC )

java uses a garbage collector that halts all cpu's threads making it UNSUITABLE for REAL-TIME application's , there are work-around's iv'e heard of like redesigning your app not to produce garbage, use a spaciel gc, or use realtime java( cost money... ) they all seem to be extra-work( and all you wanted is a nice easy path to opencv.... )

conclusion - if you want to create a professional real time app - then go with c++ unless you have a huge modular project to manage - just stick with c++ and precompiled headers( make things compile faster... ) while java is a pleasure to work with , when it comes to native binding's HELL breaks loose...i know iv'e been there....

Share:
21,220
Pelican
Author by

Pelican

College student in Electrical and Computer Engineering trying to get better at programming through repeated programming exercises and asking questions on social platforms such as this one. Thank you!

Updated on July 15, 2022

Comments

  • Pelican
    Pelican almost 2 years

    I am just wondering whether there would be a significant speed performance advantage relatively on a given set of machines when using JavaCV as opposed to the C/C++ implementation of OpenCV.

    Please correct me if I am wrong, but my understanding is that the c/c++ implementation of opencv is closer to the machine where as the Java implementation of OpenCV, JavaC, would have a slight speed performance disadvantage (in milliseconds) as there would be a virtual machine converting your source code to bytecode which then gets converted to machine code. Whereas, with c/c++, it gets converted straight to machine code and thus doesn't carry that intermediary step of the virtual machine overhead.

    Please don't kill me here if I made mistakes; I am just learning and would welcome constructive criticism.

    Thank you