importimport numpy asimport SimpleITK as
# This function does the coefficient fusing according to the fusion methoddef fuseCoeff(cooef1, cooef2, method):if (method == 'mean'):2elif (method == 'min'): cooef = np.minimum(cooef1, cooef2)elif (method == 'max'): cooef = np.maximum(cooef1, cooef2)return
# ParamsFUSION_METHOD1 = 'mean' # Can be 'min' || 'max || anything you choose according theoryFUSION_METHOD2 = 'max'# Read the two imageI2_itk = sitk.ReadImage("Brats18_2013_1_1_flair.nii.gz", sitk.sitkInt16)I1_itk = sitk.ReadImage("Brats18_2013_1_1_t1ce.nii.gz", sitk.sitkInt16)I1 = sitk.GetArrayFromImage(I1_itk)I2 = sitk.GetArrayFromImage(I2_itk)# First: Do wavelet transform on each imagewavelet = 'db2'"""haar family: haardb family: db1, db2, db3, db4, db5, db6, db7, db8, db9, db10, db11, db12, db13, db14, db15, db16, db17, db18, db19, db20, db21, db22, db23, db24, db25, db26, db27, db28, db29, db30, db31, db32, db33, db34, db35, db36, db37, db38sym family: sym2, sym3, sym4, sym5, sym6, sym7, sym8, sym9, sym10, sym11, sym12, sym13, sym14, sym15, sym16, sym17, sym18, sym19, sym20coif family: coif1, coif2, coif3, coif4, coif5, coif6, coif7, coif8, coif9, coif10, coif11, coif12, coif13, coif14, coif15, coif16, coif17bior family: bior1.1, bior1.3, bior1.5, bior2.2, bior2.4, bior2.6, bior2.8, bior3.1, bior3.3, bior3.5, bior3.7, bior3.9, bior4.4, bior5.5, bior6.8rbio family: rbio1.1, rbio1.3, rbio1.5, rbio2.2, rbio2.4, rbio2.6, rbio2.8, rbio3.1, rbio3.3, rbio3.5, rbio3.7, rbio3.9, rbio4.4, rbio5.5, rbio6.8dmey family: dmeygaus family: gaus1, gaus2, gaus3, gaus4, gaus5, gaus6, gaus7, gaus8mexh family: mexhmorl family: morlcgau family: cgau1, cgau2, cgau3, cgau4, cgau5, cgau6, cgau7, cgau8shan family: shanfbsp family: fbspcmor family: cmor"""cooef1 = pywt.wavedecn(I1[:, :], wavelet)cooef2 = pywt.wavedecn(I2[:, :], wavelet)
# Second: for each level in both image do the fusion according to the desire optionfusedCooef = []for i in# The first values in each decomposition is the apprximation values of the top levelif i == 0:0], cooef2[0], FUSION_METHOD1))else:'aad'], cooef2[i]['aad'], FUSION_METHOD2)'ada'], cooef2[i]['ada'], FUSION_METHOD2)'add'], cooef2[i]['add'], FUSION_METHOD2)'daa'], cooef2[i]['daa'], FUSION_METHOD2)'dad'], cooef2[i]['dad'], FUSION_METHOD2)'dda'], cooef2[i]['dda'], FUSION_METHOD2)'ddd'], cooef2[i]['ddd'], FUSION_METHOD2)'aad': c1, 'ada': c2, 'add': c3, 'daa': c4, 'dad': c5, 'dda': c6, 'ddd': c7} fusedCooef.append(dictobj)# Third: After we fused the cooefficent we nned to transfor back to get the imagefusedImage = pywt.waverecn(fusedCooef, wavelet)fusedImage = fusedImage.astype(np.int)fused_itk = sitk.GetImageFromArray(fusedImage)fused_itk.SetOrigin(I2_itk.GetOrigin())fused_itk.SetSpacing(I2_itk.GetSpacing())fused_itk.SetDirection(I2_itk.GetDirection())sitk.WriteImage(fused_itk, 'fused_itk.mha')