OpenCV-Pythonを使用して、顔のドロネー三角形分割(顔検出のコアテクノロジーの1つ)を実行します

1はじめに

タイトルにドロネー三角形分割とボロノイという2つの単語が含まれているのを見ると、パートナーを初めて見たときに混乱する可能性があります(私は自分自身について話しています)。これら2つの概念をより直感的に理解するには、次の図を参照してください。

左:68の顔の特徴点中央:ドロネー三角形分割、右ボロノイ図

左の写真は前の記事で述べた68の顔の特徴点マーカーです。真ん中の写真は左の写真に基づいており、左の写真に基づいて68ポイント間でドロネー三角形分割(Delaunay)を形成しています。中央に描かれたボロノイ図図

2.ドロネー三角形分割

Delaunay三角形分割アルゴリズムは、ロシアの数学者Boris Delaunayにちなんで名付けられました。この方法の目的は、三角形分割で三角形の最小角度を最大化することであり、「非常に薄い」三角形の出現を回避することを目的としています。

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

上の左と右の画像の変換ステーションは、Delaunayが最小角度を最大化する方法を示しています。左と右の画像は4つの頂点の2つの異なる分割方法ですが、左の画像の頂点AとCは三角形のBCDとABD。円周の内側で、角度Cを非常に大きくします

右の図では、分割形式に2つの変更があります。1。B座標とD座標が右にシフトされます。2。分割線がBDからACに変更されます。最後に、分割後の三角形はそれほど「細く」ありません。

3、ボロノイ図

ボロノイという名前もロシアの数学者ジョージ・ボロノイに由来しています。興味深いのは、ジョージ・ボロノイがボリス・ドロネーの博士課程の監督者であるということです。

ボロノイ図は、ドロネー三角形分割に基づいて作成されます。ドロネー三角形分割のすべての頂点を取り、隣接する三角形の周囲を線分で接続して領域を形成します。隣接する領域は異なる色で覆われています。ボロノイ図は現在よく使用されています。凸面領域。セグメンテーションフィールド

以下の20個の頂点で構成されるボロノイ図から、図内の隣接する点間の距離の長さが等しいことがわかります。

20個の頂点で構成されるボロノイ

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インスタンスを作成し(このクラスは次の2つの画像の描画で使用されます)、すべての点をに挿入します作成されたクラス:

 #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.元の画像にドロネー三角形分割を描画してプレビューします。ここでは、アニメーション効果を追加しました-線分による描画(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.最後にボロノイ図を描きます

 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.小さな要約

ドロネー三角形分割は初めての友達には完全には理解されていないかもしれませんが、この細分化技術は顔認識、融合、顔の変化に不可欠です。この記事は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/

おすすめ

転載: blog.csdn.net/weixin_42512684/article/details/106650061