将函数应用于4d数组的每个维度,并在python中返回4d数组

时间:2019-07-31 13:48:29

标签: python numpy scipy

我加载了形状为(10000,28,28,1)的MNIST(测试)数据集(表示10000张图像(灰度28x28图像))。我想在每个图像上应用运动模糊内核,并获得同样形状的输出(10000、28、28、1)。 我尝试使用def,vectorize,但无法按预期工作。

它在python 3.6上运行

x_test.shape
--> (numpy.ndarray) (10000, 28, 28, 1)


def blurize(x):
    # kernel
    k = np.array([[0,0,0,0,0,0,0.0013],
    [0,0,0,0.0086,0.0574,0.1061,0.1165],
    [0,0.0450,0.0938,0.1426,0.0938,0.0450,0],
    [0.1165,0.1061,0.0574,0.0086,0,0,0],
    [0.0013,0,0,0,0,0,0]])
    return (ndimage.convolve(x.reshape(28,28), k, mode='constant', cval=0.0))

blurred = blurize(x_test)
plt.imshow(blurred[1], interpolation='none', cmap='gray')
plt.show()

结果:

ValueError: cannot reshape array of size 7840000 into shape (28,28)

如果我尝试过 blurred = blurize(x_test[1]).有效,但仅适用于第二张图像。由于我不想通过x_test [i]遍历整个数组并将帧再次合并为预期的输出数组(10000,28,28,1)。 谢谢。

1 个答案:

答案 0 :(得分:1)

您可以squeeze输入数组,广播内核,然后调整输出形状以匹配初始尺寸:

ndimage.convolve(x.squeeze(), k[None, ...], mode='constant', cval=0.0)[..., None]