使用python调用函数库,实现2种频域锐化算法。
时间: 2024-05-10 16:21:36 浏览: 82
以下是两种频域锐化算法的Python实现:
1. 傅里叶变换+高通滤波器
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
def fourier_highpass(img, filter_size=30):
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
rows, cols = img.shape
crow, ccol = rows//2, cols//2
fshift[crow-filter_size:crow+filter_size, ccol-filter_size:ccol+filter_size] = 0
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.real(img_back)
img_back = np.uint8(np.clip(img_back, 0, 255))
return img_back
img = cv2.imread('lena.png', 0)
img_highpass = fourier_highpass(img)
plt.subplot(121), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(122), plt.imshow(img_highpass, cmap='gray'), plt.title('Highpass Filter')
plt.show()
```
2. 巴特沃斯高通滤波器
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
def butterworth_highpass(img, filter_size=30, order=2):
rows, cols = img.shape
crow, ccol = rows//2, cols//2
x = np.linspace(-cols/2, cols/2-1, cols)
y = np.linspace(-rows/2, rows/2-1, rows)
xv, yv = np.meshgrid(x, y)
d = np.sqrt(xv**2 + yv**2)
H = 1 / (1+(d/filter_size)**(2*order))
F = np.fft.fft2(img)
Fshift = np.fft.fftshift(F)
Gshift = H * Fshift
G = np.fft.ifftshift(Gshift)
g = np.fft.ifft2(G)
g = np.real(g)
g = np.uint8(np.clip(g, 0, 255))
return g
img = cv2.imread('lena.png', 0)
img_highpass = butterworth_highpass(img)
plt.subplot(121), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(122), plt.imshow(img_highpass, cmap='gray'), plt.title('Butterworth Highpass Filter')
plt.show()
```
阅读全文