OpenCV (JavaCV) 与 OpenCV(C/C++ 接口)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21207755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV (JavaCV) vs OpenCV (C/C++ interfaces)
提问by Pelican
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.
我只是想知道在使用 JavaCV 而不是 OpenCV 的 C/C++ 实现时,在给定的一组机器上是否会有显着的速度性能优势。
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.
如果我错了,请纠正我,但我的理解是 opencv 的 c/c++ 实现更接近机器,而 OpenCV 的 Java 实现,JavaC,会有轻微的速度性能劣势(以毫秒为单位),因为会有将您的源代码转换为字节码的虚拟机,然后将其转换为机器代码。而使用 c/c++,它会直接转换为机器代码,因此不会携带虚拟机开销的中间步骤。
Please don't kill me here if I made mistakes; I am just learning and would welcome constructive criticism.
如果我犯了错误,请不要在这里杀了我;我只是在学习,欢迎有建设性的批评。
Thank you
谢谢
采纳答案by ffriend
I'd like to add a couple of things to @ejbs's answer.
我想在@ejbs 的回答中添加一些内容。
First of all, you concerned 2 separate issues:
首先,您关注两个不同的问题:
- Java vs. C++ performance
- OpenCV vs JavaCV
- Java 与 C++ 的性能
- OpenCV 与 JavaCV
Java vs. C++performance is a long, long story. On one hand, C++ programsare 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 bytecodeis compiled "just-in-time" and is always optimized for processor architecturethey 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.
Java 与 C++ 的性能是一个很长很长的故事。一方面,C++ 程序被编译为高度优化的本机代码。它们快速启动并始终快速运行,而不会因垃圾收集或其他 VM 任务而暂停(就像 Java 那样)。另一方面,一旦编译,C++ 中的程序无论在什么机器上运行都不能改变,而Java 字节码是“即时”编译的,并且总是针对处理器架构进行优化他们继续前进。在现代世界中,有这么多不同的设备(和处理器架构),这可能非常重要。此外,一些 JVM(例如 Oracle Hotspot)甚至可以优化已经编译为本机代码的代码!VM 收集有关程序执行的数据,并不时尝试以针对此特定执行进行优化的方式重写代码。因此,在如此复杂的情况下,比较不同编程语言的实现性能的唯一真正方法就是运行它们并查看结果。
OpenCV vs. JavaCVis another story. First you need to understand stack of technologies behind these libraries.
OpenCV 与 JavaCV是另一回事。首先,您需要了解这些库背后的技术栈。
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.
OpenCV 最初是在 1999 年在英特尔研究实验室创建的,是用 C 编写的。从那时起,它多次更改维护者,成为开源并达到第 3 版(即将发布)。目前,该库的核心是用 C++ 编写的,具有流行的 Python 接口和许多其他编程语言的包装器。
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).
JavaCV 就是这样的包装器之一。因此,在大多数情况下,当您使用 JavaCV 运行程序时,您实际上也在使用 OpenCV,只需通过另一个接口调用它即可。但是 JavaCV 提供的不仅仅是围绕 OpenCV 的一对一包装器。事实上,它捆绑了大量的图像处理库,包括 FFmpeg、OpenKinect 等。(请注意,在 C++ 中,您也可以绑定这些库)。
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.
因此,一般而言,无论您使用的是 OpenCV 还是 JavaCV,您都将获得几乎相同的性能。它更多地取决于您的主要任务 - 是 Java 还是 C++ 更适合您的需求。
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 filter2Dfunction 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.
关于性能还有一点很重要。使用 OpenCV(直接或通过包装器)有时您会发现 OpenCV 函数通过几个顺序克服了其他实现。这是因为在其核心中大量使用了低级优化。例如,OpenCV 的filter2D函数是SIMD加速的,因此可以并行处理多组数据。而在计算机视觉方面,这种对常用功能的优化很容易带来显着的加速。
回答by ejbs
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.
JavaCV 与 OpenCV 接口,所以当你调用 OpenCV 相关的东西时会有一些开销,但通常大部分繁重的工作仍然在 C++ 端,因此不会有非常大的性能损失。
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.
附注。我在这里很新,但我很确定这不是 StackOverflow 的合适问题。
回答by shadowMaster
i would like to add a few more insights on java as an interface to c++ libraries...
我想添加一些关于 Java 作为 C++ 库接口的更多见解......
A)developing:
A) 开发:
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...
1)虽然java可能更容易管理大型项目并且编译速度非常快,但它非常非常困难,几乎不可能从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...
2)除非你自己构建绑定(即使使用 swig 或其他任何东西也不是一件容易的事......)你依赖于绑定构建器的善意/健康/时间......所以在这种情况下,我更喜欢javacv 上的官方“桌面 java”绑定...
B)performance.
B) 性能。
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...
1) 虽然绑定可以优化(使用 neobuffer 的内存传输),如在 javacv 情况下,每个本机函数调用的 jni 开销仍然非常小 - 这在我们的情况下是没有意义的,因为与此相比,大多数 opencv 函数消耗 X100000++ cpu 周期jni开销...
2) The BIG-PROBLEM ---- stop the world GARBAGE COLLECTIOR( GC )
2)大问题----停止世界垃圾收集器(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.... )
java 使用垃圾收集器来停止所有 cpu 的线程,使其不适合实时应用程序,听说有一些变通方法,例如重新设计您的应用程序以不产生垃圾,使用 spaciel gc,或使用实时 java(花钱... )他们似乎都是额外的工作(你想要的只是一个很好的 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....
结论 - 如果你想创建一个专业的实时应用程序 - 除非你有一个巨大的模块化项目要管理,否则请使用 c++ - 只需坚持使用 c++ 和预编译头文件(使事情编译更快......)而 java 是一种工作乐趣,当谈到本机绑定时,地狱会崩溃……我知道我去过那里……