题目

In frequency, a Laplacian filter can be defined by H(u,v) = -(u2 + v2). In this project one is required to implement this filter in the Fourier frequency domain.
A python proto-type code is provided, and one should implement the function called FreqLaplacian, which accepts an numpy array as input, and returns filtered result as another numpy array. Note: The most difficult part of this project is how to represent the filtered result.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import numpy as np
from PIL import Image
import sys
import matplotlib.pyplot as plt
import scipy.signal as signal

def ReadImage(file_path):
img = Image.open(file_path).convert('L') # 转换为灰度图像
return np.array(img)


def FreqLaplacian(f):
# 创建一个拉普拉斯滤波器
laplacian_kernel = np.array([[0, 1, 0],
[1, -4, 1],
[0, 1, 0]])

# 使用 scipy.signal.convolve2d 进行卷积操作
g = signal.convolve2d(f, laplacian_kernel, boundary='symm', mode='same')

return g


if __name__ == '__main__':
# 读取图像
f = ReadImage('./aLenna.bmp')

# 应用拉普拉斯滤波器
g = FreqLaplacian(f)

# 显示结果
plt.imshow(g, cmap='gray')
plt.title('Laplacian Filtered Image')
plt.show()