Issue
I was wondering if there is a way to reverse a 3d numpy array? Like in 1d case we can go from [1 2 3] to [3 2 1]. Is there something similar for 3d?
Thanks
Solution
Do you mean reverse it along an particular axis?
For example, if we have a color image of the size height x width x 3
where the last axis is red, green, blue (RGB) pixels, and we want to convert it to blue, green, red (BGR):
image_bgr = image[:, :, ::-1]
If you'd prefer, you can even write that as:
image_bgr = image[..., ::-1]
As a more complete example, consider this:
import numpy as np
# Some data...
x = np.arange(3 * 4 * 5).reshape(5, 4, 3)
print x[0,0,:]
# Reverse the last axis
y = x[:, :, ::-1]
print y[0,0,:]
In general, though, you can use this to reverse any particular axis. For example, you could just as easily have done:
y = x[:, ::-1, :]
To reverse the second axis.
Answered By - Joe Kington
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.