[Numpy-discussion] fast grayscale conversion

Zachary Pincus zachary.pincus@yale....
Mon Jun 20 15:41:43 CDT 2011


You could try:
src_mono = src_rgb.astype(float).sum(axis=-1) / 3.

But that speed does seem slow. Here are the relevant timings on my machine (a recent MacBook Pro) for a 3.1-megapixel-size array:
In [16]: a = numpy.empty((2048, 1536, 3), dtype=numpy.uint8)

In [17]: timeit numpy.dot(a.astype(float), numpy.ones(3)/3.)
10 loops, best of 3: 116 ms per loop

In [18]: timeit a.astype(float).sum(axis=-1)/3.
10 loops, best of 3: 85.3 ms per loop

In [19]: timeit a.astype(float)
10 loops, best of 3: 23.3 ms per loop




On Jun 20, 2011, at 4:15 PM, Alex Flint wrote:

> At the moment I'm using numpy.dot to convert a WxHx3 RGB image to a grayscale image:
> 
> src_mono = np.dot(src_rgb.astype(np.float), np.ones(3)/3.);
> 
> This seems quite slow though (several seconds for a 3 megapixel image) - is there a more specialized routine better suited to this?
> 
> Cheers,
> Alex
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list