opencv实现无缝融合--seamless clone

先看效果图:

                                                                                                     +

                                                                                                     ||

要求: opencv

再看python代码实现: 

import cv2
import numpy as np
from math import sqrt

folder = 'ball_merge/'
# Read images : src image will be cloned into dst
im = cv2.imread(folder + "backdrop.jpg")
obj = cv2.imread(folder + "char.jpg")
# Create an all white mask
mask = 255 * np.ones(obj.shape, obj.dtype)

# The location of the center of the src in the dst
width, height, channels = im.shape
center = (height // 2, width // 2)

# Seamlessly clone src into dst 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)

# Write results
cv2.imwrite(folder + "normal_merge.jpg", normal_clone)
cv2.imwrite(folder + "fluid_merge.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代表融合的模式,可以比较 cv2.NORMAL_CLONE和cv2.MIXED_CLONE的差别。、

猜你喜欢

转载自blog.csdn.net/leviopku/article/details/83658767