组合12个1x3矩阵

时间:2018-05-17 08:25:10

标签: python numpy

我是Python的新手,我确实尝试使用内置函数(例如append(到列表)来执行此操作但我不确定如何解决此问题。

我有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中组合所有这些矩阵?

4 个答案:

答案 0 :(得分:2)

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()来组合。

答案 1 :(得分:1)

您可以使用np.array将矢量连接到矩阵中。

import numpy as np
a = np.ones(3)
b = np.ones(3) * 2
c = np.ones(3) * 3
matrix = np.array([a, b, c])
print(repr(matrix))

输出:

array([[1., 1., 1.],
      [2., 2., 2.],
      [3., 3., 3.]])

答案 2 :(得分:1)

尝试在循环之前创建一个新数组:

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

这样你应该将所有数组放入新的光阵列

答案 3 :(得分:1)

这就是你要找的东西:

np.concatenate(comp_light(gray['chrome'][i],mask['chrome']) for i in range(12))
#[[ 0.44179545  0.44251159  0.78038469]
# [ 0.03981791  0.1526196   0.98748255]
# [-0.04485713  0.17308602  0.98388468]
# [-0.09494382  0.43736653  0.89425734]
# [-0.30896139  0.48596268  0.81754702]
# [-0.09564658  0.56019186  0.82282247]
# [ 0.24524103  0.40670384  0.88002774]
# [ 0.08200615  0.4203557   0.90364599]
# [ 0.20189239  0.34563383  0.91639332]
# [ 0.0855834   0.34030184  0.93641345]]