傅里叶变换:
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('smart car.jpeg',0)
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) # 傅里叶变换(零频率分量不在中心位置)
result1 = 20 * np.log(cv2.magnitude(dft[:,:,0], dft[:,:,1]))
dftShift = np.fft.fftshift(dft) # 傅里叶变换(零频率分量在中心位置)
result2 = 20 * np.log(cv2.magnitude(dftShift[:,:,0], dftShift[:,:,1]))
plt.figure(figsize=(10,4),dpi=120)
plt.subplot(131),plt.imshow(img, cmap = 'gray')
plt.title('img'),plt.axis('off')
plt.subplot(132),plt.imshow(result1, cmap = 'gray')
plt.title('dft'), plt.axis('off')
plt.subplot(133),plt.imshow(result2, cmap = 'gray')
plt.title('dftShift'), plt.axis('off')
plt.show()
傅里叶逆变换:
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('smart car.jpeg',0)
dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT) # 傅里叶变换(零频率分量不在中心位置)
dftShift = np.fft.fftshift(dft) # 傅里叶变换(零频率分量在中心位置)
idftShift = np.fft.ifftshift(dftShift) # 傅里叶逆变换(零频率分量在中心位置)
result1 = cv2.magnitude(idftShift[:,:,0], idftShift[:,:,1])
idft = cv2.idft(idftShift) # 傅里叶逆变换(零频率分量不在中心位置)
result2 = cv2.magnitude(idft[:,:,0], idft[:,:,1])
plt.figure(figsize=(10,4),dpi=120)
plt.subplot(131),plt.imshow(img, cmap = 'gray')
plt.title('img'),plt.axis('off')
plt.subplot(132),plt.imshow(result1, cmap = 'gray')
plt.title('idftShift'), plt.axis('off')
plt.subplot(133),plt.imshow(result2, cmap = 'gray')
plt.title('idft'), plt.axis('off')
plt.show()
低通滤波器:
import numpy as np
import cv2
import matplotlib.pyplot as plt
img1 = cv2.imread('smart car.jpeg',0)
dft = cv2.dft(np.float32(img1),flags = cv2.DFT_COMPLEX_OUTPUT) # 傅里叶变换(零频率分量不在中心位置)
dftShift = np.fft.fftshift(dft) # 傅里叶变换(零频率分量在中心位置)
rows, cols = img1.shape
row_center, col_center = int(rows/2) , int(cols/2) # 图像中心
# 理想低通滤波器
mask = np.zeros((rows, cols, 2), np.float32)
mask[row_center-60:row_center+60, col_center-60:col_center+60] = 1
img3 = cv2.magnitude(mask[:,:,0], mask[:,:,1])
convolution = dftShift * mask # 卷积
idftshift = np.fft.ifftshift(convolution)
idft = cv2.idft(idftshift)
img2 = cv2.magnitude(idft[:,:,0], idft[:,:,1])
plt.figure(figsize=(10,4),dpi=120)
plt.subplot(131),plt.imshow(img1, cmap = 'gray')
plt.title('original'),plt.axis('off')
plt.subplot(132),plt.imshow(img3, cmap = 'gray')
plt.title('mask'), plt.axis('off')
plt.subplot(133),plt.imshow(img2, cmap = 'gray')
plt.title('ILPF'), plt.axis('off')
plt.show()
高通滤波器:
import numpy as np
import cv2
import matplotlib.pyplot as plt
img1 = cv2.imread('smart car.jpeg',0)
dft = cv2.dft(np.float32(img1),flags = cv2.DFT_COMPLEX_OUTPUT) # 傅里叶变换(零频率分量不在中心位置)
dftShift = np.fft.fftshift(dft) # 傅里叶变换(零频率分量在中心位置)
rows, cols = img1.shape
row_center, col_center = int(rows/2) , int(cols/2) # 图像中心
# 理想高通滤波器
mask = np.ones((rows, cols, 2), np.float32)
mask[row_center-60:row_center+60, col_center-60:col_center+60] = 0
img3 = cv2.magnitude(mask[:,:,0], mask[:,:,1])
convolution = dftShift * mask # 卷积
idftshift = np.fft.ifftshift(convolution)
idft = cv2.idft(idftshift)
img2 = cv2.magnitude(idft[:,:,0], idft[:,:,1])
plt.figure(figsize=(10,4),dpi=120)
plt.subplot(131),plt.imshow(img1, cmap = 'gray')
plt.title('original'),plt.axis('off')
plt.subplot(132),plt.imshow(img3, cmap = 'gray')
plt.title('mask'), plt.axis('off')
plt.subplot(133),plt.imshow(img2, cmap = 'gray')
plt.title('IHPF'), plt.axis('off')
plt.show()
本文代码已开源,欢迎大家进行二次开发:
https://gitee.com/xiaolong_ROS/Graphics-Processing-and-Machine-Vision
如有错误或者不足之处,欢迎大家留言指正!