简体   繁体   English

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

[英]Convert a list of matrices to only one 2D matrix in Python

I have a list of 3960 matrices, which is simply the SIFT descriptors of 3960 images. 我有一个3960矩阵的列表,它只是3960图像的SIFT描述符。 This is supposed to result in a list of matrices with an unknown number of lines (which of course will depend on the image) and 128 columns (from SIFT descriptors). 据推测,这将导致矩阵列表的行数未知(当然,行数取决于图像)和128列(来自SIFT描述符)。 I am trying to put this list in just one 2D matrix, which the number of lines is the sum of the number of lines of these matrices and 128 columns, however, I am not being able to do that. 我试图将此列表仅放在一个2D矩阵中,行数是这些矩阵和128列的行数的总和,但是,我无法做到这一点。 Here is my code: 这是我的代码:

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))

The sift_keypoints shape is (1,3960) according to this code, which is, of course, not what I want. 根据此代码,sift_keypoints的形状为(1,3960),这当然不是我想要的。 How to transform this list in a 2D numpy array? 如何在二维numpy数组中转换此列表?

EDIT one simple example that illustrates my problem is the one in the following code 编辑一个说明我的问题的简单示例是以下代码中的一个

#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)

Solution using np.row_stack 使用np.row_stack解决方案

Suppose l is your list of Numpy arrays of the shape (n, 128) . 假设l是您的形状为(n, 128)的Numpy数组的列表。

Suppose that m is the total number of lines: the object is to stack all the objects and create a matrix of the shape (m, 128) . 假设m是总行数:对象是堆叠所有对象并创建形状为(m, 128)的矩阵。

We can proceed as follows, using Numpy's row_stack : 我们可以使用Numpy的row_stack进行以下row_stack

result = np.row_stack(l)

Use np.concatenate : 使用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]])                                                                                                  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM