话不多说,直接上代码
import pywt
import cv2
import numpy as np
import time
# This function does the coefficient fusing according to the fusion method
def fuseCoeff(cooef1, cooef2,cooef3,cooef4, method):
if (method == 'mean'):
cooef = (cooef1 + cooef2 + cooef3 + cooef4) / 4
elif (method == 'min'):
cooef = np.minimum(np.minimum(cooef1, cooef2),np.minimum(cooef3,cooef4))
elif (method == 'max'):
cooef = np.maximum(np.maximum(cooef1, cooef2),np.maximum(cooef3,cooef4))
return cooef
# Params
FUSION_METHOD = 'mean' # Can be 'min' || 'max || anything you choose according theory
FUSION_METHOD1 = 'max'
wavelet = 'db2'
# Read the four image
a = 1
while a <= 500:
start = time.time()
I1 = cv2.imread(r'D:\data-5000\A-500/{}.jpg'.format(a))
I2 = cv2.imread(r'D:\data-5000\B-500/{}.jpg'.format(a))
I3 = cv2.imread(r'D:\data-5000\C-500/{}.jpg'.format(a))
I4 = cv2.imread(r'D:\data-5000\S-GS-500/{}.jpg'.format(a))
# First: Do wavelet transform on each image
cooef1 = pywt.wavedec2(I1[:, :], wavelet, level=1)
cooef2 = pywt.wavedec2(I2[:, :], wavelet, level=1)
cooef3 = pywt.wavedec2(I3[:, :], wavelet, level=1)
cooef4 = pywt.wavedec2(I4[:, :], wavelet, level=1)
# Second: for each level in both image do the fusion according to the desire option
fusedCooef= []
for i in range(len(cooef1)):
# The first values in each decomposition is the apprximation values of the top level
if (i == 0):
fusedCooef.append(fuseCoeff(cooef1[0], cooef2[0], cooef3[0],cooef4[0],FUSION_METHOD))
else:
# For the rest of the levels we have tupels with 3 coeeficents
c1 = fuseCoeff(cooef1[i][0], cooef2[i][0],cooef3[i][0],cooef4[i][0], FUSION_METHOD1)
c2 = fuseCoeff(cooef1[i][1], cooef2[i][1],cooef3[i][1],cooef4[i][1], FUSION_METHOD1)
c3 = fuseCoeff(cooef1[i][2], cooef2[i][2],cooef3[i][2],cooef4[i][2], FUSION_METHOD1)
fusedCooef.append((c1, c2, c3))
# Third: After we fused the cooefficent we ned to transfor back to get the image
fusedImage = pywt.waverec2(fusedCooef, wavelet)
# Forth: normmalize values to be in uint8
fusedImage1 = np.multiply(np.divide(fusedImage - np.min(fusedImage), (np.max(fusedImage) - np.min(fusedImage))), 255)
fusedImage1 = fusedImage1.astype(np.uint8)
# Fith: Show image
cv2.imwrite(r'D:\data-5000\xiaobobianhuan\{}.jpg'.format(a), fusedImage1)
a += 1
over = time.time()
times = over - start
print('花费时间:{}s'.format(times))
这里我融合是一次性四张图像进行融合,可做适当更改