1、傅里叶变换
#!/usr/bin/env python3
# encod=utf8
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# cap = cv.VideoCapture('./images/stripe.gif')
#
# ret, test_img = cap.read()
# test_img = cv.cvtColor(test_img, cv.COLOR_BGR2GRAY)
test_img = np.zeros((128, 128), np.uint8)
# test_img = cv.line(test_img,(0,0), (128,128), 255, 3, cv.LINE_AA)
test_img = cv.line(test_img, (0, 128 // 2), (128 // 2, 0), 255, 3, cv.LINE_AA)
# 函数返回的np.ndarray的形状将是(rows,cols,2)
# 第一个通道代表结果的真实部分。
# 结果的虚部的第二个通道。
dft = cv.dft(test_img.astype(np.float32), flags=cv.DFT_COMPLEX_OUTPUT + cv.DFT_SCALE)
# 应用反向移位,以便DC组件再次位于左上角
# array([ 0., 1., 2., 3., 4., -5., -4., -3., -2., -1.])
# >>> np.fft.fftshift(freqs)
# array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.])
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20 * np.log(cv.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
plt.subplot(121), plt.imshow(test_img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
2、傅里叶变换
#!/usr/bin/env python3
# encod=utf8
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img_path = './images/Fig4.11(a).jpg'
# img_path = './images/test.jpg'
img = cv.imread(img_path)
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
h, w = img_gray.shape
# 返回最佳DFT大小——高度
h_ = cv.getOptimalDFTSize(h)
# 返回最佳DFT大小——宽度
w_ = cv.getOptimalDFTSize(w)
# 给图像添加边框
# 如top=1,bottom=1,left=1,right=1表示需要1个像素宽的边框。
img_resize = cv.copyMakeBorder(img_gray, 0, h_ - h, w_ - w, 0, borderType=cv.BORDER_CONSTANT, value=0)
# 函数返回的np.ndarray的形状将是(rows,cols,2)
# 第一个通道代表结果的真实部分。
# 结果的虚部的第二个通道。
dft = cv.dft(img_resize.astype(np.float32), flags=cv.DFT_COMPLEX_OUTPUT + cv.DFT_SCALE)
# 应用反向移位,以便DC组件再次位于左上角
# array([ 0., 1., 2., 3., 4., -5., -4., -3., -2., -1.])
# >>> np.fft.fftshift(freqs)
# array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.])
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = 20 * np.log(cv.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(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
3、反傅里叶变换
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img_path = './images/Fig4.11(a).jpg'
# img_path ='./images/Fig5.08(b).jpg'
img = cv.imread(img_path, 0)
img_float32 = img.astype(np.float32)
dft = cv.dft(img_float32, flags=cv.DFT_COMPLEX_OUTPUT + cv.DFT_SCALE)
dft_shift = np.fft.fftshift(dft)
rows, cols = img.shape
crow, ccol = rows // 2, cols // 2 # center
# 首先创建一个蒙版,中心正方形为255,其余为0。
mask = np.zeros((rows, cols, 2), np.uint8)
mask[crow - 50:crow + 50, ccol - 50:ccol + 50] = 255
fshift = dft_shift * mask
f_ishift = np.fft.ifftshift(fshift)
magnitude_spectrum = 20 * np.log(cv.magnitude(fshift[:, :, 0], fshift[:, :, 1]))
img_back = cv.idft(f_ishift)
img_back = cv.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(133), plt.imshow(img_back, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
4、反傅里叶变换
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img_path = './images/Fig4.11(a).jpg'
# img_path ='./images/Fig5.08(b).jpg'
img = cv.imread(img_path, 0)
img_float32 = img.astype(np.float32)
dft = cv.dft(img_float32, flags=cv.DFT_COMPLEX_OUTPUT + cv.DFT_SCALE)
dft_shift = np.fft.fftshift(dft)
rows, cols = img.shape
crow, ccol = rows // 2, cols // 2 # center
# 首先创建一个蒙版,中心正方形为0,其余为255。
mask = np.ones((rows, cols, 2), np.uint8)
mask[crow - 50:crow + 50, ccol - 50:ccol + 50] = 0
fshift = dft_shift * mask
magnitude_spectrum = 20 * np.log(cv.magnitude(fshift[:, :, 0], fshift[:, :, 1]))
f_ishift = np.fft.ifftshift(fshift)
img_back = cv.idft(f_ishift)
img_back = cv.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(133), plt.imshow(img_back, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()