pip install PyWavelets
python版本代码:
import pywtimport cv2import numpy as npimport mathfrom scipy import signaldef pcnn(img, link=5, beta=0.1, iteration=200): """ PCNN generate fire maps image :param img:source image :param link:pcnn link parm :param beta:pcnn link coefficient :param iteration:pcnn iteration number :return:fire maps image """ m, n = np.shape(img)[0], np.shape(img)[1] alpha_L = 1 alpha_Theta = 0.2 vL = 1.0 vTheta = 20 center_x = round(link / 2) center_y = round(link / 2) W = np.zeros((link, link)) for i in range(link): for j in range(link): if i == center_x and j == center_y: W[i, j] = 0 else: W[i, j] = 1. / math.sqrt(pow(i - center_x, 2) + pow(j - center_y, 2)) imgf = img.astype(np.float) F = abs(imgf) L = np.zeros((m, n)) Y = np.zeros((m, n)) Theta = np.zeros((m, n)) img_pcnn = np.zeros((m, n)) for i in range(iteration): K = signal.convolve2d(Y, W, mode='same') L = math.exp(-alpha_L) * L + vL * K Theta = math.exp(-alpha_Theta) * Theta + vTheta * Y U = np.multiply(F, 1 + np.multiply(beta, L)) Y = U > Theta Yf = Y.astype(np.float) img_pcnn = img_pcnn + Yf return img_pcnn# This function does the coefficient fusing according to the fusion methoddef fuseCoeff(cooef1, cooef2, method): if (method == 'mean'): cooef = (cooef1 + cooef2) / 2 elif (method == 'min'): cooef = np.minimum(cooef1, cooef2) elif (method == 'max'): cooef = np.maximum(cooef1, cooef2) elif (method == 'pcnn'): pcnn1 = pcnn(cooef1, 5, 0.1, 200) pcnn2 = pcnn(cooef2, 5, 0.1, 200) pcnn_cooef = np.maximum(pcnn1, pcnn2) cooef1[pcnn1 != pcnn_cooef] = 0 cooef2[pcnn2 != pcnn_cooef] = 0 cooef = cooef1 + cooef2 return cooef# ParamsFUSION_METHOD = 'mean' # Can be 'min' || 'max || anything you choose according theoryFUSION_METHOD1 = 'pcnn'# Read the two imageI1 = cv2.imread('IR1.png', 0)I2 = cv2.imread('VIS1.png', 0)# First: Do wavelet transform on each imagewavelet = 'db2'cooef1 = pywt.wavedec2(I1[:, :], wavelet, level=1)cooef2 = pywt.wavedec2(I2[:, :], wavelet, level=1)# Second: for each level in both image do the fusion according to the desire optionfusedCooef = []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], FUSION_METHOD)) else: # For the rest of the levels we have tupels with 3 coeeficents c1 = fuseCoeff(cooef1[i][0], cooef2[i][0], FUSION_METHOD1) c2 = fuseCoeff(cooef1[i][1], cooef2[i][1], FUSION_METHOD1) c3 = fuseCoeff(cooef1[i][2], cooef2[i][2], FUSION_METHOD1) fusedCooef.append((c1, c2, c3))# Third: After we fused the cooefficent we nned to transfor back to get the imagefusedImage = pywt.waverec2(fusedCooef, wavelet)# Forth: normmalize values to be in uint8fusedImage1 = np.multiply(np.divide(fusedImage - np.min(fusedImage), (np.max(fusedImage) - np.min(fusedImage))), 255)fusedImage1 = fusedImage1.astype(np.uint8)# Fith: Show imagecv2.imwrite("win.bmp", fusedImage1)
4、融合结果 下图是红外图像和可见光图像。 小波变换脉冲耦合神经网络融合结果 与小波变换和最大值融合策略融合结果相比,PCNN融合方法在图像细节上保留的更好。
如果碰到任何问题,随时留言,我会尽量去回答的。