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
?
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.
Broadcasting is the name given to the method that NumPy uses to allow array arithmetic between arrays with a different shape or size.
Can an array store different data types? Yes, a numpy array can store different data String, Integer, Complex, Float, Boolean.
np.repeat(mask.reshape(2560L, 1920L, 1L), 3, axis=2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With