[Numpy-discussion] Help speeding up element-wise operations for video processing

brendan simons spam4bsimons@yahoo...
Mon Sep 15 23:52:19 CDT 2008

Hi everyone

I have a quick problem.  I'm trying to write a routine to "demosaic" video data extremely quickly.  I thought my algorithm would make good use of my processor's vector math units, but it isn't fast enough (it currently takes about 300ms for each 8 megapixel frame)  Maybe you can help me speed it up?

Lets just look at one piece of the algorithm - interpolating the green pixels:  My camera hands me a frame as an int* c array of 8 bit integers. Forget the problem of turning that into a numpy array for now and assume I have the image as a numpy uint8 array "ain" .   Also assume that Greenmask is a precomputed array of 1s and 0s of the same size as ain.   Now, I do the following:

#interpolate the green pixels from the bayer filter image ain
gi = g[:-2, 1:-1].astype('uint16')
gi += g[2:, 1:-1]
gi += g[1:-1, :-2]
gi += g[1:-1, 2:]
gi /= 4
gi += g[1:-1, 1:-1]
return gi

I do something similar for red and blue, then stack the interpolated red, green and blue integers into an array of 24 bit integers and blit to the screen.

I was hoping that none of the lines would have to iterate over pixels, and would instead do the adds and multiplies as single operations. Perhaps numpy has to iterate when copying a subset of an array?  Is there a faster array "crop" ?  Any hints as to how I might code this part up using ctypes?