opencv實現無縫融合--seamless clone
阿新 • • 發佈:2018-11-16
先看效果圖:
+
||
要求: 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的差別。、