KevinJ-Huang / FECNet

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

交换幅值分量的可视化代码 #12

Open wbjfefhuefbhefa opened 4 months ago

wbjfefhuefbhefa commented 4 months ago

你好,请问您能提供论文中关于表1的交换幅值分量的可视化代码吗,我自己尝试之后,并不能达到这么好的效果。万分感谢

KevinJ-Huang commented 4 months ago

你好,我明天找一下发到这里,理论上来说效果应该还是有的

KevinJ-Huang commented 4 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 fft_exchange(clear, hazy): w, h = clear.shape hazy = cv2.resize(hazy,(h,w),interpolation=cv2.INTER_CUBIC)

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)

fft_hazy = np.fft.fft2(hazy)
fft_hazy = np.fft.fftshift(fft_hazy)
mag_hazy = np.abs(fft_hazy)#必须取log,因为最大值包含着太大的能量了,导致直接归一化,其它数值为0
pha_hazy = np.angle(fft_hazy)

a = mag_clear * np.cos(pha_hazy)
b = mag_clear * np.sin(pha_hazy)
c = mag_hazy * np.cos(pha_clear)
d = mag_hazy * np.sin(pha_clear)

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)

real_res1 = mag_hazy * np.cos(pha_clear)
imag_res1 = mag_hazy * np.sin(pha_clear)
y1 = torch.complex(torch.from_numpy(c), torch.from_numpy(d))
y1 = y1.numpy()
y1 = np.fft.ifft2(np.fft.ifftshift(y1))
y1 = np.abs(y1)

return y, y1

def fft_exchange3d(clear, hazy): out = [] out1 = [] for d in range(clear.shape[2]): clear1 = clear[:, :, d] hazy1 = hazy[:, :, d] y, y1= fft_exchange(clear1, hazy1) out.append(y) out1.append(y1) out = np.dstack(out) out1 = np.dstack(out1) out = out.astype(np.uint8) out1 = out1.astype(np.uint8) return out, out1

if name == "main": path = '/media/jieh/My Passport/swap/Rain' files = os.listdir(path)

if not os.path.exists():

#     os.makedirs()
#     os.makedirs()
for i,file in enumerate(files):
    clear_path = os.path.join(path,file)
    img_clear = cv2.imread(clear_path, -1)
    img_clear = cv2.cvtColor(img_clear, cv2.COLOR_BGR2RGB)

    files2 = os.listdir(os.path.join(path.replace('Rain','Clean')))

    for j, file2 in enumerate(files2):
        hazy_path = os.path.join(path.replace('Rain','Clean'),files2[j])
        img_hazy = cv2.imread(hazy_path, -1)
        img_hazy = cv2.cvtColor(img_hazy, cv2.COLOR_BGR2RGB)

        y, y1 = fft_exchange3d(img_clear, img_hazy)

        plt.imsave(os.path.join('/media/jieh/My Passport/swap/RandomRainClean/',file[:2] + '-' + file2[:2] + '.jpg'), y)
        plt.imsave(os.path.join('/media/jieh/My Passport/swap/RandomCleanRain/',file[:2] + '-' + file2[:2] + '.jpg'), y1)
KevinJ-Huang commented 4 months ago

排版有点乱,将就看一下行不行

wbjfefhuefbhefa commented 4 months ago

感谢大佬的分享!!!