先看效果圖:
要求: 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的差別。、
原文鏈接:https://blog.csdn.net/leviopku/article/details/83658767