我有7张大小为29*29的图像,我想为所有7张图像添加一个齐次坐标(增加特征,x0=1),但我不确定如何操作。
我的原始图像维度是
images.shape #(7, 29, 29)
我尝试过的方法是使用np.ones()进行组合,但结果是为第一个特征创建了一个单独的数组,导致维度变为7*2
np.array([list(a) for a in zip(np.ones([7,1]),images_all[:,:])]).shape#(7,2)##[[array([1.]),# array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], ....
如您所见,它将1添加为一个单独的数组,并未将其作为第一个元素附加。
此外,我还尝试通过循环遍历图像并在第一个元素处插入1,但这使得维度变为30并导致错误
for i in range(len(images)): images[i][0] = np.insert(images[i][0], 0, 1., axis=0)
ValueError: could not broadcast input array from shape (30) into shape (29)
回答:
首先创建一个较大的全1数组,调整原始数组的形状,并更新较大的数组。
padded_images = np.ones((7,29*29+1))padded_images[:,1:] = images.reshape(7,29*29)