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


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. 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). 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. 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. How to transform this list in a 2D numpy array?



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)





Can you explain your problem with a simple example, where metrics are of smaller size.
– ZdaR
Jul 3 at 4:17





@ZdaR every loop of the for statement will generate a matrix of shape (x,128) where x is the number of lines that will vary depending on the image and 128 columns are specific of the SIFT descriptor I used. Suppose that the total of lines of different matrices is 39600 with 128 columns. What I want is to vertically concatenate all these matrices in this list to generate a matrix of shape (39600,128). I will try to put a simple example in the question. Thanks.
– mad
Jul 3 at 4:33





2 Answers
2



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


np.row_stack



Suppose l is your list of Numpy arrays of the shape (n, 128).


l


(n, 128)



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:


row_stack


result = np.row_stack(l)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages