What's the fastest way to iterate over a CvMat in Python using OpenCV?
Question: I'm using OpenCV with Python to process a video stream. 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. Numpy may be the way to go, but I'm not yet familiar
with it.

    
    
    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


Answer: OpenCV has pretty good python documentation
[here](http://opencv.willowgarage.com/documentation/python/index.html).
Basically you should always try to do operations on video frames using these
builtin opencv functions, or numpy. For frame processing take a look at
[operations on
arrays](http://opencv.willowgarage.com/documentation/python/core_operations_on_arrays.html),
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.

