本文介绍了OpenCV的Python或C ++编码性能不同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是一点一点地开始opencv,但首先我需要决定OpenCV的哪个API更有用。我预测Python实现更短,但与本机C ++实现相比,运行时间将更加密集和缓慢。

I aim to start opencv little by little but first I need to decide which API of OpenCV is more useful. I predict that Python implementation is shorter but running time will be more dense and slow compared to the native C++ implementations. Is there any know can comment about performance and coding differences between these two perspectives?

推荐答案

如前面的答案所述,Python比较慢与C ++或C相比。Python的构建是因为它的简单性,可移植性和创造力,用户只需要关心他们的算法,而不是编程麻烦。

As mentioned in earlier answers, Python is slower compared to C++ or C. Python is built for its simplicity, portability and moreover, creativity where users need to worry only about their algorithm, not programming troubles.

OpenCV,有一些不同。 Python-OpenCV只是一个原始C / C ++代码的包装器。它通常用于组合两种语言的最佳特征, C / C ++和C ++的性能。

But here in OpenCV, there is something different. Python-OpenCV is just a wrapper around the original C/C++ code. It is normally used for combining best features of both the languages, Performance of C/C++ & Simplicity of Python.

因此,当你从Python中调用OpenCV中的函数时,实际运行的是底层的C / C ++源代码。所以在性能上没有太大差别(我记得我在某处读到性能损失小于1%,不记得在哪里)。对OpenCV中一些基本功能的粗略估计显示了一个最坏情况 penalty = [Python花费的最长时间 - C ++中花费的最短时间] C ++ )。

So when you call a function in OpenCV from Python, what actually run is underlying C/C++ source. So there won't be much difference in performance.( I remember I read somewhere that performance penalty is <1%, don't remember where. A rough estimate with some basic functions in OpenCV shows a worst-case penalty of <4%. ie penalty = [maximum time taken in Python - minimum time taken in C++]/minimum time taken in C++ ).

当你的代码有很多本地Python代码时就会出现问题。例如,如果你正在做自己的函数在OpenCV中不可用,事情变得更糟。这样的代码在Python中运行,这大大降低了性能。

The problem arises when your code has a lot of native python codes.For eg, if you are making your own functions that are not available in OpenCV, things get worse. Such codes are ran natively in Python, which reduces the performance considerably.

但是新的OpenCV-Python接口完全支持Numpy。 Numpy是Python中科学计算的包。它也是本地C代码的包装器。它是一个高度优化的库,支持各种矩阵运算,非常适合图像处理。所以如果你可以正确地组合OpenCV函数和Numpy函数,你会得到一个非常高的速度代码。

But new OpenCV-Python interface has full support to Numpy. Numpy is a package for scientific computing in Python. It is also a wrapper around native C code. It is a highly optimized library which supports a wide variety of matrix operations, highly suitable for image processing. So if you can combine both OpenCV functions and Numpy functions correctly, you will get a very high speed code.

要记住的是,总是尽量避免循环和迭代蟒蛇。相反,请使用Numpy(和OpenCV)中提供的数组处理工具。使用 C = A + B 简单地添加两个numpy数组比使用双循环要快很多倍。

Thing to remember is, always try to avoid loops and iterations in Python. Instead, use array manipulation facilities available in Numpy (and OpenCV). Simply adding two numpy arrays using C = A+B is a lot times faster than using double loops.

例如,您可以查看以下文章:

For eg, you can check these articles :




  1. Fast Array Manipulation in Python
  2. A query on performance comparison of OpenCV-Python interfaces, cv and cv2

这篇关于OpenCV的Python或C ++编码性能不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:45