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

2024-10-01 02:37:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我加载MNIST(test)数据集,其形状为(10000,28,28,1)(表示10000个图像(灰度28x28图像))。我想对每个图像应用运动模糊内核,得到同样形状的输出(10000,28,28,1)。 我试过使用def,vectorize,但是没有达到我的预期效果。你知道吗

它运行在python3.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)中。 谢谢。你知道吗


Tags: 数据test图像defplt数组array内核