拉普拉斯算子
长这样:
[ − 1 2 − 1 − 1 2 − 1 − 1 2 − 1 ] \begin{bmatrix} -1 & 2 &-1 \\ -1 & 2 &-1 \\-1 & 2 &-1 \end{bmatrix} ⎣⎡−1−1−1222−1−1−1⎦⎤
性质
Image = np.array([[0, 0, 0, 0, 0, 0],
[0, 1, 5, 9, 7, 0],
[0, 1, 5, 9, 7, 0],
[0, 1, 5, 9, 7, 0],
[0, 1, 5, 9, 7, 0],
[0, 0, 0, 0, 0, 0]])
Kernel = [[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]]
ax1 = plt.subplot(1,2,1)
plt.imshow(Image)
plt.title('Image')
ax1 = plt.subplot(1,2,2)
plt.imshow(Kernel)
plt.title('Kernel')
def convolution(Image, Kernel):
count = 0
for i in range(0, 3):
for j in range(0, 3):
count += Image[2-i][2-j]*Kernel[i][j]
return count
def correlation(Image, Kernel):
count = 0
for i in range(0, 3):
for j in range(0, 3):
count += Image[i][j]*Kernel[i][j]
return count
result1 = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(4):
for j in range(4):
result1[i][j] = convolution(Image[i:i+3, j:j+3], Kernel)
result2 = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
for i in range(4):
for j in range(4):
result2[i][j] = correlation(Image[i:i+3, j:j+3], Kernel)
_, ax = plt.subplots(1, 2, figsize = (10, 10))
ax[0].imshow(result1)
ax[0].set_title('Convolution Result')
ax[1].imshow(result2)
ax[1].set_title('Correlation Result')
从结果看出,用拉普拉斯算子对图像做卷积和关联操作,其结果是一样的,因为拉普拉斯算子本身旋转 180° 的结果还是它本身。