我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用cv2.Subdiv2D()。
def draw_polyline(im, landmarks): if faceOnly: print("faceOnly on") im[0:screenheight] = 0.0 pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32) pts = pts.reshape((-1, 1, 2)) if len(landmarks): bottomRight = (max(landmarks[0][:, 0]), max(landmarks[0][:, 1])) topLeft = (min(landmarks[0][:, 0]), min(landmarks[0][:, 1])) rect = (0, 0, im.shape[1], im.shape[0]) # FIXME: Delaunay triangulation. # subdiv = cv2.Subdiv2D(rect) for landmark in landmarks: for group in OVERLAY_GROUPS: ftrpoints = [landmark[group]] cv2.polylines(im, ftrpoints, False, (0, 255, 0), 1, 8) # FIXME: Delaunay triangulation. # for pt in ftrpoints: # subdiv.insert(pt) # FIXME: Delaunay triangulation. # if len(landmarks): # draw_delaunay( im, subdiv, (255,255,2555)) return im
def calculateDelaunayTriangles(rect, points): # Create subdiv subdiv = cv2.Subdiv2D(rect) # Insert points into subdiv for p in points: subdiv.insert((p[0], p[1])) # 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 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])) return delaunayTri
def __init__(self, mask, imsizex, imsizey, dpi=100): # convert mask to x,y points points = map(list, zip(*reversed(map(list, np.where(mask))))) # make subdivision rect = (0, 0, imsizex, imsizey) self.subdiv = cv2.Subdiv2D(rect) self.subdiv.insert(points) # initialize figure self.fig = plt.figure("Image Stream", figsize=(imsizex / dpi, imsizey / dpi), dpi=dpi) # initialize ax self.ax = self.fig.add_axes([0, 0, 1, 1], frame_on=False) self.ax.xaxis.set_visible(False) self.ax.yaxis.set_visible(False) # initialize image self.im = self.ax.imshow(np.zeros((imsizey, imsizex)), interpolation='none', animated=True) self.image = None # start figure self.fig.show() self.fig.canvas.draw()
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 # Warps and alpha blends triangular regions from img1 and img2 to img
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 = [] 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 rectContains(rect, pt1) and rectContains(rect, pt2) and 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 # Warps and alpha blends triangular regions from img1 and img2 to img