赞
踩
利用频率域滤波方法去掉图像“house”中的竖条纹
import cv2 import numpy as np from matplotlib import pyplot as plt # 将gif图片转换成可识别图像 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 # create a mask first, center square is 1, remaining all zeros 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 # apply mask and inverse DFT fshift = dft_shift * mask f_ishift = np.fft.ifftshift(fshift) # fftshit()函数的逆函数,它将频谱图像的中心低频部分移动至左上角 img_back = cv2.idft(f_ishift) # 将频率域转化回空间域,输出是一个复数,cv2.idft()返回的是一个双通道图像 img_back2 = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]) # idft[:,:,0]求得实部,用idft[:,:,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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。