擅长:python、mysql、java
<p>我强烈建议为此使用<a href="http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/filter_2d/filter_2d.html" rel="nofollow noreferrer">openCV</a>。但是原则上,您几乎可以直接使用<a href="https://en.wikipedia.org/wiki/Kernel_(image_processing)" rel="nofollow noreferrer">wiki-article on kernel convolution</a>上的“伪代码”来创建自己的函数。。。</p>
<pre><code>ks = (kl-1)/2 ## kernels usually square with odd number of rows/columns
kl = len(kernel)
imx = len(matrix)
imy = len(matrix[0])
for i in range(imx):
for j in range(imy):
acc = 0
for ki in range(kl): ##kernel is the matrix to be used
for kj in range(kl):
if 0 <= i-ks <= kl: ## make sure you don't get out of bound error
acc = acc + (matrix[i-ks+ki][j-ks+kj] * kernel[ki][kj])
matrix[i][j] = acc
</code></pre>
<p>原则上,这应该能做到(但我还没有测试过…)</p>
<p>我希望这对你有帮助。</p>