def is_valid(self): """Performs semantic check on the quad. return true if valid""" for corner in self.corners: if corner.x < self.__coord_bounds.x_min or \ corner.x > self.__coord_bounds.x_max or \ corner.y < self.__coord_bounds.y_min or \ corner.y > self.__coord_bounds.y_max: return False if geom.line_segment_intersect(self.corners[0], self.corners[1], self.corners[2], self.corners[3]): return False end_segment_dist = geom.line_line_distance(self.corners[0], self.corners[1], self.corners[2], self.corners[3]) if end_segment_dist * 6 < self.get_base_length(): return False end_dist = geom.distance(self.corners[0], self.corners[3]) if end_dist * 6 < self.get_base_length(): return False return True
def _set_bounding_box_size(self): contours = cv2.findContours(self._working_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) min_rect = cv2.minAreaRect(contours[1][0]) box = cv2.boxPoints(min_rect) dist = [geom.distance(box[0], pt) for pt in box[1:]] dist.sort() self._quad_box_size = tuple(dist[0:-1])
def get_rel_avg_position_error(orig, other): rel_diff_sum = 0 for i in range(4): rel_diff_sum += geom.distance(orig.corners[i], other.corners[i]) / np.linalg.norm( orig.corners[i]) return rel_diff_sum / 4
def _find_corners(lines): pairs = [pair for pair in itertools.combinations(lines.tolist(), 2)] distances = [] for pair in pairs: l1 = shapes.create_line_segment_from_np(pair[0]) l2 = shapes.create_line_segment_from_np(pair[1]) distances.append(geom.line_line_distance(l1.a, l1.b, l2.a, l2.b)) pairs.pop(np.argmax(distances)) if pairs[0][0] in pairs[1]: base = shapes.create_line_segment_from_np(pairs[0][0]) elif pairs[0][1] in pairs[1]: base = shapes.create_line_segment_from_np(pairs[0][1]) else: raise BaseNotFound line_list = [ shapes.create_line_segment_from_np(line) for line in lines.tolist() ] line_list.remove(base) inner = [] outer = [] for line in line_list: A = ((line.b.y - line.a.y, line.a.x - line.b.x), (base.b.y - base.a.y, base.a.x - base.b.x)) B = (line.a.x * line.b.y - line.b.x * line.a.y, base.a.x * base.b.y - base.b.x * base.a.y) A = np.stack(A) try: intersect = np.linalg.solve(A, B) except np.linalg.LinAlgError: raise IntersectionNotFound dist = [ geom.distance(intersect, point) for point in line.get_endpoints() ] inner.append(shapes.Point2D(intersect[0], intersect[1])) outer.append(line[np.argmax(dist)]) return [outer[0], inner[0], inner[1], outer[1]]
def _find_parallel(self, point, other_points): distances = [geom.distance(point, -other) for other in other_points] return np.argmin(distances)
def get_c_multiplier(self): return geom.distance(self.corners[3], self.corners[2]) / self.get_base_length()
def get_base_length(self): return geom.distance(self.corners[1], self.corners[2])
def get_abs_sum_position_error(orig, other): abs_diff = 0 for i in range(4): abs_diff += geom.distance(orig.corners[i], other.corners[i]) return abs_diff
def get_length(self): return geom.distance(self.a, self.b)