Python cv2.Subdiv2D() Examples

The following are 6 code examples of cv2.Subdiv2D(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: triangulation.py    From yry with Apache License 2.0 6 votes vote down vote up
def measure_triangle(image, points):
    rect = (0, 0, image.shape[1], image.shape[0])
    sub_div = cv2.Subdiv2D(rect)

    for p in points:
        sub_div.insert(p)

    triangle_list = sub_div.getTriangleList()

    triangle = []
    pt = []

    for t in triangle_list:
        pt.append((t[0], t[1]))
        pt.append((t[2], t[3]))
        pt.append((t[4], t[5]))

        pt1 = (t[0], t[1])
        pt2 = (t[2], t[3])
        pt3 = (t[4], t[5])

        if rect_contains(rect, pt1) and rect_contains(rect, pt2) and rect_contains(rect, pt3):
            ind = []
            for j in range(0, 3):
                for k in range(0, len(points)):
                    if abs(pt[j][0] - points[k][0]) < 1.0 and abs(pt[j][1] - points[k][1]) < 1.0:
                        ind.append(k)
            if len(ind) == 3:
                triangle.append((ind[0], ind[1], ind[2]))

        pt = []

    return triangle 
Example #2
Source File: AverageFace.py    From Machine-Learning-Study-Notes with Apache License 2.0 5 votes vote down vote up
def calculateDelaunayTriangles(rect, points):
        def rectContains(point, rect):
            if point[0] < rect[0] or point[0] > rect[2]:
                return False
            elif point[1] < rect[1] or point[1] > rect[3]:
                return False
            return True  
        subDiv = cv2.Subdiv2D(rect)
        for point in points:
            subDiv.insert((point[0], point[1]))
        triangleList = subDiv.getTriangleList()
        return_mat = []
        for triangle in triangleList:
            pt = []
            pt.append((triangle[0], triangle[1]))
            pt.append((triangle[2], triangle[3]))
            pt.append((triangle[4], triangle[5])) 
            if rectContains(pt[0], rect) and rectContains(pt[1], rect) and rectContains(pt[2], rect):
                ind = []
                for i in range(3):
                    for j in range(len(points)):
                        if np.abs(pt[i][0] - points[j][0]) < 1.0 and np.abs(pt[i][1] - points[j][1]) < 1.0:
                            ind.append(j)
                if len(ind) == 3:
                    return_mat.append(ind)
        return return_mat 
Example #3
Source File: FaceAverage.py    From Machine-Learning-Study-Notes with Apache License 2.0 5 votes vote down vote up
def calculateDelaunayTriangles(rect, points):
    subdiv = cv2.Subdiv2D(rect);
    for p in points:
        subdiv.insert((p[0], p[1]));
    triangleList = subdiv.getTriangleList();
    delaunayTri = []

    for t in triangleList:
        pt = []
        pt.append((t[0], t[1]))
        pt.append((t[2], t[3]))
        pt.append((t[4], t[5]))

        pt1 = (t[0], t[1])
        pt2 = (t[2], t[3])
        pt3 = (t[4], t[5])

        if rectContains(rect, pt1) and rectContains(rect, pt2) and rectContains(rect, pt3):
            ind = []
            for j in range(0, 3):
                for k in range(0, len(points)):
                    if (abs(pt[j][0] - points[k][0]) < 1.0 and abs(pt[j][1] - points[k][1]) < 1.0):
                        ind.append(k)
            if len(ind) == 3:
                delaunayTri.append((ind[0], ind[1], ind[2]))

    return delaunayTri 
Example #4
Source File: triangulation.py    From face_merge_master with Apache License 2.0 5 votes vote down vote up
def measure_triangle(image, points):
    rect = (0, 0, image.shape[1], image.shape[0])
    sub_div = cv2.Subdiv2D(rect)

    for p in points:
        sub_div.insert(p)

    triangle_list = sub_div.getTriangleList()

    triangle = []
    pt = []

    for t in triangle_list:
        pt.append((t[0], t[1]))
        pt.append((t[2], t[3]))
        pt.append((t[4], t[5]))

        pt1 = (t[0], t[1])
        pt2 = (t[2], t[3])
        pt3 = (t[4], t[5])

        if rect_contains(rect, pt1) and rect_contains(rect, pt2) and rect_contains(rect, pt3):
            ind = []
            for j in range(0, 3):
                for k in range(0, len(points)):
                    if abs(pt[j][0] - points[k][0]) < 1.0 and abs(pt[j][1] - points[k][1]) < 1.0:
                        ind.append(k)
            if len(ind) == 3:
                triangle.append((ind[0], ind[1], ind[2]))

        pt = []

    return triangle 
Example #5
Source File: faceSwap.py    From voice-enabled-chatbot with MIT License 4 votes vote down vote up
def calculateDelaunayTriangles(rect, points):
    # create subdiv
    subdiv = cv2.Subdiv2D(rect);
    # Insert points into subdiv
    for p in points:
        subdiv.insert(p)

    triangleList = subdiv.getTriangleList();

    delaunayTri = []

    pt = []
    for t in triangleList:
        pt.append((t[0], t[1]))
        pt.append((t[2], t[3]))
        pt.append((t[4], t[5]))

        pt1 = (t[0], t[1])
        pt2 = (t[2], t[3])
        pt3 = (t[4], t[5])

        if rectContains(rect, pt1) and rectContains(rect, pt2) and rectContains(rect, pt3):
            ind = []
            # Get face-points (from 68 face detector) by coordinates
            for j in range(0, 3):
                for k in range(0, len(points)):
                    if (abs(pt[j][0] - points[k][0]) < 1.0 and abs(pt[j][1] - points[k][1]) < 1.0):
                        ind.append(k)
                        # Three points form a triangle. Triangle array corresponds to the file tri.txt in FaceMorph
            if len(ind) == 3:
                delaunayTri.append((ind[0], ind[1], ind[2]))

        pt = []
    return delaunayTri 
Example #6
Source File: delaunay.py    From face-morphing with MIT License 3 votes vote down vote up
def makeDelaunay(theSize1,theSize0,theList):

    # Check if a point is inside 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

    # Write the delaunay triangles into a file
    def draw_delaunay(subdiv,dictionary1) :

        list4=[]

        triangleList = subdiv.getTriangleList();
        r = (0, 0, theSize1,theSize0)

        for t in triangleList :
            pt1 = (int(t[0]), int(t[1]))
            pt2 = (int(t[2]), int(t[3]))
            pt3 = (int(t[4]), int(t[5]))
            if rect_contains(r, pt1) and rect_contains(r, pt2) and rect_contains(r, pt3) :
                list4.append((dictionary1[pt1],dictionary1[pt2],dictionary1[pt3]))
        dictionary1={}

        return list4
    
    # Make a rectangle.
    rect = (0, 0, theSize1,theSize0)

    # Create an instance of Subdiv2D.
    subdiv = cv2.Subdiv2D(rect);

    # Make a points list and a searchable dictionary. 
    theList=theList.tolist()
    points=[(int(x[0]),int(x[1])) for x in theList]
    dictionary={x[0]:x[1] for x in list(zip(points,range(76)))}
    # Insert points into subdiv
    for p in points :
        subdiv.insert(p)
        
    # Make a delaunay triangulation list.
    list4=draw_delaunay(subdiv,dictionary);
    
    # Return the list.
    return list4

# makeDelaunay(1080,1440)