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

1,介绍

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

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

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

2,Delaunay 三角剖分

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

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

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

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

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 中格式如下

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

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.split()
            points.append((int(x),int(y)))
    #Insert points into subdiv
    for p in points:
        subdiv.insert(p)

3,在原图上绘制 Delaunay 三角剖分并预览,这里我加入了动画效果 — 逐线段绘制(用了 for 循环)

#Draw delaunay triangles
def draw_delaunay(img,subdiv,delaunay_color):
    trangleList = subdiv.getTriangleList()
    size = img.shape
    r = (0,0,size[1],size[0])
    for t in  trangleList:
        pt1 = (t[0],t[1])
        pt2 = (t[2],t[3])
        pt3 = (t[4],t[5])
        if (rect_contains(r,pt1) and rect_contains(r,pt2) and rect_contains(r,pt3)):
            cv2.line(img,pt1,pt2,delaunay_color,1)
            cv2.line(img,pt2,pt3,delaunay_color,1)
            cv2.line(img,pt3,pt1,delaunay_color,1)
            
 #Insert points into subdiv
    for p in points:
        subdiv.insert(p)

        #Show animate
        if animate:
            img_copy = img_orig.copy()
            #Draw delaunay triangles
            draw_delaunay(img_copy,subdiv,(255,255,255))
            cv2.imshow(win_delaunary,img_copy)
            cv2.waitKey(100)

预览效果如下:

4,最后绘制 Voronoi Diagram

 def draw_voronoi(img,subdiv):
    (facets,centers) = subdiv.getVoronoiFacetList([])

    for i in range(0,len(facets)):
        ifacet_arr = []
        for f in facets[i]:
            ifacet_arr.append(f)

        ifacet = np.array(ifacet_arr,np.int)
        color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
        cv2.fillConvexPoly(img,ifacet,color)
        ifacets = np.array([ifacet])
        cv2.polylines(img,ifacets,True,(0,0,0),1)
        cv2.circle(img,(centers[i][0],centers[i][1]),3,(0,0,0))
    
  for p in points:
        draw_point(img,p,(0,0,255))

  #Allocate space for Voroni Diagram
  img_voronoi = np.zeros(img.shape,dtype = img.dtype)

  #Draw Voonoi diagram
  draw_voronoi(img_voronoi,subdiv)

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

4,小总结

Delaunay 三角剖分对于第一次接触的小伙伴来说可能还未完全理解,但这一剖分技术对于做人脸识别、融合、换脸是不可或缺的,本篇文章只是仅通过 OpenCV 的 Subdiv2D 函数下实现此功能,真正的识别技术要比这个复杂地多。

对于感兴趣的小伙伴们,我的建议还是跟着提供的代码敲一遍,完整代码贴在下面:

import cv2
import numpy as np
import random

#Check if a point is insied a rectangle
def rect_contains(rect,point):
    if point[0] <rect[0]:
        return False
    elif point[1]<rect[1]:
        return  False
    elif point[0]>rect[2]:
        return False
    elif point[1] >rect[3]:
        return False
    return True

# Draw a point
def draw_point(img,p,color):
    cv2.circle(img,p,2,color)

#Draw delaunay triangles
def draw_delaunay(img,subdiv,delaunay_color):
    trangleList = subdiv.getTriangleList()
    size = img.shape
    r = (0,0,size[1],size[0])
    for t in  trangleList:
        pt1 = (t[0],t[1])
        pt2 = (t[2],t[3])
        pt3 = (t[4],t[5])
        if (rect_contains(r,pt1) and rect_contains(r,pt2) and rect_contains(r,pt3)):
            cv2.line(img,pt1,pt2,delaunay_color,1)
            cv2.line(img,pt2,pt3,delaunay_color,1)
            cv2.line(img,pt3,pt1,delaunay_color,1)

# Draw voronoi diagram
def draw_voronoi(img,subdiv):
    (facets,centers) = subdiv.getVoronoiFacetList([])

    for i in range(0,len(facets)):
        ifacet_arr = []
        for f in facets[i]:
            ifacet_arr.append(f)

        ifacet = np.array(ifacet_arr,np.int)
        color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
        cv2.fillConvexPoly(img,ifacet,color)
        ifacets = np.array([ifacet])
        cv2.polylines(img,ifacets,True,(0,0,0),1)
        cv2.circle(img,(centers[i][0],centers[i][1]),3,(0,0,0))


if __name__ == '__main__':
    #Define window names;
    win_delaunary = "Delaunay Triangulation"
    win_voronoi = "Voronoi Diagram"

    #Turn on animations while drawing triangles
    animate = True

    #Define colors for drawing
    delaunary_color = (255,255,255)
    points_color = (0,0,255)

    #Read in the image
    img_path = "E:/data_ceshi/timg.jpg"

    img = cv2.imread(img_path)

    #Keep a copy   around
    img_orig = img.copy()

    #Rectangle to be used with Subdiv2D
    size = img.shape
    rect = (0,0,size[1],size[0])

    #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.split()
            points.append((int(x),int(y)))
    #Insert points into subdiv
    for p in points:
        subdiv.insert(p)

        #Show animate
        if animate:
            img_copy = img_orig.copy()
            #Draw delaunay triangles
            draw_delaunay(img_copy,subdiv,(255,255,255))
            cv2.imshow(win_delaunary,img_copy)
            cv2.waitKey(100)

    #Draw delaunary triangles
    draw_delaunay(img,subdiv,(255,255,255))

    #Draw points
    for p in points:
        draw_point(img,p,(0,0,255))

    #Allocate space for Voroni Diagram
    img_voronoi = np.zeros(img.shape,dtype = img.dtype)

    #Draw Voonoi diagram
    draw_voronoi(img_voronoi,subdiv)

    #Show results
    cv2.imshow(win_delaunary,img)
    cv2.imshow(win_voronoi,img_voronoi)
    cv2.waitKey(0)

参考链接

https://www.learnopencv.com/

你可能感兴趣的:(OpenCv)