python长啥样_两张脸融合在一起长啥样?Opencv-Python 来告诉你!

提了好几天的人脸融合技术,今天终于被提上日程,该技术是基于之前介绍的 特征点提取、Delaunay 三角剖分延伸得到的,如果之前没有了解过这两篇文章,建议提前看下 利用 OpenCV-Python 进行人脸 Delaunay 三角剖分(人脸检测核心技术之一),实现人脸识别、人脸68个特征点提取,或许这个 Python 库能帮到你!

1,Image Morphing 介绍

图像融合简单来说,通过把图像设置为不同的透明度,把两张图像融合为一张图像(一般要求图像需要等尺寸),公式如下:

b151f8198618367a6c222aef076445d2b31ce573.png?token=88641a4340d36e3b0793d0443618c4b6

可以根据这个公式尝试实现一下融合技术,利用 OpenCV 的 cv2.addWeighted() 函数,代码如下:

import cv2import numpy as npfile_path1 = "E:/data_ceshi/1.jpg"file_path2 = "E:/data_ceshi/2.jpg"img1 = cv2.imread(file_path1)img2 = cv2.imread(file_path2)morph_img = cv2.addWeighted(img1,0.5,img2,0.5,0)save_img = np.hstack((img1,morph_img,img2))cv2.imwrite("E:/data_ceshi/save.jpg",save_img)cv2.imshow("morph_img",save_img)cv2.waitKey(0)这里 alpha 设置为 0.5, 最终结果如下图:

c2cec3fdfc0392455cac9733b5836ac47d1e258a.jpeg?token=ce8307e42b67db281be17cf098466aec

左右两边分别为融合之前的两张图片,中间为融合结果,看起来非常不好,图片中脸的部分的确融合了,但是给我们的感觉就是明显的失真效果,太假了

对于上面提到的融合想法,若想要达到不错的效果需要对人脸区域进行对齐操作,而这一步就需要用到之前介绍的技术:人脸68个特征点提取,Delaunay 三角剖分

2,特征点提取

在做人脸对齐时,不仅需要考虑人脸部分需要对齐,这里也需要考虑图片的整体性(例如头发、脖子、肩膀等部位),这里除去 dlib 提取 68 个特征点之外,又加入了12个特征点(人工标记)分别为图像四角、四边中点、肩膀处,右耳边缘、脖子等

bba1cd11728b471038958582e8d90dfbfd0323e7.jpeg?token=8bb7d99a9a32eeff172abab8c5485662

3,Delaunay 三角剖分

三角剖分目的网格化图像脸部区域,方便寻找特征对应点,为后面使用仿射变换进行对齐操作:

f2deb48f8c5494ee2ac4fe0705e22ef898257eec.jpeg?token=cfa2e6ba163c5bef9315a79c08d26865

从三角剖分图上来看,人脸区域轮廓是非常相似的,人脸融合时需要把脸部每一个对应的小三角区域事先一一对齐,然后利用设置的透明度参数来做最终的效果融合。这样结果就显得不那么失真。

4,Face Morph(脸部融合)

下面将脸部融合技术拆解为几部分:

1,脸部特长点提取、三角剖分(前面已经详细介绍了,这里就不再一一展开了),详情参考这篇文章:

利用 OpenCV-Python 进行人脸 Delaunay 三角剖分(人脸检测核心技术之一)

实现人脸识别、人脸68个特征点提取,或许这个 Python 库能帮到你!

2,对 1 中的三角剖分每个顶点做对应点衔接并记录下来,对应点记录的是三角形三顶点的索引数,如下图所示:

42166d224f4a20a4cb38b00db6455624730ed0f0.jpeg?token=657d54836745af0f18f04b9ccf5eefac

3,图片中对每一个三角剖分区域做放射变换,用到的函数:getAffineTransform() 得到仿射变换矩阵,warpAffine() 进行放射变换,最终得到两个变换图像,

4,对 3 中得到的两图像中像素值调整透明度参数,来进行图像融合

最终结果如下:

7af40ad162d9f2d34117066f82fb44156227cccd.jpeg?token=9e032fbd9256c653a5ca65964283be61

out_img1.jpg

342ac65c103853430b2ec8f8b9047e78cb808820.jpeg?token=019a8ce0ae4fde71191e1b08e93258ec

out_img2.jpg

574e9258d109b3deb060524ce5a8a287810a4cd0.jpeg?token=06eda9bc8376d2f09d84ee17cb967750

out_img3.jpg结果来看,脸部区域能够取得不错的结果,但整体来看仍然有很大的瑕疵,但是我们可以通过手动选择更多特征对应点来改善这种效果,最后附上完整代码

import cv2import numpy as npimport sys#Read points from text filedefreadPoints(path):# Create an array of points points = []# Read pointswith open(path) as file:for line in file: x,y = line.split() points.append((int(x),int(y)))return points# Apply affine tranform calculated using srcTri and sdtTri to src and output an image of sizedefapplyAffineTransform(src,srcTri,dstTri,size):#Given a pair of triangles,find the affine transform. warpMat = cv2.getAffineTransform(np.float32(srcTri),np.float32(dstTri))#Apply the Affine Transform just foundto the src image dst = cv2.warpAffine(src,warpMat,(size[0],size[1]),None,flags=cv2.INTER_LINEAR,borderMode=cv2.BORDER_REFLECT_101)return dst# Warps and alpha blends triangular regions from img1 and img2 to imgdefmorphTriangle(img1,img2,img,t1,t2,t,alpha):#Find bounding rectangle for each triangle r1 = cv2.boundingRect(np.float32([t1])) r2 = cv2.boundingRect(np.float32([t2])) r = cv2.boundingRect(np.float32([t]))# Offset points by left top corner of the respective rectangles t1Rect = [] t2Rect = [] tRect = []for i in range(0,3): tRect.append(((t[i][0] - r[0]),(t[i][1]-r[1]))) t1Rect.append(((t1[i][0]-r1[0]),(t1[i][1]-r1[1]))) t2Rect.append(((t2[i][0] -r2[0]),(t2[i][1]-r2[1])))# Get mask by filling triangles mask = np.zeros((r[3],r[2],3),dtype = np.float32) cv2.fillConvexPoly(mask,np.int32(tRect),(1.0,1.0,1.0),16,0)# Apply warpImage to small rectangular patched img1Rect = img1[r1[1]:r1[1]+r1[3],r1[0]:r1[0]+r1[2]] img2Rect = img2[r2[1]:r2[1]+r2[3],r2[0]:r2[0]+r2[2]] size = (r[2],r[3]) warpImage1 = applyAffineTransform(img1Rect,t1Rect,tRect,size) warpImage2 = applyAffineTransform(img2Rect,t2Rect,tRect,size)# Alpha blend rectangular patches imgRect = (1.0-alpha) *warpImage1 +alpha*warpImage2# Copy triangular region of rectangular patch to tje output image print(r[1],r[3],r[0],r[2]) print(imgRect.shape) img[r[1]:r[1]+r[3],r[0]:r[0]+r[2]] = img[r[1]:r[1]+r[3],r[0]:r[0]+r[2]]*(1-mask) +imgRect*maskif __name__ =='__main__': filename1 = "E:/data_ceshi/2.jpg" filename2 = "E:/data_ceshi/3.jpg" points_txt1 = "E:/data_ceshi/2.txt" points_txt2 ="E:/data_ceshi/3.txt" alpha = 0.5# Read images img1 = cv2.imread(filename1) img2 = cv2.imread(filename2)# Convertat to float data type img1 = np.float32(img1) img2 = np.float32(img2)# Read array of corresponding points points1 = readPoints(points_txt1) points2 = readPoints(points_txt2) points = []# Compute weighted average point coordinatefor i in range(0,len(points1)): x = (1-alpha) *points1[i][0] +alpha *points2[i][0] y = (1-alpha)*points1[i][1] + alpha*points2[i][1] points.append((x,y)) imgMorph = np.zeros(img1.shape,dtype = img1.dtype)# Read triangles for tri.txtwith open("E:/data_ceshi/tri.txt") as file:for line in file: x,y,z = line.split() x = int(x) y = int(y) z = int(z) t1 = [points1[x],points1[y],points1[z]] t2 = [points2[x],points2[y],points2[z]] t = [points[x],points[y],points[z]]# Morph one triangle at a time morphTriangle(img1,img2,imgMorph,t1,t2,t,alpha)# Display Results out_img = np.hstack((img1,imgMorph,img2)) cv2.imwrite("E:/data_ceshi/out_img.jpg",out_img) cv2.imshow("Morphed Face",np.uint8(imgMorph)) cv2.waitKey(0)5,小总结

虽然本次面向对象是人脸,但相同技术原理也可以运用到其他物体上面,比如把苹果和橘子部分融合、人脸区域更换等功能,如果有更好的 idea 的话,可能会得到意想不到的结果,也可以在下方留言!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值