gpt4 book ai didi

python - 组合 12 个 1x3 矩阵

转载 作者:太空宇宙 更新时间:2023-11-04 08:34:14 24 4
gpt4 key购买 nike

我是 Python 的新手,我确实尝试使用内置函数(例如附加(到列表))来执行此操作,但我不确定如何去做。

我有 12 个 1x3 矩阵,我想将它们全部组合起来创建一个 12x3 矩阵。

我有一个计算图像中光线方向的函数(返回一个 1x3 数组)

def comp_light(img, mask):
"""
assume the viewer is at (0, 0, 1)
"""

light_direct=[]

#Calculate centroid of gray image
centroid = compute_centroid(img)

#Calculate centroid of mask
mask_centroid = compute_centroid(mask)
mask_radius = compute_radius(mask, mask_centroid)

r = mask_radius
dx = centroid[1]-mask_centroid[1]
dy = centroid[0]-mask_centroid[0]
dy = -dy

N = np.array([dx/r,dy/r,math.sqrt(r*r - dx*dx - dy*dy)/r])

R = np.array([0,0,1])
L = 2*np.dot(N,R)*N-R

light_direct.append(L)


return light_direct

然后我有 12 个图像(灰度和蒙版)的数据,我想用这两个来计算光的方向。

for i in range(12):
light = comp_light(gray['chrome'][i],mask['chrome'])
print(light)

我从这个循环中得到的输出是

[array([0.44179545, 0.44251159, 0.78038469])]
[array([0.03981791, 0.1526196 , 0.98748255])]
[array([-0.04485713, 0.17308602, 0.98388468])]
[array([-0.09494382, 0.43736653, 0.89425734])]
[array([-0.30896139, 0.48596268, 0.81754702])]
[array([-0.09564658, 0.56019186, 0.82282247])]
[array([0.24524103, 0.40670384, 0.88002774])]
[array([0.08200615, 0.4203557 , 0.90364599])]
[array([0.20189239, 0.34563383, 0.91639332])]
[array([0.0855834 , 0.34030184, 0.93641345])]
[array([0.11078827, 0.05101894, 0.99253364])]
[array([-0.1302858 , 0.35921852, 0.92411453])]

如何在 Python 中组合所有这些矩阵?

最佳答案

import numpy as np
x=[[np.array([0.44179545, 0.44251159, 0.78038469])],
[np.array([0.03981791, 0.1526196 , 0.98748255])],
[np.array([-0.04485713, 0.17308602, 0.98388468])],
[np.array([-0.09494382, 0.43736653, 0.89425734])],
[np.array([-0.30896139, 0.48596268, 0.81754702])],
[np.array([-0.09564658, 0.56019186, 0.82282247])],
[np.array([0.24524103, 0.40670384, 0.88002774])],
[np.array([0.08200615, 0.4203557 , 0.90364599])],
[np.array([0.20189239, 0.34563383, 0.91639332])],
[np.array([0.0855834 , 0.34030184, 0.93641345])],
[np.array([0.11078827, 0.05101894, 0.99253364])],
[np.array([-0.1302858 , 0.35921852, 0.92411453])]]
x1=np.concatenate(x,axis=0)
x1.reshape((12,3))
print(x1.shape)

希望对您有所帮助。所有数组都存储在一个列表中。您可以使用 np.concatenate() 进行组合。

关于python - 组合 12 个 1x3 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50386682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com