基于小波变换的图像融合(python)

本文通过Python展示了如何利用小波变换进行图像融合,详细介绍了相关代码实现,可以适用于多张图像的融合处理。
摘要由CSDN通过智能技术生成

话不多说,直接上代码

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))

这里我融合是一次性四张图像进行融合,可做适当更改

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二月断痕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值