コード例 #1
0
ファイル: Swap.py プロジェクト: goldsamantha/inyourface
    def calculateDelaunayTriangles(self, rect, points):
        #create subdiv
        subdiv = cv2.Subdiv2D(rect)

        # Insert points into subdiv
        for p in points:
            subdiv.insert(p)

        triangleList = subdiv.getTriangleList()

        delaunayTri = []

        pt = []

        count = 0

        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 self.rectContains(rect, pt1) and self.rectContains(
                    rect, pt2) and self.rectContains(rect, pt3):
                count = count + 1
                ind = []
                for j in xrange(0, 3):
                    for k in xrange(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]))

            pt = []

        return delaunayTri
コード例 #2
0
def calculateDelaunayTriangles(rect, points):
    # subdiv
    subdiv = cv2.Subdiv2D(rect)
    print("subdiv: ", subdiv)

    # 将点插入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])

        # 确保检测点是否在rect里面
        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)
            # 三个点组成一个三角形。 三角形数组对应于FaceMorph中的文件tri.txt
            if len(ind) == 3:
                delaunayTri.append((ind[0], ind[1], ind[2]))

        pt = []

    return delaunayTri
コード例 #3
0
def calculateDelaunayTriangles_subdiv(rect, points, img):
    subdiv = cv2.Subdiv2D(rect)
    for p in points:
        subdiv.insert(p)

    # imgToShow = np.copy(img)
    # draw_delaunay(imgToShow, subdiv)
    # showBGRimage(imgToShow)

    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 = []
            for j in xrange(0, 3):
                for k in xrange(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]))
            elif len(ind) > 3:
                ind = best_solution(ind, 3, 5)
                delaunayTri.append((ind[0], ind[1], ind[2]))
            else:
                logging.error('Insufficient points for making triangle')
        pt = []

    return delaunayTri
コード例 #4
0
ファイル: triangulate.py プロジェクト: emilyholt/face_morph
def delaunay_triangulation(img_height, img_width, avg_landmarks):
    
    '''
    Triangulation of the dest_img needs to have the same patterns of the triangulation 
    of the src_img, meaning the connection of the points has to be the same. After 
    the triangulation of the src_img, we use the indices of the landmark points in 
    the triangulation so we can replicate the same triangulation on the dest_img 
    '''

    # Make a landmark_coords list and a searchable landmark_coords_dict 
    landmark_coords = [(int(x[0]), int(x[1])) for x in avg_landmarks]
    landmark_coords_dict = {x[0]:x[1] for x in list(zip(landmark_coords, range(76)))}

    # Make a bounding rectangle
    rect = (0, 0, img_height, img_width)

    # Use OpenCV to subdivide the bounding rect 
    subdiv = cv2.Subdiv2D(rect);

    # Add landmark_coords to the subdiv
    for coord in landmark_coords :
        subdiv.insert(coord)
        
    # use the subdiv create the delaunay triangulation list
    subdiv_triangles = subdiv.getTriangleList();
    delaunay_triangles=[]

    for triang in subdiv_triangles:
        triang_coord1 = (int(triang[0]), int(triang[1]))
        triang_coord2 = (int(triang[2]), int(triang[3]))
        triang_coord3 = (int(triang[4]), int(triang[5]))
        
        if point_in_rect(rect, triang_coord1) and \
           point_in_rect(rect, triang_coord2) and \
           point_in_rect(rect, triang_coord3) :
            delaunay_triangles.append((landmark_coords_dict[triang_coord1], 
                                       landmark_coords_dict[triang_coord2], 
                                       landmark_coords_dict[triang_coord3]))
    
    return delaunay_triangles
コード例 #5
0
ファイル: mywarper.py プロジェクト: rahulm/ee239as-project
def calculateDelaunayTriangles(rect, points):
    #create subdiv
    subdiv = cv2.Subdiv2D(rect);
    
    # Insert points into subdiv
    for p in points:
        p1=(int(p[0]),int(p[1]))
        if p1[1]<=rect[2]-1 and p1[0]<=rect[2]-1 and p1[1]>=rect[0] and p1[0]>=rect[0]:
            subdiv.insert(p1) 
    
    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
コード例 #6
0
def calculate_delaunay_triangles(rect, points):
    # create subdiv
    subdiv = cv2.Subdiv2D(rect)

    # Insert points into subdiv
    for p in points:
        subdiv.insert((p[0], p[1]))

    triangle_list = subdiv.getTriangleList()

    delaunay_tri = []

    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_point(rect, pt1) and rect_contains_point(
                rect, pt2) and rect_contains_point(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)):
                        # if(np.logical_and((abs(pt[j][0] - points[k][0]) < 1.0), (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:
                delaunay_tri.append((ind[0], ind[1], ind[2]))

        pt = []

    return delaunay_tri
コード例 #7
0
def make_delaunay(f_w, f_h, theList):

    # Make a rectangle.
    rect = (0, 0, f_w, f_h)

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

    # Make a points list and a searchable dictionary.
    theList = theList
    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(f_w, f_h, subdiv, dictionary)

    # Return the list.
    return list4
コード例 #8
0
ファイル: utils.py プロジェクト: Jeret-Ljt/average_face
def calculateDelaunayTriangles(rect, points):
    subdiv = cv2.Subdiv2D(rect)  # Create subdiv
    #print(points)
    for p in points:
        subdiv.insert((p[0], p[1]))  # Insert points into subdiv

    triangleList = subdiv.getTriangleList(
    )  #get the information of triangle dividing
    delaunayTri = []  #find which point that the triangle point refers to

    for t in triangleList:
        ind = []
        for j in range(0, 3):
            for k in range(0, len(points)):
                if abs(t[2 * j] - points[k][0]) < 0.5 and abs(
                        t[2 * j + 1] - points[k][1]) < 0.5:
                    ind.append(k)
                    break
        if len(ind) == 3:
            delaunayTri.append(ind)

    return delaunayTri
コード例 #9
0
def barycentric_interp(lidar_image):
    rect = (0, 0, lidar_image.shape[1], lidar_image.shape[0])
    lidar_smooth_result = np.zeros(
        (lidar_image.shape[0], lidar_image.shape[1]), dtype=np.uint8)
    subdiv = cv2.Subdiv2D(rect)
    for i in range(0, lidar_image.shape[0]):
        index = i * lidar_image.shape[1]
        for j in range(0, lidar_image.shape[1]):
            if lidar_image[i][j] > 0:
                subdiv.insert([(j, i)])
            index += 1
    for i in range(0, lidar_image.shape[0]):
        for j in range(0, lidar_image.shape[1]):
            if lidar_image[i][j] > 0:
                lidar_smooth_result[i][j] = lidar_image[i][j]
            else:
                loc, edge, vertex = subdiv.locate((j, i))
                neighbor_point = []
                for k in range(0, 3):
                    neighbor_point.extend([
                        np.array(subdiv.getVertex(subdiv.edgeOrg(edge)[0])[0],
                                 dtype=np.int)
                    ])
                    edge = subdiv.getEdge(edge, cv2.Subdiv2D_NEXT_AROUND_LEFT)

                valid_points = np.full(3, fill_value=False, dtype=np.bool)

                for k in range(0, 3):
                    if 0 <= neighbor_point[k][0] < lidar_image.shape[1] and 0 <= neighbor_point[k][1] < \
                            lidar_image.shape[0]:
                        valid_points[k] = True

                if np.all(valid_points):
                    u, v, w = barycentric(np.array([j, i]), neighbor_point[0],
                                          neighbor_point[1], neighbor_point[2])
                    lidar_smooth_result[i][j] = lidar_image[neighbor_point[0][1]][neighbor_point[0][0]] * u + \
                                                lidar_image[neighbor_point[1][1]][neighbor_point[1][0]] * v + \
                                                lidar_image[neighbor_point[2][1]][neighbor_point[2][0]] * w
    return lidar_smooth_result
コード例 #10
0
def makeHeighmap(path, name, size, points, heights, tile):
    # bail if it doesnt look right
    total_samples = len(points)
    if total_samples != len(heights):
        print("Lengths don't match")
        return

    # convert mercator to pixels and map pixels to height values
    # bbox = getTileMercatorBoundingBox(tile[0], tile[1], tile[2])
    bbox = getBoundingBox(points)

    point_heights = {}
    for i in range(total_samples):
        x = int(remap(points[i][0], bbox[0], bbox[1], 0, size - 1))
        y = int(remap(points[i][1], bbox[2], bbox[3], size - 1, 0))
        point_heights[(x, y)] = heights[i]

    # subdivision from opencv, can do voronoi and its dual the delaunay triangulation
    subdiv = cv2.Subdiv2D((0, 0, size, size))
    for p in point_heights.iterkeys():
        subdiv.insert(p)
    (facets, centers) = subdiv.getVoronoiFacetList([])

    # an image where we will rasterize the voronoi cells
    image = numpy.zeros((size, size, 3), dtype = 'uint8')
    for i in xrange(0, len(facets)):
        ifacet_arr = []
        for f in facets[i]:
            ifacet_arr.append(f)
        ifacet = numpy.array(ifacet_arr, numpy.int)
        # the color is the height at the voronoi cite for this cell, offset to bring to unsigned 16bits
        height = point_heights[(centers[i][0], centers[i][1])] + 32768
        # to back them into a standard texture we split the high and low order bytes, note the order is G B R
        color = (int(math.floor(height % 255)), int(math.floor(height / 255) % 255), 0)
        # we exploit the fact that voronoi cells are convex polygons for faster rasterization
        cv2.fillConvexPoly(image, ifacet, color, cv2.CV_AA, 0)

    # we'll keep the result here
    cv2.imwrite(path + '/' + name + '.png', image)
コード例 #11
0
def _mesh_initializer(landmarks):
    """
    Get triangulation mesh:
        Call subdiv.getEdgelist() to obtain mesh edges
    :param landmarks: facial landmark numpy array
    :return:
    """
    if not isinstance(landmarks, np.ndarray):
        landmarks = face_utils.shape_to_np(landmarks)
    landmarks = landmarks.astype(np.int32)
    size = __output_shape
    landmarks_added = np.array(
        [[0, 0], [size[0] // 2, 0], [size[0] - 1, 0], [0, size[1] // 2],
         [0, size[1] - 1], [size[0] - 1, size[1] // 2],
         [size[0] // 2, size[1] - 1], [size[0] - 1, size[1] - 1]],
        dtype=np.int32)
    landmarks = np.append(landmarks, landmarks_added, axis=0).astype(np.int32)
    rect = (0, 0, size[1], size[0])
    subdiv = cv2.Subdiv2D(rect)
    for i in range(landmarks.shape[0]):
        subdiv.insert((landmarks[i][0], landmarks[i][1]))
    return subdiv
コード例 #12
0
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]) < 1e-7 and abs(
                            pt[j][1] - points[k][1]) < 1e-7:
                        ind.append(k)
                        break
            if len(ind) == 3:
                triangle.append((ind[0], ind[1], ind[2]))
            else:
                print('errors:', len(ind))

        pt = []

    return triangle
コード例 #13
0
def calculate_delaunay(img,points):
    rect = (0,0,img.shape[1],img.shape[0])
    subdiv = cv2.Subdiv2D(rect)  
    print(img.shape)
    for p in points:
        subdiv.insert(p)

    triangles = subdiv.getTriangleList()

    indexes = []
    for tri in triangles:
        pt1 = np.float32([tri[0],tri[1]])
        pt2 = np.float32([tri[2],tri[3]])
        pt3 = np.float32([tri[4],tri[5]])

        index1 = np.argmin(np.sqrt(((np.float32(points) - pt1)**2).sum(axis = 1)),axis = 0)
        index2 = np.argmin(np.sqrt(((np.float32(points) - pt2)**2).sum(axis = 1)),axis = 0)
        index3 = np.argmin(np.sqrt(((np.float32(points) - pt3)**2).sum(axis = 1)),axis = 0)

        indexes.append((index1,index2,index3))

    return indexes
コード例 #14
0
def getTriangles(mesh, imageShape):
    rect = (0, 0, imageShape[0], imageShape[1])
    subdiv = cv2.Subdiv2D(rect)
    subdiv.insert(list(mesh))
    triangles = subdiv.getTriangleList()
    filteredIndexTriangles = []
    for triangle in triangles:
        if (rect[0] <= triangle[0] < rect[2]
                and rect[0] <= triangle[2] < rect[2]
                and rect[0] <= triangle[4] < rect[2]
                and rect[1] <= triangle[1] < rect[3]
                and rect[1] <= triangle[3] < rect[3]
                and rect[1] <= triangle[5] < rect[3]):
            cornerIndices = []
            for index in range(0, 6, 2):
                cornerIndices.append(
                    np.where(
                        (mesh == [triangle[index],
                                  triangle[index + 1]]).all(axis=1))[0][0])
            filteredIndexTriangles.append(cornerIndices)
    filteredIndexTriangles = np.array(filteredIndexTriangles)
    return filteredIndexTriangles
コード例 #15
0
    def get_deluanay_triangles_from_landmarks(self, landmarks, bounds):
        self.say("Getting Deluanay triangles from landmarks... ", "")
        subdiv2d = cv2.Subdiv2D(bounds)
        for landmark in landmarks:
            x, y = landmark
            subdiv2d.insert((x, y))

        triangles = subdiv2d.getTriangleList()

        deluanay_triangles = []
        for triangle in triangles:
            x1, y1, x2, y2, x3, y3 = triangle
            point1 = (x1, y1)
            point2 = (x2, y2)
            point3 = (x3, y3)

            if self.__is_point_in_bounds(point1, bounds) and \
                    self.__is_point_in_bounds(point2, bounds) and \
                    self.__is_point_in_bounds(point3, bounds):
                deluanay_triangles.append((point1, point2, point3))
        self.say("done")
        return deluanay_triangles
コード例 #16
0
 def _triangulate(self) -> None:
     # rect (tuple) - "xmin", "ymin", "w", "h"
     rect = cv2.boundingRect(np.array(self._points))
     subdiv = cv2.Subdiv2D(rect)
     for pt in self._points:
         subdiv.insert(pt)
     self._trianglar_grid = []
     # trangules
     trangules = subdiv.getTriangleList()
     for tri in trangules:
         # vertices of a triangule
         tri = [
             (tri[0], tri[1]), (tri[2], tri[3]), (tri[4], tri[5])
         ]
         # point indices of a trangule
         tri_pts = []
         for v in tri:
             # go through all points
             for idx, pt in enumerate(self._points):
                 if (abs(v[0] - pt[0]) < 1) and (abs(v[1] - pt[1]) < 1):
                     tri_pts.append(idx)
         self._trianglar_grid.append(tri_pts)
コード例 #17
0
def calculateDelaunayTriangles(rect, points):
    # Create subdiv
    subdiv = cv2.Subdiv2D(rect)

    # Insert points into subdiv
    for p in points:
        subdiv.insert((p[0], p[1]))

    #subdiv.insert([(p[0], p[1]) for p in points])

    # List of triangles. Each triangle is a list of 3 points ( 6 numbers )
    triangleList = subdiv.getTriangleList()

    # Find the indices of triangles in the points array

    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
コード例 #18
0
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)

            if len(ind) == 3:
                delaunayTri.append((ind[0], ind[1], ind[2]))

        pt = []

    return delaunayTri
コード例 #19
0
def delaunay(size, points, removeOutlier=False):
    triList = []
    rect = (0, 0, size[1], size[0])
    subdivOriginal = cv2.Subdiv2D(rect)
    for p in points:
        subdivOriginal.insert(p)
    triangleList = subdivOriginal.getTriangleList()
    triangleList = triangleList.astype(np.int32)
    if removeOutlier == True:
        for triangle in triangleList:
            pt1 = (triangle[0], triangle[1])
            pt2 = (triangle[2], triangle[3])
            pt3 = (triangle[4], triangle[5])
            if rect_contains(rect, pt1) and rect_contains(rect, pt2) and rect_contains(rect, pt3):
                triList.append((pt1, pt2, pt3))
    else:
        for triangle in triangleList:
            pt1 = (triangle[0], triangle[1])
            pt2 = (triangle[2], triangle[3])
            pt3 = (triangle[4], triangle[5])
            triList.append((pt1, pt2, pt3))
    return triList
コード例 #20
0
    def construct_triangulation(img, points):
        rect = (0, 0, img.shape[1], img.shape[0])
        sub_div = cv2.Subdiv2D(rect)
        bound_points = [
            ShapeEngine.get_bound_point(img, -(i + 1)) for i in range(8)
        ]
        for point in bound_points:
            sub_div.insert(point)
        for p in points:
            sub_div.insert(p)

        triangle_list = sub_div.getTriangleList()
        triangles = []
        bound_triangles = []
        for t in triangle_list:
            pt = [(t[0], t[1]), (t[2], t[3]), (t[4], t[5])]
            ind = []
            for j in range(3):
                idx = None
                for k in range(len(points)):
                    if abs(pt[j][0] - points[k][0]) < 1e-6 and abs(
                            pt[j][1] - points[k][1]) < 1e-6:
                        idx = k
                        break
                if idx is None:
                    for k in range(len(bound_points)):
                        if abs(pt[j][0] - bound_points[k][0]) < 1e-6 and abs(
                                pt[j][1] - bound_points[k][1]) < 1e-6:
                            idx = -(k + 1)
                            break
                if idx is None:
                    continue
                ind.append(idx)
            if len(ind) == 3:
                if all(map(lambda x: x >= 0, ind)):
                    triangles.append(ind)
                else:
                    bound_triangles.append(ind)
        return triangles, bound_triangles
コード例 #21
0
def triangulation(land_points, img):
    points = np.array(land_points, np.int32)
    chull = cv2.convexHull(points)
    rect = cv2.boundingRect(chull)
    rectangle = np.asarray(rect)
    subdiv = cv2.Subdiv2D(rect)
    p_list = []
    for p in land_points:
        p_list.append((p[0], p[1]))
    for p in p_list:
        subdiv.insert(p)
    triangles = subdiv.getTriangleList()
    triangles = np.array(triangles, dtype=np.int32)
    vert = []
    VPoint = []
    pt = []
    for t in triangles:
        pt.append((t[0], t[1]))
        pt.append((t[2], t[3]))
        pt.append((t[4], t[5]))
        temp = []

        for i in range(3):
            for j in range(len(points)):
                if (abs(pt[i][0] - points[j][0]) < 1.0
                        and abs(pt[i][1] - points[j][1]) < 1.0):
                    temp.append(j)
        if len(temp) == 3:
            vert.append((points[temp[0]], points[temp[1]], points[temp[2]]))
            VPoint.append((temp[0], temp[1], temp[2]))
        pt = []

        #cv2.line(img, tuple(points[temp[0]]), tuple(points[temp[1]]), (0, 0, 255), 2)
        #cv2.line(img, tuple(points[temp[1]]), tuple(points[temp[2]]), (0, 0, 255), 2)
        #cv2.line(img, tuple(points[temp[0]]), tuple(points[temp[2]]), (0, 0, 255), 2)
    vert = np.asarray(vert)
    VPoint = np.asarray(VPoint)
    chull = np.reshape(chull, (chull.shape[0], chull.shape[2]))
    return img, vert, VPoint, chull
コード例 #22
0
def getDelaunayTriangles(points):
    #获取点的边界
    convexHullPoints = getConvexHullPoints(points)
    #print convexHullPoints
    rec = cv2.boundingRect(np.int32([convexHullPoints]))
    #print rec
    subdiv = cv2.Subdiv2D((0, 0, rec[0] + rec[2], rec[1] + rec[3]))
    for point in points:
        #print point,rec,(0, 0, rec[0]+rec[2],rec[1]+rec[3])
        subdiv.insert((point[0], point[1]))

    triangles = subdiv.getTriangleList()
    validTriangles = []
    for triangle in triangles:
        pt1 = (triangle[0], triangle[1])
        pt2 = (triangle[2], triangle[3])
        pt3 = (triangle[4], triangle[5])
        if pt1 not in points or pt2 not in points or pt3 not in points:
            continue
        validTriangles.append(np.int32([pt1, pt2, pt3]))
        #print np.int32([pt1,pt2,pt3])
    return np.int32(validTriangles)
コード例 #23
0
def measure_triangle(image, points):  #(src_img, morph_points)
    #    print('measure_triangle===')
    rect = (0, 0, image.shape[1], image.shape[0])

    sub_div = cv2.Subdiv2D(rect)

    for p in points:
        sub_div.insert(p)
        # print('p',image.shape,p)
        # sub_div.insert((p[1],p[0]))

    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
コード例 #24
0
    def _delaunay_triangulation(self):
        """
        Delaunay triangulation
            https://blog.csdn.net/zhaoyin214/article/details/87942919
        """
        # rect (tuple) - "xmin", "ymin", "w", "h"
        rect = cv2.boundingRect(self._points.values)

        subdiv = cv2.Subdiv2D(rect)

        for _, pt in self._points.iterrows():
            subdiv.insert(tuple(pt))

        # index of points consist of a trangular
        triangulation_pt_indices = []

        triangule_list = subdiv.getTriangleList()

        for tri in triangule_list:

            # vertices of a triangule
            tri_vertices = [(tri[0], tri[1]), (tri[2], tri[3]),
                            (tri[4], tri[5])]

            # index of points consist of a trangular
            tri_pt_idx_list = []
            for vertex in tri_vertices:
                # traverse all points
                for pt_idx, pt in self._points.iterrows():

                    if (abs(vertex[0] - pt.x) < 1) and \
                        (abs(vertex[1] - pt.y) < 1):
                        tri_pt_idx_list.append(pt_idx)

            triangulation_pt_indices.append(tri_pt_idx_list)

        self._triangulation_pt_indices = pd.DataFrame(
            triangulation_pt_indices, columns=TRIANGULATION_COLUMES)
コード例 #25
0
    def voronoi(self, img, landmark_dict, colors=None, remove_bg=False):
        if remove_bg:
            img_copy = np.full(img.shape, 255, dtype=np.uint8)
        else:
            img_copy = img.copy()  #Copy image to not affect the original one

        bg = None

        for landmark in landmark_dict:
            landmark_points = landmark_dict[landmark]
            hull_pts = np.int32(landmark_points)

            size = img.shape
            rect = (0, 0, size[1], size[0])

            #Create subdiv with the whole image
            subdiv = cv2.Subdiv2D(rect)
            subdiv.insert(landmark_points)

            (facets, centers) = subdiv.getVoronoiFacetList([])
            k = len(colors)

            for i in range(len(facets)):
                if colors:
                    (rb, rg, rr) = colors[i % k]
                else:
                    (rb, rg, rr) = self.pick_random_colors(1)[0]

                ith_facet = list()

                for f in facets[i]:
                    ith_facet.append(f)

                ith_facet = np.array(ith_facet, np.int)

                cv2.fillConvexPoly(img_copy, ith_facet, (rb, rg, rr))

        return img_copy
コード例 #26
0
def voronoi_from_pixels(pixels, dimensions, pixelsOfInterest):
    # Create the subdiv which will hold the voronoi information
    rect = (0, 0, dimensions[0], dimensions[1])
    subdiv = cv2.Subdiv2D(rect)
    for pixel in pixelsOfInterest:
        p = pixel[0], pixel[1]
        subdiv.insert(p)

    # Allocate space for Voronoi Diagram
    # img_voronoi should have shape (height, width, channels)
    img_voronoi = np.zeros(shape=(HEIGHT, WIDTH, 3), dtype=np.uint8)

    # Draw Voronoi diagram

    (facets, centers) = subdiv.getVoronoiFacetList([])
    facetCount = len(facets)
    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)

        p = centers[i]
        y = int(p[1])
        x = int(p[0])

        rgb = stateRepresentation.get_rgb_pixel_from_frame(pixels, x, y)
        # Get BGR color
        color = [rgb[2], rgb[1], rgb[0]]

        cv2.fillConvexPoly(img_voronoi, ifacet, color, cv2.LINE_AA, 0)
        ifacets = np.array([ifacet])
        cv2.polylines(img_voronoi, ifacets, True, (0, 0, 0), 1, cv2.LINE_AA, 0)

    s = img_voronoi.shape
    l = len(img_voronoi)
    return img_voronoi
コード例 #27
0
def drawTriangles(fileName, imageType):
    image = cv2.imread(fileName)

    if ("cat" in imageType):
        imageMarked, imagePts = tempPtsToCatImage(fileName)
    else:
        imageMarked, imagePts = markTheHumanFace(fileName)

    subdiv = cv2.Subdiv2D((0, 0, image.shape[1], image.shape[0]))

    for i in range(68):
        subdiv.insert(imagePts[i])

    height = image.shape[0]
    width = image.shape[1]

    subdiv.insert((0, 0))
    subdiv.insert((width / 2, 0))
    subdiv.insert((width - 1, 0))
    subdiv.insert((width - 1, height / 2))
    subdiv.insert((width - 1, height - 1))
    subdiv.insert((width / 2, height - 1))
    subdiv.insert((0, height - 1))
    subdiv.insert((0, height / 2))

    triangles = subdiv.getTriangleList()
    colorGreen = (0, 255, 0)

    for i in range(len(triangles)):
        sel_triangle = triangles[i].astype(np.int)
        cv2.line(image, (sel_triangle[0], sel_triangle[1]),
                 (sel_triangle[2], sel_triangle[3]), colorGreen)
        cv2.line(image, (sel_triangle[0], sel_triangle[1]),
                 (sel_triangle[4], sel_triangle[5]), colorGreen)
        cv2.line(image, (sel_triangle[2], sel_triangle[3]),
                 (sel_triangle[4], sel_triangle[5]), colorGreen)

    return image
コード例 #28
0
ファイル: faceMorph.py プロジェクト: hmc-fly-f18/FaceMorph
def makeDelaunay(image, listOfPoints):
    '''the input is an image file read in by opencv and a list of average points
    the output is the final triangulation points in a list of tuples
    '''
    size = image.shape
    rect = (0, 0, size[0], size[0])
    # Create an instance of Subdiv2D.
    subdiv = cv2.Subdiv2D(rect)

    # Make a points list and a searchable dictionary.
    theList = listOfPoints
    points = [(int(x[0]), int(x[1]))
              for x in theList]  #making a tuple of points

    #the following line might still need some testing
    dictionary = {x[0]: x[1] for x in list(zip(points, range(len(points))))}

    # Insert points into subdiv
    for p in points:
        subdiv.insert(p)

    # Make a delaunay triangulation list.
    return draw_delaunay(subdiv, dictionary, rect)
コード例 #29
0
def generate_voronoi_diagram(img):
    if (type_grid == "random"):
        points = np.random.rand(num_cells, 2)
        points = scale_points(points)
        keyPoints = np.array(points)
    elif (type_grid == "const"):
        brief = cv2.xfeatures2d.SIFT_create(num_cells)
        kp = brief.detect(img, None)
        keyPoints = cv2.KeyPoint_convert(kp)

    points = []
    for keyPoint in keyPoints:
        points.append((keyPoint[0], keyPoint[1]))

    size = img.shape
    print(size)
    subdiv2DShape = (0, 0, size[1], size[0])
    subdiv = cv2.Subdiv2D(subdiv2DShape)

    for p in points:
        subdiv.insert(p)

    return subdiv
コード例 #30
0
def delaunaryTriangles(rect,points):
    #print(points,rect)
    subdiv = cv2.Subdiv2D(rect)
    pointsDict = {}
    #Get the triangle list 
    for i,p in enumerate(points) :

        subdiv.insert((int(p[0]),int(p[1])))
        pointsDict[(int(p[0]),int(p[1]))] = i 
    triangleList = subdiv.getTriangleList();
    
    triangleIndList = []
    #Get the index of the  triangle points
    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 pt1 in pointsDict and pt2 in pointsDict and pt3 in pointsDict:
            triangleIndList.append([pointsDict[pt1],pointsDict[pt2],pointsDict[pt3]])
    
    return triangleIndList