Opencv python: seamlessClone泊松融合 (我把Lena變成了彼岸花怪/(ㄒoㄒ)/~~)

爲什麼要進行融合呢?原因是LZ在進行貼圖操作的時候,經常會出現很明顯的邊界效應,在各種查找資料的情況下,找到了一種比較適合圖像融合的方法,並且OpenCV有對應的接口,所以就網上下了圖片,做了一些嘗試,當然最後並沒有使用這個函數是因爲貼圖效果太不明顯了,LZ甚至以爲是自己代碼寫錯了,所以這個函數因人而異。

首先這個函數的用法:

def seamlessClone(src, dst, mask, p, flags, blend=None): # real signature unknown; restored from __doc__
    """
    seamlessClone(src, dst, mask, p, flags[, blend]) -> blend
    .   @brief Image editing tasks concern either global changes (color/intensity corrections, filters,
    .   deformations) or local changes concerned to a selection. Here we are interested in achieving local
    .   changes, ones that are restricted to a region manually selected (ROI), in a seamless and effortless
    .   manner. The extent of the changes ranges from slight distortions to complete replacement by novel
    .   content @cite PM03 .
    .   
    .   @param src Input 8-bit 3-channel image.
    .   @param dst Input 8-bit 3-channel image.
    .   @param mask Input 8-bit 1 or 3-channel image.
    .   @param p Point in dst image where object is placed.
    .   @param blend Output image with the same size and type as dst.
    .   @param flags Cloning method that could be cv::NORMAL_CLONE, cv::MIXED_CLONE or cv::MONOCHROME_TRANSFER
    """
    pass

NORMAL_CLONE
Python: cv.NORMAL_CLONE
The power of the method is fully expressed when inserting objects with complex outlines into a new background
將具有複雜輪廓的對象插入新背景,也就是說不保留dst 圖像的texture細節,目標區域的梯度只由源圖像決定。

MIXED_CLONE
Python: cv.MIXED_CLONE

The classic method, color-based selection and alpha masking might be time consuming and often leaves an undesirable halo. Seamless cloning, even averaged with the original image, is not effective. Mixed seamless cloning based on a loose selection proves effective.
基於寬鬆選擇的混合無縫克隆,保留des圖像的texture 細節。目標區域的梯度是由原圖像和目的圖像的組合計算出來(計算dominat gradient)。

MONOCHROME_TRANSFER
Python: cv.MONOCHROME_TRANSFER
Monochrome transfer allows the user to easily replace certain features of one object by alternative features.
不保留src圖像的顏色細節,只有src圖像的質地,顏色和目標圖像一樣,可以用來進行皮膚質地填充(美顏是不是可以用呢?)

import cv2 
import numpy as np

# read our test imges
img1 = cv2.imread("../test_imgs/lena_standard.jpg")
img2 = cv2.imread("../test_imgs/beiji.jpeg")
cv2.imshow("img1", img1)
cv2.imshow("img2", img2)
cv2.waitKey()
cv2.destroyAllWindows()

在這裏插入圖片描述
在這裏插入圖片描述

# create an white mask
mask = 255*np.ones(img1.shape, img1.dtype)
# the location of the src in the dst
width, height, channel = img2.shape
center = (int(height/2), int(width/2))
normal_clone = cv2.seamlessClone(img1, img2, mask, center, cv2.NORMAL_CLONE)
cv2.imshow("normal_clone", normal_clone)
cv2.waitKey()

在這裏插入圖片描述

mixed_clone = cv2.seamlessClone(img1, img2, mask, center, cv2.MIXED_CLONE)
cv2.imshow("mixed_clone", mixed_clone)
cv2.waitKey()
# cv2.destroyAllWindows()

在這裏插入圖片描述

mono_clone = cv2.seamlessClone(img1, img2, mask, center, cv2.MONOCHROME_TRANSFER)
cv2.imshow("mono_clone", mono_clone)
cv2.waitKey()
cv2.destroyAllWindows()

在這裏插入圖片描述
額,測試圖片感覺沒選好,融合起來感覺哪裏怪怪的/(ㄒoㄒ)/~~

#這段代碼寫的可隨意了,就是生成一個mask,做一個融合
img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)    
ret, binary = cv2.threshold(img1_gray, 100, 255, cv2.THRESH_BINARY | cv2.THRESH_TRIANGLE)

contours, _ = cv2.findContours(binary,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# print("contour: ", contour)
src_mask = np.zeros(img1.shape, img1.dtype)

area = []
want_area = []
for i in range(len(contours)):
    area.append(cv2.contourArea(contours[i]))
area.sort()
for i in range(len(contours)):
    if (cv2.contourArea(contours[i]) < area[-1]):
        cv2.fillConvexPoly(src_mask, contours[i], 0)
    else:
        cv2.fillConvexPoly(src_mask, contours[i], (255,255, 255))
test_mask = src_mask[:,:,1]


# print("mask_shape: ", mask_new.shape)
normal_clone_bin = cv2.seamlessClone(img1, img2, test_mask, center, cv2.NORMAL_CLONE)

cv2.imshow("normal_clone_bin", normal_clone_bin)
cv2.waitKey()
cv2.destroyAllWindows()

在這裏插入圖片描述

參考地址:

  1. https://www.jianshu.com/p/49adfbe4b804
  2. https://docs.opencv.org/3.4/df/da0/group__photo__clone.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章