[Numpy-discussion] fast grayscale conversion

Christopher Barker Chris.Barker at noaa.gov
Mon Jun 20 19:41:00 EDT 2011


Alex Flint wrote:
> Thanks, that's helpful. I'm now getting comparable times on a different 
> machine, it must be something else slowing down my machine more 
> generally, not just numpy.

you also might want to get a bit fancier than simply scaling linearly 
R,G, and B don't necessarily all contribute equally to our sense of 
"whiteness"

For instance, PIL uses:

"""
When from a colour image to black and white, the library uses the ITU-R 
601-2 luma transform:

     L = R * 299/1000 + G * 587/1000 + B * 114/1000
"""

which would be easy enough to do with numpy.

-Chris


> 
> On Mon, Jun 20, 2011 at 5:11 PM, Eric Firing <efiring at hawaii.edu 
> <mailto:efiring at hawaii.edu>> wrote:
> 
>     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
>      >> NumPy-Discussion at scipy.org <mailto:NumPy-Discussion at scipy.org>
>      >> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>      >
>      > _______________________________________________
>      > NumPy-Discussion mailing list
>      > NumPy-Discussion at scipy.org <mailto:NumPy-Discussion at scipy.org>
>      > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
>     _______________________________________________
>     NumPy-Discussion mailing list
>     NumPy-Discussion at scipy.org <mailto:NumPy-Discussion at scipy.org>
>     http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list