python定义一个卷积函数
时间: 2023-11-17 10:02:53 浏览: 85
卷积python代码
5星 · 资源好评率100%
好的,下面是 Python 定义一个卷积函数的代码:
```python
import numpy as np
def convolve(image, kernel):
height, width = image.shape
k_height, k_width = kernel.shape
h_pad = k_height // 2
w_pad = k_width // 2
padded_image = np.pad(image, ((h_pad,h_pad),(w_pad,w_pad)), 'constant')
output = np.zeros((height, width))
for i in range(height):
for j in range(width):
roi = padded_image[i:i+k_height, j:j+k_width]
output[i,j] = np.sum(roi * kernel)
return output
```
请问您还有其他问题吗?
阅读全文