def reverse_points(self): """ If orientation is clockwise, convert to counter-clockwise. """ sc = constants.CLIPPER_SCALE pts = st(self.points, sc) if pyclipper.Orientation(pts) is False: reverse_poly = pyclipper.ReversePath(pts) solution = pyclipper.SimplifyPolygon(reverse_poly) else: solution = pyclipper.SimplifyPolygon(pts) self.points = sf(solution, sc)[0] return self
def reverse_points(pts): """ If orientation is clockwise, convert to counter-clockwise. """ points = [] sc = constants.CLIPPER_SCALE for poly in st(pts, sc): if pyclipper.Orientation(poly) is False: reverse_poly = pyclipper.ReversePath(poly) solution = pyclipper.SimplifyPolygon(reverse_poly) else: solution = pyclipper.SimplifyPolygon(poly) points.extend(solution) return points
def clean_polygon(self, polygon): """清多边形 Args: polygon ([type]): [description] Returns: [type]: [description] """ simple = pyclipper.SimplifyPolygon(polygon, pyclipper.PFT_NONZERO) if simple is None or len(simple) == 0: return None biggest = simple[0] biggest_area = pyclipper.Area(biggest) for i in range(1, len(simple)): area = abs(pyclipper.Area(simple[i])) if area > biggest_area: biggest = simple[i] biggest_area = area clean = pyclipper.CleanPolygon(biggest, self.config['curveTolerance']) if clean is None or len(clean) == 0: return None return clean
def create_merged_points(self): """ """ from spira.gdsii.utils import scale_polygon_up as spu from spira.gdsii.utils import scale_polygon_down as spd polygons = spu(self.points) self.points = [] for poly in polygons: if pyclipper.Orientation(poly) is False: reverse_poly = pyclipper.ReversePath(poly) solution = pyclipper.SimplifyPolygon(reverse_poly) else: solution = pyclipper.SimplifyPolygon(poly) for sol in solution: self.points.append(sol) self.points = bool_operation(subj=self.points, method='union') self.points = spd(self.points) return self
def simplify(points): """ Parameters ---------- pol_points : list(list(coord_x, coord_y)) list of points of WSP Returns ------- list(new_polygon_1, ...), where new_polygon_i : list(list(coord_x, coord_y)) """ return pyclipper.SimplifyPolygon(points)
def create_polys(points): x_vec = points[:, 0] y_vec = points[:, 1] points_list = np.column_stack((x_vec, y_vec)) points_list = (tuple(map(tuple, points_list)), ) points_list = pyclipper.scale_to_clipper(points_list) [[[int(i) for i in j] for j in k] for k in points_list] simple_poly = pyclipper.SimplifyPolygon(points_list[0]) simple_poly = pyclipper.scale_from_clipper(simple_poly) #simple_poly = np.asarray(simple_poly) return simple_poly
def clean_polygon(self, polygon): simple = pyclipper.SimplifyPolygon(polygon, pyclipper.PFT_NONZERO) if simple is None or len(simple) == 0: return None biggest = simple[0] biggest_area = pyclipper.Area( biggest) # 给出端点,求多边形面积,端点顺序一定要是逆时针的,否则结果为负 for i in range(1, len(simple)): area = abs(pyclipper.Area(simple[i])) if area > biggest_area: biggest = simple[i] biggest_area = area clean = pyclipper.CleanPolygon(biggest, self.config['curveTolerance']) if clean is None or len(clean) == 0: return None return clean
def test_simplify_polygons(self): solution = pyclipper.SimplifyPolygons([PATH_SUBJ_1]) solution_single = pyclipper.SimplifyPolygon(PATH_SUBJ_1) self.assertEqual(len(solution), 1) self.assertEqual(len(solution), len(solution_single)) _do_solutions_match(solution, solution_single)
def test_simplify_polygon(self): solution = pyclipper.SimplifyPolygon(PATH_SUBJ_1) self.assertEqual(len(solution), 1)
def assign_path_to_region(out, regions): trimmed_polys = e2e_postprocessing.get_trimmed_polygons(out) polys = [] for t in trimmed_polys: p = t[:, :2, 0].tolist() + t[::-1, :2, 1].tolist() polys.append(p) scores = [] for i, r in enumerate(regions): region_poly = r['bounding_poly'] scores_i = [] scores.append(scores_i) for p in polys: pc = pyclipper.Pyclipper() try: pc.AddPath(p, pyclipper.PT_CLIP, True) except: scores_i.append(0) # print p print("Failed to assign text line, probably not an issue") continue pc.AddPath(region_poly, pyclipper.PT_SUBJECT, True) solution = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO) # pts = np.array(region_poly, np.int32) # pts = pts.reshape((-1,1,2)) # cv2.polylines(img,[pts],True,(0,0,255), thickness=3) area = 0 for path in solution: area += pyclipper.Area(path) scores_i.append(area) background_scores = [] total_areas = [] for p in polys: pc = pyclipper.Pyclipper() try: pc.AddPath(p, pyclipper.PT_CLIP, True) except: total_areas.append(np.inf) background_scores.append(np.inf) # print p print("Failed to assign text line, probably not an issue") continue pc.AddPaths([r['bounding_poly'] for r in regions], pyclipper.PT_SUBJECT, True) solution = pc.Execute(pyclipper.CT_INTERSECTION, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO) area = 0 for path in solution: area += pyclipper.Area(path) simple_path = pyclipper.SimplifyPolygon(p, pyclipper.PFT_NONZERO) total_area = 0 for path in simple_path: total_area += pyclipper.Area(path) total_areas.append(total_area) background_score = total_area - area background_scores.append(background_score) return np.array(scores), np.array(background_scores), np.array(total_areas)
def lf_non_max_suppression_area(lf_xy_positions, confidences, overlap_range, overlap_thresh): # lf_xy_positions = np.concatenate([l.data.cpu().numpy()[None,...] for l in lf_xy_positions]) # lf_xy_positions = lf_xy_positions[:,:,:2,:2] # print lf_xy_positions # raw_input() lf_xy_positions = [l[:, :2, :2] for l in lf_xy_positions] #this assumes equal length positions # lf_xy_positions = np.concatenate([l[None,...] for l in lf_xy_positions]) # lf_xy_positions = lf_xy_positions[:,:,:2,:2] c = confidences bboxes = [] center_lines = [] scales = [] for i in range(len(lf_xy_positions)): pts = lf_xy_positions[i] # for i in xrange(lf_xy_positions.shape[1]): # pts = lf_xy_positions[:,i,:] if overlap_range is not None: pts = pts[overlap_range[0]:overlap_range[1]] f = pts[0] delta = f[:, 0] - f[:, 1] scale = np.sqrt((delta**2).sum()) scales.append(scale) # ls = pts[:,:,0].tolist() + pts[:,:,1][::-1].tolist() # ls = [[int(x[0]), int(x[1])] for x in ls] # poly_regions.append(ls) center_lines.append((pts[:, :, 0] + pts[:, :, 1]) / 2.0) min_x = pts[:, 0].min() max_x = pts[:, 0].max() min_y = pts[:, 1].min() max_y = pts[:, 1].max() bboxes.append((min_x, min_y, max_x, max_y)) bboxes = np.array(bboxes) if len(bboxes.shape) < 2: return [] x1 = bboxes[:, 0] y1 = bboxes[:, 1] x2 = bboxes[:, 2] y2 = bboxes[:, 3] area = (x2 - x1 + 1) * (y2 - y1 + 1) idxs = np.argsort(c) overlapping_regions = [] pick = [] while len(idxs) > 0: last = len(idxs) - 1 i = idxs[last] pick.append(i) xx1 = np.maximum(x1[i], x1[idxs[:last]]) yy1 = np.maximum(y1[i], y1[idxs[:last]]) xx2 = np.minimum(x2[i], x2[idxs[:last]]) yy2 = np.minimum(y2[i], y2[idxs[:last]]) # compute the width and height of the bounding box w = np.maximum(0, xx2 - xx1 + 1) h = np.maximum(0, yy2 - yy1 + 1) # compute the ratio of overlap overlap_bb = (w * h) / area[idxs[:last]] overlap = [] for step, j in enumerate(idxs[:last]): #Skip anything that does't actually have any overlap if overlap_bb[step] < 0.1: overlap.append(0) continue path0 = center_lines[i] path1 = center_lines[j] path = np.concatenate([path0, path1[::-1]]) path = [[int(x[0]), int(x[1])] for x in path] expected_scale = (scales[i] + scales[j]) / 2.0 one_off_area = expected_scale**2 * (path0.shape[0] + path1.shape[0]) / 2.0 simple_path = pyclipper.SimplifyPolygon(path, pyclipper.PFT_NONZERO) inter_area = 0 for path in simple_path: inter_area += abs(pyclipper.Area(path)) area_ratio = inter_area / one_off_area area_ratio = 1.0 - area_ratio overlap.append(area_ratio) overlap = np.array(overlap) to_delete = np.concatenate( ([last], np.where(overlap > overlap_thresh)[0])) idxs = np.delete(idxs, to_delete) return pick