Issue
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)
Solution
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.
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.