利用频率域滤波方法去掉图像“house”中的竖条纹
import cv2
import numpy as np
from matplotlib import pyplot as plt
gif = cv2.VideoCapture('house.gif')
ret, frame = gif.read()
if ret:
print('change success!')
img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
else:
print("change fail!")
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = np.fft.fftshift(dft)
rows, cols = img.shape
mask = np.zeros((rows, cols, 2), np.uint8)
for i in range(rows):
for j in range(cols):
mask[i][j] = 1
m = 18
n = 14
mask[int(rows / 2 - m):int(rows / 2 + m), int(cols / 2 - m):int(cols / 2 + m)] = 0
mask[int(rows / 2 - n):int(rows / 2 + n), int(cols / 2 - n):int(cols / 2 + n)] = 1
fshift = dft_shift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = cv2.idft(f_ishift)
img_back2 = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
plt.subplot(131), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(img_back2, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.subplot(133), plt.imshow(20*np.log(img_back2), cmap='gray')
plt.title('normalize'), plt.xticks([]), plt.yticks([])
plt.show()
|