ValueError: axes don't match array - Can't transpose an array
Asked Answered
P

1

8

Traceback Error

Traceback (most recent call last):
File "C:\Users\trial2\trial.py", line 55, in <module>
image_stack(image)
File "C:\Users\trial2\trial.py", line 41, in image_stack
transposed_axes = np.transpose(img, axes=concat)
File "<__array_function__ internals>", line 5, in transpose
File "C:\Users\trial2\venv\lib\site-packages\numpy\core\fromnumeric.py", line 660, in transpose
return _wrapfunc(a, 'transpose', axes)
File "C:\Users\trial2\venv\lib\site-packages\numpy\core\fromnumeric.py", line 57, in _wrapfunc
return bound(*args, **kwds)
ValueError: axes don't match array

The Traceback Error is also there, if that helps to solve the issue I need to calculate the new transpose axes for the image that has been inputted into the function as parameter. The function gets the shape of the image, and then calculates the transpose axes for the image. The problem is occurring in transposing the image array to the new axes. I can't figure out the issue to why it isn't working. Do I need to turn the concat variable into a tuple for it to work properly or is something else causing the issue for it

def image_stack(image):
    img_shape = cv2.imread(image).shape
    img = np.asarray(img_shape)
    print(type(img))
    n = len(img)
    val_1 = list(range(1, n - 1, 2))
    val_2 = list(range(0, n - 1, 2))
    print(img)
    if n % 2 == 0:
        y_ax = val_1
        x_ax = val_2
        axes = (y_ax, x_ax, [n-1])
    else:
        y_ax = val_2
        x_ax = val_1
        axes = (y_ax, x_ax, [n - 1])
    print(type(axes))
    concat = np.concatenate(axes)
    print(concat)
    if type(axes) == tuple:
        transposed_axes = np.transpose(img, axes=concat)
        print(transposed_axes)

image = 'C:\\trial_images\\9.jpg'
image_stack(image) 
Prismatic answered 10/8, 2021 at 17:58 Comment(2)
What's the img.shape and concat value. For a 2d array, axes an only be [0,1] (no change) or [1,0]` (default).. For 3d it has to be some permutation of [0,1,2]. If your concat` isn't something like that, I suggest rereading transpose docs.Adebayo
@Adebayo the img.shape was [720, 1280, 3 ] and for concat the values where [0,1, 2] by the type was numpy.ndarray. Should I change it to a tuple, or are the values incorrect as you said [0,1,2] are a permutation for 3dPrismatic
A
6

The type of the axes argument doesn't matter:

In [96]: arr = np.ones( [720, 1280, 3 ] )
In [97]: np.transpose(arr,[0,1,2]).shape
Out[97]: (720, 1280, 3)
In [98]: np.transpose(arr,(0,1,2)).shape
Out[98]: (720, 1280, 3)
In [99]: np.transpose(arr,np.array([0,1,2])).shape
Out[99]: (720, 1280, 3)

but if I provide more values than dimensions of arr I get your error:

In [103]: np.transpose(arr,[0,1,2,3]).shape
Traceback (most recent call last):
  File "<ipython-input-103-fc01ec77b59e>", line 1, in <module>
    np.transpose(arr,[0,1,2,3]).shape
  File "<__array_function__ internals>", line 5, in transpose
  File "/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py", line 660, in transpose
    return _wrapfunc(a, 'transpose', axes)
  File "/usr/local/lib/python3.8/dist-packages/numpy/core/fromnumeric.py", line 57, in _wrapfunc
    return bound(*args, **kwds)
ValueError: axes don't match array

I expect the same for [0,1,2] and a 2d arr.

Hopefully this gives you ideas of how to debug problems yourself. Fireup an interactive session and try some alternatives.

Adebayo answered 10/8, 2021 at 21:44 Comment(1)
Got the same error when provided fewer values than number of array dimensions.Hetman

© 2022 - 2024 — McMap. All rights reserved.