def get_delaunay(df, with_tri=False): graph = nx.Graph() tri = Delaunay(df[["x", "y"]]) edges = set() for n in range(tri.nsimplex): edge = sorted([tri.simplices[n, 0], tri.simplices[n, 1]]) edges.add( ( edge[0], edge[1], euclidean( (df.loc[tri.simplices[n, 0], ["x", "y"]]), (df.loc[tri.simplices[n, 1], ["x", "y"]]), ), ) ) edge = sorted([tri.simplices[n, 0], tri.simplices[n, 2]]) edges.add( ( edge[0], edge[1], euclidean( (df.loc[tri.simplices[n, 0], ["x", "y"]]), (df.loc[tri.simplices[n, 2], ["x", "y"]]), ), ) ) edge = sorted([tri.simplices[n, 1], tri.simplices[n, 2]]) edges.add( ( edge[0], edge[1], euclidean( (df.loc[tri.simplices[n, 1], ["x", "y"]]), (df.loc[tri.simplices[n, 2], ["x", "y"]]), ), ) ) graph.add_weighted_edges_from(edges) if with_tri: tri.close() return graph, tri return graph
def __warp(image, points, size): """ Warp an image using the provided point locations. """ rows, cols = size[:2] # perform triangulation on the control points delaunay = Delaunay(points) triangles = delaunay.simplices output = np.zeros((rows, cols, 3), np.uint8) grid = np.vstack(np.unravel_index(xrange(0, rows * cols), size)).T membership = delaunay.find_simplex(grid) for triangle_id in range(len(triangles)): triangle = triangles[triangle_id] # Transformation matrix from output triangle to source triangle src = np.vstack((image['points'][triangle, :].T, [1, 1, 1])) dst = np.vstack((points[triangle, :].T, [1, 1, 1])) mat = np.dot(src, np.linalg.inv(dst))[:2, :] # Co-ordinates into output image x, y = grid[membership == triangle_id].T # Co-ordinates into source image a, b = np.dot(mat, np.vstack((x, y, np.ones(len(x))))) # Interpolate from source image to fill in the output image values output[y, x, 0] = np.clip(image['interp_red'](b, a, grid=False), 0, 255) output[y, x, 1] = np.clip(image['interp_green'](b, a, grid=False), 0, 255) output[y, x, 2] = np.clip(image['interp_blue'](b, a, grid=False), 0, 255) delaunay.close() return output
def __warp(image, points, size): """ Warp an image using the provided point locations. """ rows, cols = size[:2] # perform triangulation on the control points delaunay = Delaunay(points) triangles = delaunay.simplices output = np.zeros((rows, cols, 3), np.uint8) grid = np.vstack(np.unravel_index(xrange(0, rows*cols), size)).T membership = delaunay.find_simplex(grid) for triangle_id in range(len(triangles)): triangle = triangles[triangle_id] # Transformation matrix from output triangle to source triangle src = np.vstack((image['points'][triangle, :].T, [1, 1, 1])) dst = np.vstack((points[triangle, :].T, [1, 1, 1])) mat = np.dot(src, np.linalg.inv(dst))[:2, :] # Co-ordinates into output image x, y = grid[membership == triangle_id].T # Co-ordinates into source image a, b = np.dot(mat, np.vstack((x, y, np.ones(len(x))))) # Interpolate from source image to fill in the output image values output[y, x, 0] = np.clip(image['interp_red'](b, a, grid=False), 0, 255) output[y, x, 1] = np.clip(image['interp_green'](b, a, grid=False), 0, 255) output[y, x, 2] = np.clip(image['interp_blue'](b, a, grid=False), 0, 255) delaunay.close() return output
tri = Delaunay(points) tets = tri.points[tri.simplices] #vol = tetrahedron_volume(tets[:, 0], tets[:, 1], # tets[:, 2], tets[:, 3]) vor = Voronoi(points) print("finding all neighbours") neighbours = find_neighbours(tri) sys.stdout.write(CLR_LINE) print("creating convex hull") convex_hull = create_convex_hull_list(tri.convex_hull) sys.stdout.write(CLR_LINE) tri.close() print("length of convex hull: ", len(convex_hull)) print("Delaunay: done") num_cols = len(data_ids) l_max = 2 * 100 * u.au file = open(output_file, "wb") file.write(struct.pack("H", grid_id)) file.write(struct.pack("H", num_cols)) for d_ids in data_ids: file.write(struct.pack("H", d_ids)) n_neighbours = []
def get_triangulation(im, gray_image, a=50, b=55, c=0.15, show=False, randomize=False): '''Returns triangulations''' # Using canny edge detection. # # Reference: http://docs.opencv.org/3.1.0/da/d22/tutorial_py_canny.html # First argument: Input image # Second argument: minVal (argument 'a') # Third argument: maxVal (argument 'b') # # 'minVal' and 'maxVal' are used in the Hysterisis Thresholding step. # Any edges with intensity gradient more than maxVal are sure to be edges # and those below minVal are sure to be non-edges, so discarded. Those who # lie between these two thresholds are classified edges or non-edges based # on their connectivity. detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor_path) edges = cv2.Canny(gray_image, a, b) if show: cv2.imshow('Canny', edges) cv2.waitKey(0) cv2.destroyAllWindows() win = dlib.image_window() # Set number of points for low-poly edge vertices num_points = int(np.where(edges)[0].size * c) # Return the indices of the elements that are non-zero. # 'nonzero' returns a tuple of arrays, one for each dimension of a, # containing the indices of the non-zero elements in that dimension. # So, r consists of row indices of non-zero elements, and c column indices. r, c = np.nonzero(edges) # r.shape, here, returns the count of all points that belong to an edge. # So 'np.zeros(r.shape)' an array of this size, with all zeros. # 'rnd' is thus an array of this size, with all values as 'False'. rnd = np.zeros(r.shape) == 1 # Mark indices from beginning to 'num_points - 1' as True. rnd[:num_points] = True # Shuffle np.random.shuffle(rnd) # Randomly select 'num_points' of points from the set of all edge vertices. r = r[rnd] c = c[rnd] # Number of rows and columns in image sz = im.shape r_max = sz[0] c_max = sz[1] # Co-ordinates of all randomly chosen points pts = np.vstack([r, c]).T if randomize: rand_offset = 50 rand_dirs = [(0, rand_offset), (-rand_offset, 0), (0, -rand_offset), (rand_offset, 0)] rnd_count = 0 for point in pts: if random.random() < 0.3: rnd_count += 1 rand_dir = random.randint(0, 3) point[0] += rand_dirs[rand_dir][0] point[1] += rand_dirs[rand_dir][1] # Append (0,0) to the vertical stack pts = np.vstack([pts, [0, 0]]) # Append (0,c_max) to the vertical stack pts = np.vstack([pts, [0, c_max]]) # Append (r_max,0) to the vertical stack pts = np.vstack([pts, [r_max, 0]]) # Append (r_max,c_max) to the vertical stack pts = np.vstack([pts, [r_max, c_max]]) # Append some random points to fill empty spaces pts = np.vstack([pts, np.random.randint(0, 750, size=(100, 2))]) # print(len(pts)) # pts = my_reduce(pts, 5) # print(len(pts)) dets = detector(im, 1) # print("Number of faces detected: {}".format(len(dets))) if show: win.clear_overlay() win.set_image(im) for k, d in enumerate(dets): shape = predictor(im, d) for i in range(shape.num_parts): pts = np.vstack([pts, [shape.part(i).x, shape.part(i).y]]) if show: win.add_overlay(shape) if show: win.add_overlay(dets) dlib.hit_enter_to_continue() # Construct Delaunay Triangulation from these set of points. # Reference: https://en.wikipedia.org/wiki/Delaunay_triangulation tris = Delaunay(pts, incremental=True) # tris_vertices = pts[tris.simplices] # for tri in range(tris_vertices.shape[0]): # x_coords = [] # y_coords = [] # print(tris_vertices[tri]) # for coord in range(tris_vertices.shape[1]): # x_coords.append(tris_vertices[tri][coord][0]) # y_coords.append(tris_vertices[tri][coord][1]) # divideHighVariance(tris, im) tris.close() # exit(0) # Return triangulation return tris