擅长:python、mysql、java
<p>也许这不是最优化的解决方案,但这是我以前在numpy library for Python中使用的一个实现:</p>
<pre><code>def convolution2d(image, kernel, bias):
m, n = kernel.shape
if (m == n):
y, x = image.shape
y = y - m + 1
x = x - m + 1
new_image = np.zeros((y,x))
for i in range(y):
for j in range(x):
new_image[i][j] = np.sum(image[i:i+m, j:j+m]*kernel) + bias
return new_image
</code></pre>
<p>我希望这个代码能帮助其他有同样疑问的人。</p>
<p>问候。</p>