【问题标题】:Transpose image dimensions转置图像尺寸
【发布时间】:2020-09-27 02:42:37
【问题描述】:

喂!有没有解决方案可以在 Python 中将图像的(宽度、高度、通道)尺寸更改为(通道、高度、宽度)?

例如: 224 x 224 x 3 -> 3 x 224 x 224

【问题讨论】:

    标签: python image dimensions


    【解决方案1】:

    假设图像表示为nd.array,您可以使用moveaxis 方法,如下所示:

    x = np.zeros((3, 4, 5))
    np.moveaxis(x, 0, -1).shape
    # (4, 5, 3)
    np.moveaxis(x, -1, 0).shape
    # (5, 3, 4)
    

    在你的具体情况下:

    x = np.zeros((224, 224, 3))
    np.moveaxis(x, (2, 0, 1), (0, 1, 2)).shape
    # (3, 224, 224)
    

    您可以在以下链接中了解该方法:

    https://numpy.org/doc/stable/reference/generated/numpy.moveaxis.html

    【讨论】:

    • 我实际上用 OpenCV 加载了一个图像,我尝试了两个 moveaxis,但没有任何成功:?
    • @Fellon 你能添加你对问题的尝试吗?
    【解决方案2】:

    你可以使用np.transposehttps://numpy.org/doc/1.18/reference/generated/numpy.transpose.html:

    new_image = np.transpose(image, (2, 0, 1))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多