简体   繁体   English

使用OpenCV在Python中迭代CvMat的最快方法是什么?

[英]What's the fastest way to iterate over a CvMat in Python using OpenCV?

I'm using OpenCV with Python to process a video stream. 我将OpenCV与Python配合使用来处理视频流。 I'd like to implement my own algorithm, so I need to iterate over each frame. 我想实现自己的算法,因此需要遍历每个帧。

What I have so far works, but way too slow to be real-time. 到目前为止,我的工作正常,但是太慢了,无法实时进行。 I know that Python isn't the most efficient programming language, but I believe it can do much better than this, considering, that the built in image transformation functions are very fast. 我知道Python并不是最有效的编程语言,但是考虑到内置的图像转换功能非常快,我相信它可以做得更好。 Numpy may be the way to go, but I'm not yet familiar with it. Numpy可能是要走的路,但是我还不熟悉。

import cv, numpy
vidFile = cv.CaptureFromFile( 'sample.avi' )
nFrames = int(  cv.GetCaptureProperty( vidFile, cv.CV_CAP_PROP_FRAME_COUNT ) )
for f in xrange( nFrames ):
  frameImg = cv.QueryFrame( vidFile )
  frameMat=cv.GetMat(frameImg)
  print "mat ", mat[3,1]
  for x in xrange(frameMat.cols):
    for y in xrange(frameMat.rows):
        # just an example, multiply all 3 components by 0.5
        frameMat[y, x] = tuple(c*0.5 for c in frameMat[y, x])
  cv.ShowImage( "My Video Window",  frameMat )
  if cv.WaitKey( waitPerFrameInMillisec  ) == 27:
    break

How can I speed up the process? 我如何加快这一过程? Thanks, b_m 谢谢,b_m

OpenCV has pretty good python documentation here . OpenCV 在这里有相当不错的python文档。 Basically you should always try to do operations on video frames using these builtin opencv functions, or numpy. 基本上,您应该始终尝试使用这些内置的opencv函数或numpy对视频帧进行操作。 For frame processing take a look at operations on arrays , using this you can replace your entire pixel by pixel processing loop, which is absurdly slow: 对于帧处理,请看一下对数组的操作 ,使用它可以用像素处理循环替换整个像素,这很慢:

frameMat=cv.GetMat(frameImg)
print "mat ", mat[3,1]
for x in xrange(frameMat.cols):
    for y in xrange(frameMat.rows):
        # just an example, multiply all 3 components by 0.5
        frameMat[y, x] = tuple(c*0.5 for c in frameMat[y, x])
cv.ShowImage( "My Video Window",  frameMat )

with: 有:

cv.ConvertScale(frameImg, frameImg, scale=0.5)
cv.ShowImage( "My Video Window",  frameImg )

and easily play it in real time, there are loads of cool functions allowing you to merge videos etc. 并且可以轻松地实时播放,还有很多很棒的功能,可让您合并视频等。

Python for loops are just too slow. Python for循环太慢了。 If you can express your algorithm using the built-in functions (or numpy or another extension module), do that. 如果您可以使用内置函数(或numpy或其他扩展模块)表达算法,请执行此操作。 For example, your multiply-by-constant example is easy to implement with ConvertScale. 例如,您的乘以常数示例很容易通过ConvertScale实现。 If the algorithm is more complicated, you'll have to implement it at C level. 如果算法更复杂,则必须在C级别实现它。 Cython is one popular way to make that easier. Cython是使这一过程变得更容易的一种流行方法。

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

相关问题 在 Python 中重新映射 OpenCV 中的像素值的最快方法是什么? - What's the fastest way to remap pixel values in OpenCV in Python? 在python中迭代的最快方法 - fastest way to iterate in python Python,迭代正则表达式但在第一次匹配时停止的最快方法 - Python, fastest way to iterate over regular expressions but stop on first match 在python中迭代图像的所有像素的最快方法 - fastest way to iterate over all pixels of an image in python 迭代包含 Python 中字符串的大型列表的最快方法? - Fastest way to iterate over a large list containing strings in Python? 在 Python 中迭代列表并找到合适的字符串模式的最快(最有效)方法是什么? - What's the fastest (most efficient) way to iterate through a list and find a fitting string pattern in Python? 在python(cv2)中使用OpenCV增加彩色图像对比度的最快方法是什么? - What's the fastest way to increase color image contrast with OpenCV in python (cv2)? 如何在OpenCV中使用python接口将列表对象分配给cvmat? - how to assign a list object to cvmat using python interface in OpenCV? 在python中扩展URL的最快方法是什么 - What's the fastest way to expand url in python 在 Python 中解析 arguments 的最快方法是什么? - What's the fastest way to parse arguments in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM