Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast one channel in Numpy array into three channels

I have a RGB image img which is of shape (2560L, 1920L, 3L) and another single channel image mask which is of shape (2560L, 1920L). Now, I want to make this mask of shape (2560L, 1920L, 3L) i.e. I want to copy this single channel data into all the three channels.

I'm doing it as follows.

np.array([[[j,j,j] for j in i] for i in mask])

Is there a faster way of doing this using numpy?

like image 929
Abdul Fatir Avatar asked Jun 09 '16 10:06

Abdul Fatir


People also ask

What is broadcasting in NumPy array?

The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.

Does NumPy have broadcasting?

Broadcasting is the name given to the method that NumPy uses to allow array arithmetic between arrays with a different shape or size.

Can NumPy array have multiple types?

Can an array store different data types? Yes, a numpy array can store different data String, Integer, Complex, Float, Boolean.


1 Answers

np.repeat(mask.reshape(2560L, 1920L, 1L), 3, axis=2)
like image 90
Valéry Avatar answered Oct 05 '22 08:10

Valéry