利用 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.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)

預覽效果如下:

imag11252323.gif

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)

Snipaste_2020-06-04_14-43-10.png

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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章