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

1,介绍

看到标题里的两个词 Delaunay 三角剖分 和 Voronoi,估计第一次见到的小伙伴可能一脸懵(说的就是我自己),为了更直观地认识这两个概念,请看下图:

左图:68个人脸特征点 中图:Delaunay 三角剖分,右图 Voronoi 图表

左图是上篇文章提到的 68个人脸特征点标记,中图是基于左图的基础上对 68个点进行 点与点之间形成 Delaunay 三角剖分(德劳内),左图是基于中间图绘制的的 Voronoi Diagram (沃罗诺伊图)

2,Delaunay 三角剖分

Delaunay 三角剖分算法命名那个来源于俄国数学家 Boris Delaunay,该方法目的是最大化三角剖分中三角形中最小角,目的是避免“极瘦“的三角形的出现

Snipaste_2020-06-04_15-23-46.png

上方左图与右图的变换站示的就是 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 图种可以了解到,图中相邻点与点之间的距离是等长的

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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小张Python1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值