【问题标题】:How do i save an arrays in an array in python?如何在python中将数组保存在数组中?
【发布时间】:2022-10-13 15:52:58
【问题描述】:

我想在 python 的 5x5 数组中存储 25 个数组。

目前,我正在尝试使用嵌套的 for 循环将使用 openCV 的图像切成 25 块。 我很难将裁剪的图像存储在 slices 数组中

board = cv.imread("King Domino dataset/Cropped and perspective corrected boards/1.jpg",1)

tileDimW = int(board.shape[0]/5)
tileDimH = int(board.shape[1]/5)

slices = np.array([])
slice = np.array([tileDimH,tileDimW])

for h in range(5):
    for w in range(5):
        slice = board[tileDimH*h:tileDimH*(h+1),tileDimW*w:tileDimW*(w+1)]
        slices[h,w] = slice

我收到错误消息: “IndexError:数组的索引过多:数组是一维的,但有 2 个被索引”在最后一行

【问题讨论】:

  • 不要使用 slice 关键字作为变量名。

标签: python arrays numpy opencv


【解决方案1】:

给定一个数组a,其形状为(25, X, Y, Z, ...),您可以简单地将reshape 转换为(5, 5, X, Y, Z, ...),如下所示:

a.reshape((5, 5) + a.shape[1:])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2011-08-22
    • 2020-08-29
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    相关资源
    最近更新 更多