1,介绍
看到标题里的两个词 Delaunay 三角剖分 和 Voronoi,估计第一次见到的小伙伴可能一脸懵(说的就是我自己),为了更直观地认识这两个概念,请看下图:
左图是上篇文章提到的 68个人脸特征点标记,中图是基于左图的基础上对 68个点进行 点与点之间形成 Delaunay 三角剖分(德劳内),左图是基于中间图绘制的的 Voronoi Diagram (沃罗诺伊图)
2,Delaunay 三角剖分
Delaunay 三角剖分算法命名那个来源于俄国数学家 Boris Delaunay,该方法目的是最大化三角剖分中三角形中最小角,目的是避免“极瘦“的三角形的出现
上方左图与右图的变换站示的就是 Delaunay 怎样最大化最小角,左右两图是对于四个顶点的两种不同的剖分方式;但左图中 顶点 A、C 不在三角形 BCD、ABD 的外接圆内,使得 角 C 非常大
右图对剖分形式有两个方的 改动:1,B、D 坐标右移;2,剖分线由 BD 变为 AC ;最后使得剖分后的三角形不那么”瘦“
3,Voronoi Diagram
Voronoi 命名同样也是来源于一个 俄国数学家 Georgy Voronoy,有趣的是 Georgy Voronoy 是 Boris Delaunay 的博士导师
Voronoi 图是基于 Delaunay 三角剖分创建,取 Delaunay 剖分的所有顶点,用线段连接相邻三角形的外接圆心,构成一个区域,相邻不同区域用不同颜色覆盖;Voronoi 图目前常用于凸边形区域分割领域
从下面20个顶点组成的 Voronoi 图种可以了解到,图中相邻点与点之间的距离是等长的
4,OpenCV 代码实现
1,首先需要获取人脸 68 个特征点坐标,并写入 txt 文件,方便后面使用,这里会用到的代码
import dlib
import cv2
predictor_path = "E:/data_ceshi/shape_predictor_68_face_landmarks.dat"
png_path = "E:/data_ceshi/timg.jpg"
txt_path = "E:/data_ceshi/points.txt"
f = open(txt_path,'w+')
detector = dlib.get_frontal_face_detector()
#相撞
predicator = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
img1 = cv2.imread(png_path)
dets = detector(img1,1)
print("Number of faces detected : {}".format(len(dets)))
for k,d in enumerate(dets):
print("Detection {} left:{} Top: {} Right {} Bottom {}".format(
k,d.left(),d.top(),d.right(),d.bottom()
))
lanmarks = [[p.x,p.y] for p in predicator(img1,d).parts()]
for idx,point in enumerate(lanmarks):
f.write(str(point[0]))
f.write("\t")
f.write(str(point[1]))
f.write('\n')
写入后,txt 中格式如下
2,利用图像大小创建一个矩形范围( 因为脸部特征点都是图中),创建一个 Subdiv2D 实例(后面两个图的绘制都会用到这个类),把点都插入创建的类中:
#Create an instance of Subdiv2d
subdiv = cv2.Subdiv2D(rect)
#Create an array of points
points = []
#Read in the points from a text file
with open("E:/data_ceshi/points.txt") as file:
for line in file:
x,y = line