# opencv-python == 4.10.0.84
# python == 3.10
import cv2
import numpy as np
#from math import sqrt
folder = 'imgs/'
# Read images : src image will be cloned into dst
im = cv2.imread(folder + "auto_first_page.jpg")
# obj = cv2.imread(folder + "edpilot_log.png")
obj = cv2.imread(folder + "ding_edpilot_log_white.jpg")
# 固定 logo 图片的大小
obj_fixed_size = cv2.resize(obj,(734,250))
#cv2.imshow('new_img',obj_fixed_size )#显示新图像
#cv2.imshow('img',obj )#依旧显示原始图像,进行对比
# Create an all white mask
mask = 255 * np.ones(obj_fixed_size.shape, obj_fixed_size.dtype)
# The location of the center of the src in the dst
width, height, channels = im.shape
# center = (height // 2, width // 2)
center = ((height * 10) // 12, (width * 1) // 15)
# Seamlessly clone src into dst and put the results in output
normal_clone = cv2.seamlessClone(obj_fixed_size, im, mask, center, cv2.NORMAL_CLONE)
mixed_clone = cv2.seamlessClone(obj_fixed_size, im, mask, center, cv2.MIXED_CLONE)
# Write results
cv2.imwrite(folder + "normal_merge_w_f.jpg", normal_clone)
cv2.imwrite(folder + "fluid_merge_w_f.jpg", mixed_clone)
参数解释
自己修改对应的路径。这个代码实现的主要函数是cv2.seamlessClone(),这个函数可以根据梯度来调节风格,使得拼接的图像部分不至于那么突兀。对于cv2.seamlessClone(obj, im, mask, center, cv2.NORMAL_CLONE)来讲:
- obj代表的是子图,由cv2读进来的数组文件;
- im代表的是母图,也是由cv2都进来的数组文件;
- mask代表掩模,因为你并不需要把子图所有的部分都贴进来,所以可以用mask划分出一个兴趣域。只需要用0和255区分就可以。如果你不想管这个mask,直接都设置成255就行了;
- center表示坐标,你打算在母图的哪个位置放子图。这里是放在中间。!!!是子图中心点在母图的坐标
- cv2.NORMAL_CLONE代表融合的模式,目前有NORMAL_CLONE、MIXED_CLONE和MONOCHROME_TRANSFER三种方法。
扩展:
图像处理(十二)图像融合(1)Seamless cloning泊松克隆-Siggraph 2004
OpenCV实战 25 使用OpenCV进行泊松克隆
python-opencv读取图片及尺寸修改
python 把图像的指定轮廓内部区域复制到另一张图像上(copy area inside contours to another image)
python图片指定区域替换 img.paste函数