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

Brendan Simons spam4bsimons@yahoo...
Tue Sep 16 18:43:45 CDT 2008

On 16-Sep-08, at 4:50 AM, Stéfan van der Walt wrote:

> Hi Brendan
> 2008/9/16 brendan simons <spam4bsimons@yahoo.ca>:
>> #interpolate the green pixels from the bayer filter image ain
>> g = greenMask * 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 may be completely off base here, but you should be able to do this
> *very* quickly using your GPU, or even just using OpenGL.  Otherwise,
> coding it up in ctypes is easy as well (I can send you a code snippet,
> if you need).

I thought so too.  I briefly researched using the GPU, but I found  
that, surprisingly,  neither multitexturing in pyopengl, nor surface  
blitting in pygame was any faster than the numpy code I posted above.   
That could be because I'm working on a machine with an integrated  
graphics chip.

I would love a c-types code snippet.  I'm not very handy in c.  Since  
I gather numpy is row-major, I thought I up and down crops very  
quickly by moving the start and end pointers of the array.  For  
cropping left and right, is there a fast c command for "copy while  
skipping every nth hundred bytes"?

>> 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?
> Have you tried formulating this as a convolution, and using
> scipy.signal's 2-d convolve or fftconvolve?

I just read through the scipy tutorial on signal.convolve, and I'm a  
bit lost.  It's been years since I've last taken a numerical methods  
class.  Maybe there's something here - but I'm going to have to learn  
a bit first.

Thanks for your help,

More information about the Numpy-discussion mailing list