【发布时间】:2017-06-27 09:50:41
【问题描述】:
我正在尝试创建一个高斯模糊矩阵。我正在从http://www.labri.fr/perso/nrougier/teaching/numpy/numpy.html 修改代码
dev_data 具有 784 行像素特征,我想与相关像素周围的邻居以及像素本身进行模糊处理。当我们沿着外边缘(第 1 行,-1 行,第 1 列,-1 列)时,丢弃任何超出边界的邻居。我不太确定如何进行这种丢弃。
代码:
# Initialize a new feature array with the same shape as the original data.
blurred_dev_data = np.zeros(dev_data.shape)
#we will reshape the 784 feature-long rows into 28x28 matrices
for i in range(dev_data.shape[0]):
reshaped_dev_data = np.reshape(dev_data[i], (28,28))
#the purpose of the reshape is to use the average of the 8 pixels + the pixel itself to blur
for idx, pixel in enumerate(reshaped_dev_data):
pixel = np.mean(reshaped_dev_data[idx-1:idx-1,idx-1:idx-1] + reshaped_dev_data[idx-1:idx-1,idx:idx] + reshaped_dev_data[idx-1:idx-1,idx+1:] +
reshaped_dev_data[idx:idx,idx-1:idx-1] + reshaped_dev_data[idx:idx,idx:idx] + reshaped_dev_data[idx:idx,idx+1:] +
reshaped_dev_data[idx+1: ,idx-1:idx-1] + reshaped_dev_data[idx+1: ,idx:idx] + reshaped_dev_data[idx+1: ,idx+1:])
blurred_dev_data[i,:] = reshaped_dev_data.ravel()
我收到一个索引错误:
ValueError: operands could not be broadcast together with shapes (0,0) (0,27)
这不是索引错误,所以我不太确定我在这里做错了什么/如何解决它。
【问题讨论】:
-
将
reshaped_dev_data[idx-1:idx-1,idx-1:idx-1]编辑为reshaped_dev_data[idx-1,idx-1]等等。 -
谢谢。现在我得到了我所期待的越界错误。你知道忽略越界索引的好方法吗?
-
我建议使用高斯模糊滤镜 - docs.scipy.org/doc/scipy-0.14.0/reference/generated/…
标签: python numpy vectorization