想要创建一个未知维度的空矩阵并附加特征向量



我想将 HoG 特征向量附加到未知维度的空矩阵中。是否需要提前指定矩阵的维度?我已经尝试了一些python代码,但它说所有输入数组必须具有相同的维度。

import matplotlib.pyplot as plt
from skimage.feature import hog
from skimage import data, exposure, img_as_float
from skimage import data
import numpy as np
from scipy import linalg
import cv2
import glob

shape = (16576, 1)
X = np.empty(shape)
print X.shape
hog_image = np.empty(shape)
hog_image_rescaled = np.empty(shape)
for img in glob.glob("/home/madhuri/pythoncode/faces/*.jpg"):
n= cv2.imread(img)
gray = cv2.cvtColor(n, cv2.COLOR_RGB2GRAY)
hog_image = hog(gray, orientations=9, pixels_per_cell=(16, 16),     
cells_per_block=(3, 3), visualise=False)
hog_image_rescaled = exposure.rescale_intensity(hog_image,
in_range=(0,10))  
X = np.append(X, hog_image_rescaled, axis=1)
print 'X is'
print np.shape(X)
X = []    # use an 'empty' list
# hog_image = np.empty(shape)  # no point initializing these variables
# hog_image_rescaled = np.empty(shape)  # you just reassign them in the loop
for img in glob.glob("/home/madhuri/pythoncode/faces/*.jpg"):
n= cv2.imread(img)
gray = cv2.cvtColor(n, cv2.COLOR_RGB2GRAY)
hog_image = hog(gray, orientations=9, pixels_per_cell=(16, 16),     
cells_per_block=(3, 3), visualise=False)
hog_image_rescaled = exposure.rescale_intensity(hog_image,
in_range=(0,10))  
X.append(hog_image_rescaled)

现在X将是重新缩放的图像列表。 这些元素现在可以连接在哪个维度上是合适的:

np.concatenate(X, axis=1)
np.stack(X)
# etc

的列表模型

alist = []
for ....
alist.append(...)

不能很好地转换为数组。np.appendnp.concatenate的掩护,并制作一个新的数组,这比列表附加更昂贵。 为这样的循环定义一个好的起始"空"数组是很棘手的。np.empty不合适:

In [977]: np.empty((2,3))
Out[977]: 
array([[1.48e-323, 1.24e-322, 1.33e-322],
[1.33e-322, 1.38e-322, 1.38e-322]])
In [978]: np.append(_, np.zeros((2,1)), axis=1)
Out[978]: 
array([[1.48e-323, 1.24e-322, 1.33e-322, 0.00e+000],
[1.33e-322, 1.38e-322, 1.38e-322, 0.00e+000]])

最新更新