图像融合
✔️ 图像融合:
- 背景:图像融合是图像处理的一个基本问题,目的是将源图像中一个物体或者一个区域嵌入到目标图像生成一个新的图像。在对图像进行合成的过程中,为了使合成后的图像更自然,合成边界应当保持无缝。但如果源图像和目标图像有着明显不同的纹理特征,则直接合成后的图像会存在明显的边界。
- 引入:基于泊松方程而引入的泊松融合求解像素最优值的方法,在保留了源图像梯度信息的同时,融合源图像与目标图像。该方法根据用户指定的边界条件求解一个泊松方程,实现了梯度域上的连续,从而达到边界处的无缝融合。
✔️ 对比传统图像融合和泊松融合:
- 精确地选择融合区域:过程单调乏味且工作量大,常常无法得到好的结果。
- Alpha-Matting:功能强大,但是实现复杂。
- 选择融合区域的过程简单且方便。
- 最终可以得到无缝融合的结果。
✔️ 变分法的解释泊松图像编辑:
✍️
表示融合图像块的梯度。
变分方程的意义表明我们的无缝融合是以源图像块内梯度场为指导,将融合边界上目标场景和源图像的差异平滑地扩散到融合图像块 I 中,这样的话,融合后的图像块能够无缝地融合到目标场景中,并且其色调和光照可以与目标场景相一致。
示例代码
- 读取需要融合的两张图:
import cv2
import numpy as np
src = cv2.imread("images/airplane.jpg")
dst = cv2.imread("images/sky.jpg")
src
dst
- 创建飞机的mask:
src_mask = np.zeros(src.shape, src.dtype)
# 绘制多边形
poly = np.array([ [4,80], [30,54], [151,63], [254,37], [298,90], [272,134], [43,122] ], np.int32)
cv2.fillPoly(src_mask, [poly], (255, 255, 255))
src_mask
- 融合操作使用
cv2.eamlessClone()
:
- 参数1:融合图(飞机)
- 参数2:目标图(天空)
- 参数3:融合图的mask
- 参数4:融合图位置中心
- 参数5:融合方式,常用
cv2.MIXED_CLONE
和cv2.NORMAL_CLONE
# 飞机中心位置在dst的坐标
center = (500,100)
# 泊松融合.
output1 = cv2.seamlessClone(src, dst, src_mask, center, cv2.MIXED_CLONE)
Output
对比cv2.MIXED_CLONE
和 cv2.NORMAL_CLONE
import cv2
import numpy as np
# Read images : obj image will be cloned into im
obj = cv2.imread("images/animations.jpg")
im= cv2.imread("images/wooden.png")
H,W = obj.shape[:2]
obj = cv2.resize(obj, (W, H-150), cv2.INTER_CUBIC)
# Create an all white mask
mask = 255 * np.ones(obj.shape, obj.dtype)
# The location of the center of the obj in the im
width, height, channels = im.shape
center = (int(height/2), int(width/2)-120)
# Seamlessly clone obj into im and put the results in output
normal_clone = cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)
mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)
- 输入图片:
obj
im
- 输出对比 Normal VS Mixed:
Normal
Mixed
其他示例图:
Sample-1
- 图片输入:
- 图片输出:
Sample-2
- 图片输入:
- 图片输出:
------------------------------------------可爱の分割线------------------------------------------
更多Opencv教程可以 Follow github的opencv教程,中文&English 欢迎Star❤️❤️❤️
JimmyHHua/opencv_tutorialsgithub.com
参考
- 实现简单的泊松融合
- 泊松融合
- Seamless Cloning using OpenCV