gpt4 book ai didi

python - 在Python中将矩阵列表转换为仅一个2D矩阵

转载 作者:行者123 更新时间:2023-12-02 16:45:21 24 4
gpt4 key购买 nike

我有一个3960矩阵的列表,它只是3960图像的SIFT描述符。据推测,这将导致出现一个矩阵列表,其中包含未知行数(当然,这将取决于图像)和128列(来自SIFT描述符)。我试图将此列表仅放在一个2D矩阵中,行数是这些矩阵和128列的行数之和,但是,我无法做到这一点。这是我的代码:

sift_keypoints = []

#read images from a text file
with open(file_images) as f:
images_names = f.readlines()
images_names = [a.strip() for a in images_names]

for line in images_names:
print(line)
#read image
image = cv2.imread(line,1)
#Convert to grayscale
image =cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#SIFT extraction
sift = cv2.xfeatures2d.SIFT_create()
kp, descriptors = sift.detectAndCompute(image,None)
#appending sift keypoints to a list
sift_keypoints.append(descriptors)

#here is what I've tried
sift_keypoints = np.asmatrix(np.asarray(sift_keypoints))

根据此代码,sift_keypoints的形状为(1,3960),这当然不是我想要的。如何在二维numpy数组中转换此列表?

编辑一个简单的例子来说明我的问题,是以下代码中的一个
#how to convert this list to a matrix with shape (412,128)?
import numpy as np
x=np.zeros((256,128))
y=np.zeros((156,128))
list=[]
list.append(x)
list.append(y)

最佳答案

使用np.concatenate:

>>> from pprint import pprint
>>> import numpy as np
>>>
>>> a = [np.full((2, 3), i) for i in range(3)]
>>> pprint(a)
[array([[0, 0, 0],
[0, 0, 0]]),
array([[1, 1, 1],
[1, 1, 1]]),
array([[2, 2, 2],
[2, 2, 2]])]
>>> np.concatenate(a, axis=0)
array([[0, 0, 0],
[0, 0, 0],
[1, 1, 1],
[1, 1, 1],
[2, 2, 2],
[2, 2, 2]])

关于python - 在Python中将矩阵列表转换为仅一个2D矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51146573/

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