我正在尝试在python中制作换发应用程序。我已经成功提取出头发。但是,它没有给出令人满意的结果。但是对于最后一部分,我需要从样本头部获取头发图像并将其放入样本模型图像中。为此,我使用了无缝克隆。这是我的代码和最终图片:
import cv2
import numpy as np
# Read images : src image will be cloned into dst
im = cv2.imread("source.jpg")
obj = cv2.imread("hair.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 = (250, 115)
# Seamlessly clone src into dst and put the results in output
mixed_clone = cv2.seamlessClone(obj, im, mask, center, cv2.MIXED_CLONE)
# Write results
cv2.imwrite("normal-clone.jpg", mixed_clone)
cv2.waitKey(0)
cv2.destroyAllWindows()
我知道它的外观不好看,但是这里的主要问题是当我使用仿射变换实现缩放和旋转目的,然后使用以下代码进行无缝克隆时:
import cv2
import numpy as np
# Read images : src image will be cloned into dst
im = cv2.imread("source.jpg")
obj = cv2.imread("hair.jpg")
#Wrap Affine transformation
rows,cols,ch = obj.shape
pts1 = np.float32([[160,199],[250,54],[339,200]])
pts2 = np.float32([[151,196],[250,33],[347,196]])
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(obj,M,(cols,rows))
# Create an all white mask
mask = 255 * np.ones(dst.shape, dst.dtype)
# The location of the center of the src in the dst
width, height, channels = im.shape
center = (250, 115)
# Seamlessly clone src into dst and put the results in output
mixed_clone = cv2.seamlessClone(dst, im, mask, center, cv2.MIXED_CLONE)
# Write results
cv2.imshow("normal-clone", mixed_clone)
cv2.waitKey(0)
cv2.destroyAllWindows()
然后,我得到以下结果:
我为这些坐标点生成编写了另一个代码。我将在以后合并它们,这样就不必手动输入那些坐标点。 但是现在,我想知道为什么仿射变换会影响无缝克隆。我在做什么错??
或者,任何用于换发的解决方案将不胜感激。