gpt4 book ai didi

python - 值错误 : axes don't match array - Can't transpose an array

转载 作者:行者123 更新时间:2023-12-05 02:40:08 25 4
gpt4 key购买 nike

回溯错误

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

Traceback Error 也存在,如果这有助于解决问题我需要为作为参数输入到函数中的图像计算新的转置轴。该函数获取图像的形状,然后计算图像的转置轴。问题发生在将图像阵列转置到新轴时。我无法弄清楚为什么它不起作用的问题。我是否需要将 concat 变量转换为元组才能正常工作,或者是其他原因导致了它的问题

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)

最佳答案

axes 参数的类型无关紧要:

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)

但是如果我提供的值多于 arr 的维度,我会得到你的错误:

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

我希望 [0,1,2] 和 2d arr 也一样。

希望这能让您了解如何自己调试问题。启动交互式 session 并尝试一些替代方案。

关于python - 值错误 : axes don't match array - Can't transpose an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68731560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com