Image processing 如何使用numpy创建m*n*3阵列
我正在尝试创建一个矩阵mxnx3,它基于笛卡尔基数x,y,z的一些关节。首先,我将第一幅图像中关节的索引排列到二维网格中Image processing 如何使用numpy创建m*n*3阵列,image-processing,numpy-ndarray,Image Processing,Numpy Ndarray,我正在尝试创建一个矩阵mxnx3,它基于笛卡尔基数x,y,z的一些关节。首先,我将第一幅图像中关节的索引排列到二维网格中 我不能做的是沿着矩阵A的第三维方向加上这些指数的笛卡尔坐标(x,y,z),从而得到mxnx3。每个关节的x、y、z将类似于R=x、G=y、B=z的彩色图像的通道R、G、B。此示例可能有助于您解决问题,但如果没有,请以更清晰的格式再次描述您的问题 #Create mxnx3 arrays and tensor form specific A matrix and then le
我不能做的是沿着矩阵A的第三维方向加上这些指数的笛卡尔坐标(x,y,z),从而得到mxnx3。每个关节的x、y、z将类似于R=x、G=y、B=z的彩色图像的通道R、G、B。此示例可能有助于您解决问题,但如果没有,请以更清晰的格式再次描述您的问题
#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)
下面的示例代码从矩阵a生成一个矩阵mxnx3,其中a表示骨架中元素的索引,结果如下:
谢谢你的回答。这不是我所期待的。我已经更新了我的问题。希望我能让你更容易理解谢谢你的帮助。这就是我一直在寻找的
#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)
# 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()