KevinJ-Huang / FECNet

ECCV 2022 (Official implementation of "Deep Fourier-based Exposure Correction Network with Spatial-Frequency Interaction")
65 stars 3 forks source link

关于振幅分量和相位分量的可视化 #3

Open tangyongst opened 9 months ago

tangyongst commented 9 months ago

作者你好,我看到文章中的图2对振幅和相位分别进行了可视化,请问有代码可以借我参考吗?最近有可视化振幅和相位的需要。非常感谢!

huangkevinj commented 9 months ago

感谢你的关注,最近远程电脑关闭了,修复好之后我将相关代码放在这里

huangkevinj commented 9 months ago

` import torch import torch.nn as nn import cv2 from mpl_toolkits.mplot3d import Axes3D import numpy as np import os import scipy.misc import PIL.Image import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D

def fft_plot3d(img_bgr, img_bgr1): img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_RGB2GRAY) fft = np.fft.fft2(img_gray) fft_shift = np.fft.fftshift(fft) abs_fft = np.log(np.abs(fft_shift))#必须取log,因为最大值包含着太大的能量了,导致直接归一化,其它数值为0 pha_fft = np.abs(np.angle(fft_shift)) abs_fft = cv2.normalize(abs_fft, 0, 255, norm_type=cv2.NORM_MINMAX).astype(np.uint8) img_gray1 = cv2.cvtColor(img_bgr1, cv2.COLOR_RGB2GRAY) fft1 = np.fft.fft2(img_gray1) fft_shift1 = np.fft.fftshift(fft1) abs_fft1 = np.log(np.abs(fft_shift1))#必须取log,因为最大值包含着太大的能量了,导致直接归一化,其它数值为0 pha_fft1 = np.abs(np.angle(fft_shift1)) abs_fft1 = cv2.normalize(abs_fft1, 0, 255, norm_type=cv2.NORM_MINMAX).astype(np.uint8) return abs_fft, pha_fft, abs_fft1, pha_fft1 ##### 以上为展示幅度和相位本身的代码

def fft2imagshow(clear): w,h = clear.shape fft_clear = np.fft.fft2(clear) fft_clear = np.fft.fftshift(fft_clear) mag_clear = np.abs(fft_clear)#必须取log,因为最大值包含着太大的能量了,导致直接归一化,其它数值为0

pha_clear = np.angle(fft_clear)

pha_clear = np.ones((w,h))*1000  # 必须设置为常数,且较大,提供一个初始大小的值,展示相位图像是同理的
a = mag_clear * np.cos(pha_hazy)
b = mag_clear * np.sin(pha_hazy)
y = torch.complex(torch.from_numpy(a), torch.from_numpy(b))
y = y.numpy()
y = np.fft.ifft2(np.fft.ifftshift(y))
y = np.abs(y)
return y   ######## 展示只将幅度图像显示的代码

def fft2imagshowC(clear): out = [] for d in range(clear.shape[2]): clear1 = clear[:, :, d] y= fft2imagshow(clear1) out.append(y) out = np.dstack(out) out = out.astype(np.uint8) return out

if name == "main": path = '/home/jieh/Dataset/Continous/Exposure/valid/input/Exp1' files = os.listdir(path)

if not os.path.exists():

#     os.makedirs()
#     os.makedirs()
for file in files:
    clear_path = os.path.join(path,file)
    img_clear = cv2.imread(clear_path, -1)
    img_clear = cv2.cvtColor(img_clear, cv2.COLOR_BGR2RGB)
    y = fft2imagshowC(img_clear)
    y = np.uint8((y-np.min(y)+5)/(np.max(y)-np.min(y)+5)*255)
    plt.imsave(os.path.join('/home/jieh/Projects/Tool/FreqExtrac/AmpExp1/',file), y)

`

这里只展示显示幅度谱的代码,相位谱的代码同理操作即可

tangyongst commented 9 months ago

谢谢大佬!

发件人:Resistence @.> 发送日期:2023-12-29 20:25:04 收件人:KevinJ-Huang/FECNet @.> 抄送人:tangyong @.>,Author @.> 主题:Re: [KevinJ-Huang/FECNet] 关于振幅分量和相位分量的可视化 (Issue #3)

`# 图像的傅里叶变换与反变换 import torch import torch.nn as nn import cv2 from mpl_toolkits.mplot3d import Axes3D import numpy as np import os import scipy.misc import PIL.Image import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def fft_plot3d(img_bgr, img_bgr1): img_gray = cv2.cvtColor(img_bgr, cv2.COLOR_RGB2GRAY) fft = np.fft.fft2(img_gray) fft_shift = np.fft.fftshift(fft) abs_fft = np.log(np.abs(fft_shift))#必须取log,因为最大值包含着太大的能量了,导致直接归一化,其它数值为0 pha_fft = np.abs(np.angle(fft_shift)) abs_fft = cv2.normalize(abs_fft, 0, 255, norm_type=cv2.NORM_MINMAX).astype(np.uint8)

img_gray1 = cv2.cvtColor(img_bgr1, cv2.COLOR_RGB2GRAY) fft1 = np.fft.fft2(img_gray1) fft_shift1 = np.fft.fftshift(fft1)

abs_fft1 = np.log(np.abs(fft_shift1))#必须取log,因为最大值包含着太大的能量了,导致直接归一化,其它数值为0 pha_fft1 = np.abs(np.angle(fft_shift1)) abs_fft1 = cv2.normalize(abs_fft1, 0, 255, norm_type=cv2.NORM_MINMAX).astype(np.uint8)

return abs_fft, pha_fft, abs_fft1, pha_fft1 以上为展示幅度和相位本身的代码def fft2imagshow(clear): w,h = clear.shape fft_clear = np.fft.fft2(clear) fft_clear = np.fft.fftshift(fft_clear) mag_clear = np.abs(fft_clear)#必须取log,因为最大值包含着太大的能量了,导致直接归一化,其它数值为0

pha_clear = np.angle(fft_clear)

pha_clear = np.ones((w,h))*1000 # 必须设置为常数,且较大,提供一个初始大小的值,展示相位图像是同理的

a = mag_clear np.cos(pha_hazy) b = mag_clear np.sin(pha_hazy)

y = torch.complex(torch.from_numpy(a), torch.from_numpy(b)) y = y.numpy() y = np.fft.ifft2(np.fft.ifftshift(y)) y = np.abs(y)

return y ######## 展示只将幅度图像显示的代码 def fft2imagshowC(clear): out = [] for d in range(clear.shape[2]): clear1 = clear[:, :, d] y= fft2imagshow(clear1) out.append(y) out = np.dstack(out) out = out.astype(np.uint8) return out if name == "main": path = '/home/jieh/Dataset/Continous/Exposure/valid/input/Exp1' files = os.listdir(path)

if not os.path.exists():

os.makedirs()

os.makedirs()

for file in files: clear_path = os.path.join(path,file) img_clear = cv2.imread(clear_path, -1) img_clear = cv2.cvtColor(img_clear, cv2.COLOR_BGR2RGB) y = fft2imagshowC(img_clear)

y = np.uint8((y-np.min(y)+5)/(np.max(y)-np.min(y)+5)*255)

plt.imsave(os.path.join('/home/jieh/Projects/Tool/FreqExtrac/AmpExp1/',file), y)

` 这里只展示显示幅度谱的代码,相位谱的代码同理操作即可 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>