在图像融合领域,论文中的对比算法可视化,需要将红外图像和可见光图像沿着斜对角线合并成一张图片。
红外与可见光图像举例:
然后做出这样的效果:
用Python的PIL库,将两张图片沿着斜对角线合并成一张图片。
代码重新封装成函数,方便调用,增加两图片之间的对角线那根白线,效果图懒得上传了,自己试试就知道了。如果需要批量处理,自己写吧。
#####################################################
# 图片拼接函数:Image_Merge(left_image_path, right_image_path, is_save=True)
# 参数:
# left_image_path:左上角图片的路径
# right_image_path:右下角图片的路径
# is_save=True:直接保存图片到“./result.png”
# is_save=Flase:返回图片
######################################################
def Image_Merge(left_image_path, right_image_path, is_save=True):
from PIL import Image, ImageDraw
# 打开两张图片,并转成RGBA格式,用于调整透明图层
image1 = Image.open(right_image_path).convert('RGBA')
image2 = Image.open(left_image_path).convert('RGBA')
assert image1.size == image2.size
w, h = image1.size
# 对vi图片作为背景,创建一个绘图对象
draw = ImageDraw.Draw(image1)
# 绘制一个斜对角线的三角形,即左上角的三角形,设置透明度为0
draw.polygon([(0, 0), (w, 0), (0, h)], fill=(0, 0, 0, 0))
# 将二者叠加图层,并转成RGB格式
result = Image.alpha_composite(image2, image1).convert('RGB')
# 再加一根白线,明显一点
draw = ImageDraw.Draw(result)
draw.line([w, 0, 0, h], fill=None, width=3, joint=None)
if is_save:
# 保存图片
result.save('result.png')
else:
return result
# 两张图片的路径
right_image_path = r'vi.png' # 右下角
left_image_path = r'ir.png' # 左上角
Image_Merge(left_image_path, right_image_path, is_save=True)