def _estimate_current_anticlockwise_degrees_using_minarearect(self, spot_xy) -> float:
# Find the minimum area rectangle around the number
nearby_contour_groups = contour_tools.extract_contour_groups_close_to(
self.contour_groups, target_point_xy=spot_xy, delta=self._min_pixels_between_contour_groups)
nearby_contours = [c for grp in nearby_contour_groups for c in grp]
box = cv2.minAreaRect(np.row_stack(nearby_contours))
corners_xy = cv2.boxPoints(box).astype(np.int32)
self._log_contours_on_current_image([corners_xy], name="Minimum area rectangle")
# Construct a vector which, once correctly rotated, goes from the bottom right corner up & left at 135 degrees
sorted_corners = sorted(corners_xy, key=lambda pt: np.linalg.norm(spot_xy - pt))
bottom_right_corner = sorted_corners[0] # The closest corner to the spot
adjacent_corners = sorted_corners[1:3] # The next two closest corners
unit_vectors_along_box_edge = misc.normalised(adjacent_corners - bottom_right_corner)
up_left_diagonal = unit_vectors_along_box_edge.sum(axis=0)
degrees_of_up_left_diagonal = np.rad2deg(np.arctan2(-up_left_diagonal[1], up_left_diagonal[0]))
return degrees_of_up_left_diagonal - 135
评论列表
文章目录