清晰地平铺以平面1D形式存储的图像的numpy数组

2024-09-29 19:22:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Numpy从一个.csv文件加载了一堆16x16的图像。每一行是存储在CMO中的256个灰度值的列表(因此形状是(n256),其中n是图像的数量)。这意味着我可以用pyplot显示任何单独的图像:

plot.imshow(np.reshape(images[index], (16,16), order='F'), cmap=cm.Greys_r)

我想用每行一定数量的图像平铺这些图像。我有一个可行的解决方案:

^{pr2}$

这样做非常好,但感觉应该有一个更干净的方法来做这种事情。我对Numpy有点新手,我想知道是否有一种更干净的方法来平铺扁平化的数据,而不需要所有的手动填充和条件连接。在

通常,这些简单的数组整形操作可以用Numpy在几行代码中完成,所以我觉得我遗漏了一些东西。(另外,使用“”作为标志,就好像它是一个空指针似乎有点混乱)


Tags: 文件csv方法图像numpy列表数量plot
1条回答
网友
1楼 · 发布于 2024-09-29 19:22:09

这是您的实现的简化版本。在

想不出更简单的方法。在

def TileImage(imgs, picturesPerRow=16):
    """ Convert to a true list of 16x16 images
    """

    # Calculate how many columns
    picturesPerColumn = imgs.shape[0]/picturesPerRow + 1*((imgs.shape[0]%picturesPerRow)!=0)

    # Padding
    rowPadding = picturesPerRow - imgs.shape[0]%picturesPerRow
    imgs = vstack([imgs,zeros([rowPadding,imgs.shape[1]])])

    # Reshaping all images
    imgs = imgs.reshape(imgs.shape[0],16,16)

    # Tiling Loop (The conditionals are not necessary anymore)
    tiled = []
    for i in range(0,picturesPerColumn*picturesPerRow,picturesPerRow):
        tiled.append(hstack(imgs[i:i+picturesPerRow,:,:]))


    return vstack(tiled)

希望有帮助。在

相关问题 更多 >

    热门问题