最近在学习变化检测,但不同时相的影像变换较大 在昇腾杯找到了解决方法。
但在网上找不到对三通道进行傅里叶变换的代码,便自己找了个单通道变换的修改了一下,现在分享出来。
import cv2
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
import matplotlib.cm as c
# 输出路径
outpath = r"C:\Users\CY\Desktop\Figure_11.png"
# 1 读取图像
img = cv.imread("C:/Users/CY/Desktop/xin/sc/41.tif", 1)
img1 = cv.imread("C:/Users/CY/Desktop/xin/sc/31.tif", 1)
rows, cols = img[:, :, 0].shape
img1 = cv.resize(img1, (int(cols), int(rows)))
# 分割通道
B, G, R = cv.split(img)
B1, G1, R1 = cv.split(img1)
# 2 设计高通滤波器(傅里叶变换结果中有两个通道,所以高通滤波中也有两个通道)
rows1, cols1 = R.shape
# 高频滤波器
mask = np.ones((rows, cols, 2), np.uint8)
i = int(rows1 / 1000)
c =int(cols1 / 1000)
mask[int(rows / 2) - i:int(rows / 2) + i, int(cols / 2) - c:int(cols / 2) + c, :] = 0
# 低频滤波器
mask1 = np.zeros((rows1, cols1, 2), np.uint8)
mask1[int(rows1 / 2) - i:int(rows1 / 2) + i, int(cols1 / 2) - c:int(cols1 / 2) + c, :] = 1
# 3 傅里叶变换
# 3.1 正变换
dftB1 = cv.dft(np.float32(B1), flags=cv.DFT_COMPLEX_OUTPUT)
dftB = cv.dft(np.float32(B), flags=cv.DFT_COMPLEX_OUTPUT)
dftG1 = cv.dft(np.float32(G1), flags=cv.DFT_COMPLEX_OUTPUT)
dftG = cv.dft(np.float32(G), flags=cv.DFT_COMPLEX_OUTPUT)
dftR1 = cv.dft(np.float32(R1), flags=cv.DFT_COMPLEX_OUTPUT)
dftR = cv.dft(np.float32(R), flags=cv.DFT_COMPLEX_OUTPUT)
# 3.2 频谱中心化
dftB1 = np.fft.fftshift(dftB1)
dftB = np.fft.fftshift(dftB)
dftG1 = np.fft.fftshift(dftG1)
dftG = np.fft.fftshift(dftG)
dftR1 = np.fft.fftshift(dftR1)
dftR = np.fft.fftshift(dftR)
# 3.3 滤波
dft_shiftB = dftB * mask
dft_shiftG = dftG * mask
dft_shiftR = dftR * mask
dft_shiftB1 = dftB1 * mask1
dft_shiftG1 = dftG1 * mask1
dft_shiftR1 = dftR1 * mask1
# 频谱合并
dft_shiftB2 = dft_shiftB + dft_shiftB1
dft_shiftG2 = dft_shiftG + dft_shiftG1
dft_shiftR2 = dft_shiftR + dft_shiftR1
# 3.4 频谱去中心化
dft_shiftB2 = np.fft.ifftshift(dft_shiftB2)
dft_shiftG2 = np.fft.ifftshift(dft_shiftG2)
dft_shiftR2 = np.fft.ifftshift(dft_shiftR2)
# 3 傅里叶逆变换
# 3.1 反变换
img_backB3 = cv.idft(dft_shiftB2)
img_backG3 = cv.idft(dft_shiftG2)
img_backR3 = cv.idft(dft_shiftR2)
# 3.2 计算灰度值
img_backB3 = cv.magnitude(img_backB3[:, :, 0], img_backB3[:, :, 1])
img_backG3 = cv.magnitude(img_backG3[:, :, 0], img_backG3[:, :, 1])
img_backR3 = cv.magnitude(img_backR3[:, :, 0], img_backR3[:, :, 1])
# 3.3 归一化
cv.normalize(img_backB3, img_backB3, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
cv.normalize(img_backG3, img_backG3, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
cv.normalize(img_backR3, img_backR3, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(img_backB3)
print(min_val)
print(max_val)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(img_backG3)
print(min_val)
print(max_val)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(img_backR3)
print(min_val)
print(max_val)
# 4.1合并3个通道
img = cv.merge((img_backB3, img_backG3, img_backR3))
# plt.imsave(r"C:\Users\CY\Desktop\Figure_11.png", img)
# 4.2保存文件
cv.imwrite(outpath, img * 255)
# 展示
img = cv.merge((img_backR3, img_backG3, img_backB3))
plt.imshow(img)
plt.show()
# plt.title('Original drawing'), plt.xticks([]), plt.yticks([])
# plt.subplot(122), plt.imshow(img_backB3, cmap='gray')
# plt.title('High pass filtering results'), plt.xticks([]), plt.yticks([])