概述

图像融合是一种常见的图像处理技术,通过将两张或多张图像按一定比例混合,生成一张新的图像。本文将介绍如何使用 OpenCV 实现图像融合,具体包括读取图像、调整图像大小、图像融合以及保存和显示结果图像。我们将通过一个具体的示例来展示这一过程。

准备工作

在开始之前,确保你已经安装了 OpenCV 库。如果没有安装,可以使用以下命令进行安装:

pip install opencv-python
  • 1.

此外,准备好两张图像文件 a.jpgb.jpg,分别作为背景图像和前景图像。

图片a:

基于 OpenCV 的图像融合_OpenCV

图片b:

基于 OpenCV 的图像融合_背景图_02

代码思路和步骤
  1. 读取图像:使用 cv2.imread 函数读取背景图像和前景图像。
  2. 调整图像大小:使用 cv2.resize 函数将两幅图像调整到相同的尺寸,以便进行融合。
  3. 图像融合:使用 cv2.addWeighted 函数将两幅图像按指定的比例进行加权融合。
  4. 保存和显示结果图像:使用 cv2.imwrite 函数保存融合后的图像,并使用 cv2.imshow 函数显示结果图像。
详细代码及注释
import cv2

# 读取图像
bg = cv2.imread('a.jpg', cv2.IMREAD_COLOR)  # 读取背景图像
fg = cv2.imread('b.jpg', cv2.IMREAD_COLOR)  # 读取前景图像

# 检查图像是否成功读取
if bg is None or fg is None:
    print("Error: Image(s) not found!")
    exit()

# 调整图像大小
dim = (691, 393)  # 设定目标尺寸
resized_bg = cv2.resize(bg, dim, interpolation=cv2.INTER_AREA)  # 调整背景图像大小
resized_fg = cv2.resize(fg, dim, interpolation=cv2.INTER_AREA)  # 调整前景图像大小

# 图像融合
blend = cv2.addWeighted(resized_bg, 0.5, resized_fg, 0.8, 0)  # 融合图像,背景图像权重为0.5,前景图像权重为0.8

# 保存融合后的图像
cv2.imwrite('blend.jpg', blend)  # 将融合后的图像保存为 blend.jpg

# 显示融合后的图像
cv2.imshow('blend', blend)  # 在窗口中显示融合后的图像
cv2.waitKey(0)  # 等待用户按键
cv2.destroyAllWindows()  # 关闭所有 OpenCV 窗口
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
代码详解
1. 导入 OpenCV 库
import cv2
  • 1.
  • import cv2:导入 OpenCV 库,用于图像处理和显示。
2. 读取图像
bg = cv2.imread('a.jpg', cv2.IMREAD_COLOR)  # 读取背景图像
fg = cv2.imread('b.jpg', cv2.IMREAD_COLOR)  # 读取前景图像
  • 1.
  • 2.
  • cv2.imread('a.jpg', cv2.IMREAD_COLOR):读取背景图像 a.jpg,并以彩色模式加载。
  • cv2.imread('b.jpg', cv2.IMREAD_COLOR):读取前景图像 b.jpg,并以彩色模式加载。
3. 检查图像是否成功读取
if bg is None or fg is None:
    print("Error: Image(s) not found!")
    exit()
  • 1.
  • 2.
  • 3.
  • if bg is None or fg is None:检查图像是否成功读取,如果任一图像未找到,则输出错误信息并退出程序。
4. 调整图像大小
dim = (691, 393)  # 设定目标尺寸
resized_bg = cv2.resize(bg, dim, interpolation=cv2.INTER_AREA)  # 调整背景图像大小
resized_fg = cv2.resize(fg, dim, interpolation=cv2.INTER_AREA)  # 调整前景图像大小
  • 1.
  • 2.
  • 3.
  • dim = (691, 393):设定目标尺寸为 691x393。
  • cv2.resize(bg, dim, interpolation=cv2.INTER_AREA):将背景图像 bg 调整到目标尺寸 dim,插值方法为 INTER_AREA
  • cv2.resize(fg, dim, interpolation=cv2.INTER_AREA):将前景图像 fg 调整到目标尺寸 dim,插值方法为 INTER_AREA
5. 图像融合
blend = cv2.addWeighted(resized_bg, 0.5, resized_fg, 0.8, 0)  # 融合图像,背景图像权重为0.5,前景图像权重为0.8
  • 1.
  • cv2.addWeighted(resized_bg, 0.5, resized_fg, 0.8, 0):将调整后的背景图像 resized_bg 和前景图像 resized_fg 进行加权融合,背景图像的权重为 0.5,前景图像的权重为 0.8,偏移量为 0。
6. 保存和显示结果图像
cv2.imwrite('blend.jpg', blend)  # 将融合后的图像保存为 blend.jpg
cv2.imshow('blend', blend)  # 在窗口中显示融合后的图像
cv2.waitKey(0)  # 等待用户按键
cv2.destroyAllWindows()  # 关闭所有 OpenCV 窗口
  • 1.
  • 2.
  • 3.
  • 4.
  • cv2.imwrite('blend.jpg', blend):将融合后的图像 blend 保存为 blend.jpg
  • cv2.imshow('blend', blend):在窗口中显示融合后的图像 blend
  • cv2.waitKey(0):等待用户按键,按任意键继续执行。
  • cv2.destroyAllWindows():关闭所有 OpenCV 窗口。
运行效果展示

假设我们有以下两张图像:

  • 背景图像 a.jpg
  • 基于 OpenCV 的图像融合_背景图_03

  • 前景图像 b.jpg
  • 基于 OpenCV 的图像融合_OpenCV_04

运行上述代码后,生成的融合图像 blend.jpg 如下所示:

  • 融合图像 blend.jpg
总结

本文详细介绍了如何使用 OpenCV 实现图像融合,包括读取图像、调整图像大小、图像融合以及保存和显示结果图像。通过理解这些基本概念和技术,你可以更加灵活地在图像处理和创意设计中应用 OpenCV。