返回结果 = cv2.dft(原始图像, 转换标识)
np.float32(img)
flags = cv2.DFT_COMPLEX_OUTPUT
,输出一个复数阵列返回值 = cv2.magnitude(参数1, 参数2)
numpy.fft.ifft2
numpy.fft.ifftshift
iimg = np.abs(逆傅里叶变换结果)
返回结果 = cv2.idft(原始数据)
返回值 = cv2.magnitude(参数1, 参数2)
numpy.fft.ifftshift
低频、高频
滤波
频域滤波
方法
高通滤波器 去掉低频
rows, cols = img.shape
crow, ccol = int(rows/2), int(cols/2)
fshift[crow-30:crow+30, ccol-30:ccol+30] = 0
低通滤波器 去掉高频
rows, cols = img.shape
crow,ccol = int(row/2), int(cols/2)
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("/home/forrest/图片/lena.jpg", 0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
result = 20*np.log(np.abs(fshift))
plt.subplot(121)
plt.imshow(img, cmap = 'gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(result, cmap = 'gray')
plt.title('result')
plt.axis('off')
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("/home/forrest/图片/lena.jpg", 0)
dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)
result = 20*np.log(cv2.magnitude(dftShift[:,:,0], dftShift[:,:,1]))
plt.subplot(121)
plt.imshow(img, cmap = 'gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(result, cmap = 'gray')
plt.title('result')
plt.axis('off')
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("/home/forrest/图片/lena.jpg", 0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
ishift = np.fft.ifftshift(fshift)
iimg = np.fft.ifft2(ishift)
iimg = np.abs(iimg)
plt.subplot(121)
plt.imshow(img, cmap = 'gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(iimg, cmap = 'gray')
plt.title('result')
plt.axis('off')
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("/home/forrest/图片/lena.jpg", 0)
dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)
ishift = np.fft.ifftshift(dftShift)
iimg = cv2.idft(ishift)
iimg = cv2.magnitude(iimg[:,:,0], iimg[:,:,1])
plt.subplot(121)
plt.imshow(img, cmap = 'gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(iimg, cmap = 'gray')
plt.title('result')
plt.axis('off')
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("/home/forrest/图片/lena.jpg", 0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
rows,cols = img.shape
crow,ccol = int(rows/2), int(cols/2)
fshift[crow-30:crow+30, ccol-30:ccol+30] = 0
ishift = np.fft.ifftshift(fshift)
iimg = np.fft.ifft2(ishift)
iimg = np.abs(iimg)
plt.subplot(121)
plt.imshow(img, cmap = 'gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(iimg, cmap = 'gray')
plt.title('result')
plt.axis('off')
plt.show()
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread("/home/forrest/图片/lena.jpg", 0)
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
dftShift = np.fft.fftshift(dft)
rows,cols = img.shape
crow,ccol = int(rows/2), int(cols/2)
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
fshift = dftShift * mask
ishift = np.fft.ifftshift(fshift)
iimg = cv2.idft(ishift)
iimg = cv2.magnitude(iimg[:,:,0], iimg[:,:,1])
plt.subplot(121)
plt.imshow(img, cmap = 'gray')
plt.title('original')
plt.axis('off')
plt.subplot(122)
plt.imshow(iimg, cmap = 'gray')
plt.title('result')
plt.axis('off')
plt.show()