I cannot find anything in the documentation but it was used in some starter code for a class I am taking at school. Upon testing per below, it seems to reverse the order of the dimensions of an numpy array.
pic = np.ones((3,4,5))
print(pic.shape,"\n", pic)
#new_shape is the reverse of pic.shape
new_shape = pic.shape[::-1]
print(new_shape)
This outputs:
(3, 4, 5)
[[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]]
(5, 4, 3)
In the starter code it was used in this way:
res_img = cv2.resize(img, sub_img.shape[::-1])
There are some other tricks that are planted in the starter code (to show us that they are there I'm guessing). Here is another example:
channels = blue, green, red = np.moveaxis(color_img, 2, 0) #move channels to pos 0
The np.moveaxis()
moved channels to the first dim position but it's assigned to blue, green and red at the same time via Python multiple assignment? When I looked up multiple assignment, I only saw stuff like var1, var2 = 1, 2
. Also np.moveaxis()
returns an array with the new shape so I have to stretch my imagination to assume the first axis can be assigned to variables. Is this correct?
My instructor just replied that np.split()
does the splitting but the documentation does not say anything about this being implicitly called when np.moveaxis()
is called.
Does anyone know what is really happening?