python编写一段程序,读入图像 Fig0464(a)(car_75DPI Moire).tif,进行如下频域滤波。 (1)观察并显示该图像的幅度谱 (2)保存幅度谱,使用软件记录幅度谱中 “类沖激”的坐标位置为x=54.1 y=43.9,x=54.1 y=84.4,x=56.7 y=164.2,x=56.7 y=205.6,x=110.9 y=40.1,x=110.9 y=81.0,x=113.0 y=161.2,x=113.0 y=202.6,使用 notchfunc函数 构造陷波滤波器 (3)陷波滤波器和幅度谱相乘,用plt.imshow显示结果,进行傅立叶反变换用plt.imshow显示结果
时间: 2023-05-21 16:07:30 浏览: 102
以下是Python程序的代码实现:
```python
import numpy as np
import cv2
from matplotlib import pyplot as plt
def notchfunc(d0, coords, shape):
P, Q = shape
mask = np.ones((P,Q))
for coord in coords:
u, v = coord
du = np.array([np.arange(P)]).T
dv = np.array([np.arange(Q)])
dist = np.sqrt((du - u)**2 + (dv - v)**2)
mask = mask * (1 / (1 + ((d0 * d0) / ((dist * dist) - (d0 * d0)))))
return mask
img = cv2.imread('Fig0464(a)(car_75DPI Moire).tif', cv2.IMREAD_GRAYSCALE)
## 1. Observing and displaying the amplitude spectrum of the image
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
mag_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(mag_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
## 2. Saving the amplitude spectrum and noting the coordinates of impulse-like points
np.save('mag_spectrum.npy', mag_spectrum)
impulse_coords = [(54.1, 43.9), (54.1, 84.4), (56.7, 164.2), (56.7, 205.6), \
(110.9, 40.1), (110.9, 81.0), (113.0, 161.2), (113.0, 202.6)]
## 3. Constructing a notch filter using the notchfunc and multiply it with the magnitude spectrum
notch_d0 = 10
mask = notchfunc(notch_d0, impulse_coords, mag_spectrum.shape)
filtered_spectrum = mag_spectrum * mask
plt.subplot(121),plt.imshow(mask, cmap = 'gray')
plt.title('Notch filter'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(filtered_spectrum, cmap = 'gray')
plt.title('Filtered Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
## 4. Inverse Fourier transforming and showing the filtered image
filt_dft_shift = dft_shift * mask
filt_dft_shift = np.fft.ifftshift(filt_dft_shift)
filt_idft = cv2.idft(filt_dft_shift)
filtered_img = cv2.magnitude(filt_idft[:,:,0],filt_idft[:,:,1])
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(filtered_img, cmap = 'gray')
plt.title('Filtered Image'), plt.xticks([]), plt.yticks([])
plt.show()
```
关于使用 `notchfunc` 函数构造陷波滤波器的详细解释可以参考以下链接:
https://www.csdn.net/gather_2e/MtjaAgz0NTc1NTItYmxvZwO0O0OO0O0O.html
关于 `np.save` 和 `np.load` 函数的具体用法可以参考以下链接:
https://numpy.org/doc/stable/reference/generated/numpy.save.html
https://numpy.org/doc/stable/reference/generated/numpy.load.html
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)