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