本文主要是介绍傅里叶变换和逆变换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import cv2
import numpy as np
import matplotlib.pyplot as plt
import torch# 降低图像亮度image_path = "eval15/gt/1.png"
original_image = cv2.imread(image_path)original_image_tensor = torch.from_numpy(original_image.transpose(2, 0, 1)).float()x = torch.fft.rfft2(original_image_tensor, norm='backward')
mag = torch.abs(x)
pha = torch.angle(x)real = mag * torch.cos(pha)
imag = mag * torch.sin(pha)
x_out = torch.complex(real, imag)# 显示原始图像
# plt.subplot(1, 2, 1)
# plt.imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
# plt.title('Original Image')# 显示逆傅立叶变换后的图像
a = original_image_tensor.shape[1]
b = original_image_tensor.shape[2]
inverse_image = torch.fft.irfft2(x_out, s=(original_image_tensor.shape[1], original_image_tensor.shape[2]), norm='backward')# plt.subplot(1, 2, 2)
# plt.imshow(cv2.cvtColor(inverse_image.numpy().transpose(1, 2, 0).astype(np.uint8), cv2.COLOR_BGR2RGB))
# plt.title('Inverse Fourier Transform Image')plt.show()
# 分别保存图像
cv2.imwrite('original_image.png', original_image)
cv2.imwrite('inverse_image.png', inverse_image.numpy().transpose(1, 2, 0).astype(np.uint8))# cv2.imwrite('original_image.png', cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
# cv2.imwrite('inverse_image.png', cv2.cvtColor(inverse_image.numpy().transpose(1, 2, 0).astype(np.uint8), cv2.COLOR_BGR2RGB))# 计算像素差值平方和
diff_squared_sum = np.sum((original_image - inverse_image.numpy().transpose(1, 2, 0))**2)
print(f"Pixel Difference Squared Sum: {diff_squared_sum}")
这篇关于傅里叶变换和逆变换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!