[Python图像处理] 小波变换执行图像融合

图像融合基础

图像融合是将多个输入图像组合到单个输出图像中的过程,输出图像中包含比单个输入图像更好的场景描述。一个好的图像融合方法应当具备以下属性:

  • 它可以保留不同图像上的大多数有用信息
  • 它不会产生干扰或误导图像视觉外观或后续图像处理步骤的伪影
  • 它必须具有鲁棒性
  • 它不应丢弃输入图像中的任何显着信息

使用小波变换执行图像融合

在本节中,我们将学习如何使用 pywt 执行图像融合操作(小波系数会被融合)。

(1) 首先导入所需的 Python 库:

import pywt
import cv2
import numpy as np
import numpy as np
import matplotlib.pylab as plt

(2) 定义函数 fuseCoeff(),根据融合方法定义系数融合:

def 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)
    else:
        cooef = []

    return cooef

(3) 指定融合方法(可以为 minmean 等):

fusion_method = 'mean'

(4) 读取要融合的输入图像。我们将使用同一原始图像的两个不同版本,第一个图像的左边部分模糊,第二个图像的右边部分模糊。我们的目标是将这两个图像中的细节相互融合:

im1 = cv2.imread('4.jpg',0)
im2 = cv2.imread('5.jpg',0)

(5) 我们两个图像的大小相同,因此需要调整图像具有相同的尺寸大小:

im2 = cv2.resize(im2,(im1.shape[1], im1.shape[0]))

图像融合

(1) 在每个输入图像上执行小波变换,计算相应的系数:

wavelet = 'sym2' #'bior1.1' #'haar' #'db1'
cooef1 = pywt.wavedec2(im1[:,:], wavelet)
cooef2 = pywt.wavedec2(im2[:,:], wavelet)

(2) 对于两个图像中的每个级别,根据所需选项执行融合:

fused_cooef = []
for i in range(len(cooef1)):
    if(i == 0):
        fused_cooef.append(fuseCoeff(cooef1[0], cooef2[0], fusion_method))
    else:
        c1 = fuseCoeff(cooef1[i][0], cooef2[i][0],fusion_method)
        c2 = fuseCoeff(cooef1[i][1], cooef2[i][1], fusion_method)
        c3 = fuseCoeff(cooef1[i][2], cooef2[i][2], fusion_method)
        fused_cooef.append((c1,c2,c3))

(3) 融合系数后,我们需要使用 IDWT 传输回去,以获取图像:

#print(len(fused_cooef))
fused_image = pywt.waverec2(fused_cooef, wavelet)

(4) 将输出值规范化为 unit8 数据范围内:

fused_image = 255*fused_image / np.max(fused_image)
fused_image = fused_image.astype(np.uint8)

(5) 最后,绘制原始图像、平均图像和小波融合的图像如下:

plt.figure(figsize=(20,20))
plt.gray()
plt.subplot(221), plt.imshow(im1), plt.axis('off'), plt.title('Image1', size=10)
plt.subplot(222), plt.imshow(im2), plt.axis('off'), plt.title('Image2', size=10)
plt.subplot(223), plt.imshow(im1//2 + im2// 2), plt.axis('off'), plt.title('Average Image', size=10) #cv2.cvtColor(fused_image,cv2.COLOR_BGR2RGB))

plt.subplot(224), plt.imshow(fused_image), plt.axis('off'), plt.title('Fused Image with Wavelets', size=10) #cv2.cvtColor(fused_image,cv2.COLOR_BGR2RGB))
plt.tight_layout()
plt.show()

得到的结果图像如下所示:

图像融合

从以上结果图像可以看出,与平均图像输出相比,使用小波进行图像融合得到的输出图像中的细节更加丰富(使用 PSNR 作为评估标准)。

相关链接

Python图像处理【1】图像与视频处理基础
Python图像处理【2】探索Python图像处理库
Python图像处理【3】Python图像处理库应用
Python图像处理【4】图像线性变换
Python图像处理【5】图像扭曲/逆扭曲
Python图像处理【6】通过哈希查找重复和类似的图像
Python图像处理【7】采样、卷积与离散傅里叶变换
Python图像处理【8】使用低通滤波器模糊图像
Python图像处理【9】使用高通滤波器执行边缘检测
Python图像处理【10】基于离散余弦变换的图像压缩
Python图像处理【11】利用反卷积执行图像去模糊
Python图像处理【12】基于小波变换执行图像去噪

猜你喜欢

转载自blog.csdn.net/qq_30167691/article/details/128435846