On 06/20/2011 10:41 AM, Zachary Pincus wrote: > 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 my slower machine (older laptop, core2 duo), you can speed it up more: In [3]: timeit a.astype(float).sum(axis=-1)/3.0 1 loops, best of 3: 235 ms per loop In [5]: timeit b = a.astype(float).sum(axis=-1); b /= 3.0 1 loops, best of 3: 181 ms per loop In [7]: timeit b = a.astype(np.float32).sum(axis=-1); b /= 3.0 10 loops, best of 3: 148 ms per loop If you really want float64, it is still faster to do the first operation with single precision: In [8]: timeit b = a.astype(np.float32).sum(axis=-1).astype(np.float64); b /= 3.0 10 loops, best of 3: 163 ms per loop Eric > > > 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 >> [email protected] >> http://mail.scipy.org/mailman/listinfo/numpy-discussion > > _______________________________________________ > NumPy-Discussion mailing list > [email protected] > http://mail.scipy.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
