我当时正在观看Andrew Ng's videos on CNN,并想用6 x 6
滤镜对3 x 3
图像进行卷积。我用numpy进行处理的方法如下:
image = np.ones((6,6))
filter = np.ones((3,3))
convolved = np.convolve(image, filter)
运行此命令会提示错误:
ValueError: object too deep for desired array
我可以从numpy documentation of convolve那里了解如何正确使用convolve
方法。
还有,我可以用numpy进行大步卷积吗?
答案 0 :(得分:1)
rows = result.fetchall()
for row in rows:
print row
函数仅适用于 1-D 卷积。这就是为什么您会得到一个错误;您需要一个可以执行二维卷积的功能。
但是,即使它确实起作用,您实际上也会执行错误的操作。机器学习中所谓的 convolution 在数学中更恰当地称为 cross-correlation 。它们实际上几乎是相同的。卷积涉及翻转滤波器矩阵,然后执行互相关。
要解决您的问题,您可以查看np.convolve
(也不要使用scipy.signal.correlate
作为名称,因为您将隐藏内置函数):
filter
输出:
from scipy.signal import correlate
image = np.ones((6, 6))
f = np.ones((3, 3))
correlate(image, f)
这是完全互相关的标准设置。如果要删除将rely on the zero-padding的元素,请传递array([[1., 2., 3., 3., 3., 3., 2., 1.],
[2., 4., 6., 6., 6., 6., 4., 2.],
[3., 6., 9., 9., 9., 9., 6., 3.],
[3., 6., 9., 9., 9., 9., 6., 3.],
[3., 6., 9., 9., 9., 9., 6., 3.],
[3., 6., 9., 9., 9., 9., 6., 3.],
[2., 4., 6., 6., 6., 6., 4., 2.],
[1., 2., 3., 3., 3., 3., 2., 1.]])
:
mode='valid'
输出:
from scipy.signal import correlate
image = np.ones((6, 6))
f = np.ones((3, 3))
correlate(image, f, mode='valid')