简体   繁体   English

如何像2D一样转置3D矩阵?

[英]How to transpose 3D matrix like 2D?

I want to transpose 3D matrix but without touching most inner list. 我想转置3D矩阵,但不涉及大多数内部列表。 The 3D matrix is numpy array of an image and the goal of transposition is to rotate image but not to change pixel values(rgb). 3D矩阵是图像的numpy数组,转置的目的是旋转图像而不改变像素值(rgb)。

image = np.array(
    [
        [[15, 15, 25], [50, 50, 50], [90, 90, 90], [255, 255, 255], [127, 127, 127]],

        [[121, 121, 121], [75, 10, 75], [187, 187, 187], [0, 0, 0], [210, 50, 100]],

        [[31, 31, 31], [63, 63, 63], [87, 87, 87], [63, 63, 63], [63, 63, 63]],

        [[95, 95, 95], [95, 95, 95], [115, 115, 115], [87, 87, 87], [95, 95, 95]],

        [[95, 95, 95], [63, 63, 63], [63, 63, 63], [87, 87, 87], [100, 250, 250]]
    ], dtype=np.uint8)

The result should be something like this. 结果应该是这样的。

result = np.array(
    [
        [[15, 15, 25], [121, 121, 121], [31, 31, 31], [95, 95, 95], [95, 95, 95]],

        [[50, 50, 50], [75, 10, 75], [63, 63, 63], [95, 95, 95], [63, 63, 63]],

        [[90, 90, 90], [187, 187, 187], [87, 87, 87], [115, 115, 115], [63, 63, 63]],

        [[255, 255, 255], [0, 0, 0], [63, 63, 63], [87, 87, 87], [87, 87, 87]],

        [[127, 127, 127], [210, 50, 100], [63, 63, 63], [95, 95, 95], [100, 250, 250]]
    ], dtype=np.uint8)

看看np.swapaxis

np.swapaxes(image, 0, 1)

I think the answer to this can be found here 我认为可以在这里找到答案

basically use this 基本上用这个

numpy.swapaxes(image,0,1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM