【问题标题】:How can i create a m * n * 3 arrays using numpy如何使用 numpy 创建 m * n * 3 数组
【发布时间】:2021-04-23 09:52:02
【问题描述】:

我正在尝试基于某些具有基数笛卡尔 x,y,z 的关节创建一个矩阵 m x n x 3。首先,我将第一张图像skeleton image 中的关节索引排列成二维网格2D grid

A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])

我不能做的是沿矩阵 A 的第三维添加这些索引的笛卡尔坐标 (x,y,z) 以获得 m x n x 3。每个关节的 x,y,z 将相似到R=x, g=y, b=z的彩色图像的Chanel R, G, B​​p>

【问题讨论】:

    标签: image-processing numpy-ndarray


    【解决方案1】:

    下面的示例代码从矩阵 A 生成一个矩阵 mxnx3,其中 A 说明了骨架中元素的索引,导致 A_result 如下所示:

    # Create mxnx3 matrix from matrix A, where A include index name of skeleton element in each mxn location
    import numpy as np
    from matplotlib import pyplot as plt
    
    A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
    
    #Let's rearrange slightly...
    A_sub=A[0][:][:]
    
    #Take the size of the matrix...
    (m_max,n_max)=A_sub.shape
    
    #Let's create basis for result matrix...note the format where mxn is first and after them the 3 dimensions of "colors"
    A_result=np.zeros((A.shape[1],A.shape[2],3))
    
    #demonstration function for the xyz coordinates...
    def tell_me_xyz_coordinate_of_element(element_number):
        #...perhaps in real application there is some measurement or the like functionality...
        #...which investigate the element_number and then gives back its location...
        #...but here to exemplify we simple return random int values back...
        x=np.random.randint(0,255)
        y=np.random.randint(0,255)
        z=np.random.randint(0,255)
        return x,y,z
    
    #let's create the result matrix...
    for m in range(m_max):
        for n in range(n_max):
            #Define x,y,z -values of the element in this m,n coordinate,
            #where the value in m,n coordinate tells the number of corresponding element...
            element_number=A_sub[m][n]
            (x,y,z)=tell_me_xyz_coordinate_of_element(element_number)
            #Set the results in the matrix...
            A_result[m][n][0]=x
            A_result[m][n][1]=y
            A_result[m][n][2]=z
    
    #Let's investigate the resulting nympy-matrix as a image...
    #...remember to change the data format to uint8 to be able to investigate as a image
    
    plt.imshow(np.uint8(A_result),interpolation='nearest')
    title_text=''.join(["Result matrix mxnx3, \nwhere m=",str(m_max+1), " n=",str(n_max+1),",\n" "with color codes 0-255 in each m and n"])
    plt.title(title_text)
    plt.show()
    

    【讨论】:

    • 感谢您的帮助。这就是我一直在寻找的
    【解决方案2】:

    此示例可能会帮助您解决问题,但如果不能,请以更清晰的格式再次描述您的问题。

    #Create mxnx3 arrays and tensor form specific A matrix and then let's show how to create mxnx3 np-array and the same also in tensor format
    import numpy as np
    import tensorflow as tf
    
    A = np.array([[[4, 3, 21, 2, 1, 13, 14, 15, 16], [4, 3, 21, 2, 1, 17, 18, 19, 20], [4, 3, 21, 9, 10, 11, 12, 24, 25], [4, 3, 21, 5,6, 7, 8, 22, 23]]])
    
    #Let's convert A to a tensor:
    
    A_in_tensorformat=tf.Variable(A)
    
    #Let's make a numpy of size mxnx3:
    m=123
    n=45
    
    B=np.ones((m,n,3))
    
    B_in_tensorformat=tf.Variable(B)
    

    【讨论】:

    • 感谢您的回答。这不是我一直在等待的。我已经更新了我的问题。希望我让它更容易理解
    猜你喜欢
    • 1970-01-01
    • 2020-03-28
    • 2020-01-14
    • 2021-12-23
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2013-11-30
    • 2019-04-30
    相关资源
    最近更新 更多