bkg_image:
原图尺寸:(375,500)


face_image:
融合的位置点坐标:h1,h2,w1,w2 = 27,295,102,372

blendwidth为融合范围的宽度
import cv2
import numpy as np
from scipy.ndimage import gaussian_filter
import time
bkg_image = cv2.imread("data/077436.jpg")
face_image = cv2.imread("data/1.jpg")
blendwidth = 20
h1,h2,w1,w2 = 27,295,102,372
#blend mask generate
blend_mask_base = np.zeros(shape=face_image.shape)
blend_mask_base[blendwidth:face_image.shape[0]-blendwidth, blendwidth:face_image.shape[1]-blendwidth] = 1
blend_mask_base = gaussian_filter(blend_mask_base, sigma=7)
# cv2.imwrite("mask.png",blend_mask_base*255)
t0 = time.perf_counter()
bkg_image_temp = bkg_image[h1:h2, w1:w2]
temp = face_image * blend_mask_base + bkg_image_temp * (1 - blend_mask_base)
temp = np.clip(temp,0,255)
bkg_image[27:295, 102:372] = temp
t1 = time.perf_counter()
print(f"inference time: {1000*(t1-t0):.2f}ms.")
cv2.imwrite("test.png",bkg_image)
融合后的效果:

该代码示例展示了如何使用OpenCV库在Python中进行图像融合。它读取两张图片,一张背景图片和一张人脸图片,然后定义一个融合区域并应用高斯滤波器生成融合掩模。最后,将人脸图像与背景图像在指定区域内按掩模进行融合,并保存结果。

1023

被折叠的 条评论
为什么被折叠?



