首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ValueError:轴与数组不匹配-无法转置数组

ValueError:轴与数组不匹配-无法转置数组
EN

Stack Overflow用户
提问于 2021-08-10 17:58:45
回答 1查看 341关注 0票数 0

回溯错误

代码语言:javascript
运行
复制
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

回溯错误也在那里,如果这有助于解决这个问题,我需要为作为参数输入到函数中的图像计算新的转置轴。该函数获取图像的形状,然后计算图像的转置轴。将图像数组转置到新轴时出现问题。我想不出它为什么不工作的问题。我是否需要将concat变量转换为元组才能正常工作,或者是其他原因导致了该问题

代码语言:javascript
运行
复制
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) 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-10 21:44:29

axes参数的类型并不重要:

代码语言:javascript
运行
复制
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的维度多,我就会得到错误:

代码语言:javascript
运行
复制
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也是如此。

希望这篇文章能给你一些如何自己调试问题的想法。启动一个交互式会话,并尝试一些替代方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68731560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档