Broadcasting question

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Broadcasting question

Chris Barker - NOAA Federal
Hi all,

I have a n X m X 3 array, and I a n X M array. I want to assign the
values in the n X m to all three of the slices in the bigger array:

A1 = np.zeros((5,4,3))
A2 = np.ones((5,4))
A1[:,:,0] = A2
A1[:,:,1] = A2
A1[:,:,2] = A2

However,it seems I should be able to broadcast that, so I don't have to
repeat myself, but I can't figure out how.

The full question:

I have a (w,h) array of ints, representing a grey scale image. I'm
trying to convert that into a RBG image. I've played with defining the
image array as a rec array or a h,w,3 uint8 array. neither one lets me
assign the three bands to the same value in one step:

import numpy as np

w = 2
h = 2

grey = np.arange(w*h).reshape((w,h))

RGBrec = np.dtype({'r':(np.uint8,0),
                    'g':(np.uint8,1),
                    'b':(np.uint8,2),
                    })

ImageRGB = np.zeros((w,h), dtype=RGBrec)
ImageInt = Image1.view((np.uint8, 3))

## neither of these work:
#ImageRGB[:,:] = grey
#ImageInt[:,:] = grey
print ImageRGB

How else might I do this?

-Chris




--
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

[hidden email]
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Broadcasting question

Charles R Harris


On Thu, May 1, 2008 at 10:58 PM, Chris.Barker <[hidden email]> wrote:
Hi all,

I have a n X m X 3 array, and I a n X M array. I want to assign the
values in the n X m to all three of the slices in the bigger array:

A1 = np.zeros((5,4,3))
A2 = np.ones((5,4))
A1[:,:,0] = A2
A1[:,:,1] = A2
A1[:,:,2] = A2

A1 += A2[:,:,newaxis] is one way. Or you could just start by stacking copies of A2 and swapping axes around:

In [5]: A1 = array([A2.T]*3).T

In [6]: A1.shape
Out[6]: (4, 5, 3)

Chuck

_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Broadcasting question

Anne Archibald
In reply to this post by Chris Barker - NOAA Federal
2008/5/2 Chris.Barker <[hidden email]>:

> Hi all,
>
>  I have a n X m X 3 array, and I a n X M array. I want to assign the
>  values in the n X m to all three of the slices in the bigger array:
>
>  A1 = np.zeros((5,4,3))
>  A2 = np.ones((5,4))
>  A1[:,:,0] = A2
>  A1[:,:,1] = A2
>  A1[:,:,2] = A2
>
>  However,it seems I should be able to broadcast that, so I don't have to
>  repeat myself, but I can't figure out how.

All you need is a newaxis on A2:
In [2]: A = np.zeros((3,4))

In [3]: B = np.ones(3)

In [4]: A[:,:] = B[:,np.newaxis]

In [5]: A
Out[5]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])


Anne
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Broadcasting question

Chris Barker - NOAA Federal
In reply to this post by Charles R Harris

Charles R Harris wrote:
> A1 += A2[:,:,newaxis] is one way.

Exactly what I was looking for -- thanks Charles (and Ann)

-Chris

--
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

[hidden email]
_______________________________________________
Numpy-discussion mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/numpy-discussion