def _bounding_quadrangle(self, vertices): hull_points = cv2.convexHull(vertices, clockwise=False) # The convex hull of a list of markers must have at least 4 corners, since a # single marker already has 4 corners. If the convex hull has more than 4 # corners we reduce that number with approximations of the hull. if len(hull_points) > 4: new_hull = cv2.approxPolyDP(hull_points, epsilon=1, closed=True) if new_hull.shape[0] >= 4: hull_points = new_hull if len(hull_points) > 4: curvature = abs(methods.GetAnglesPolyline(hull_points, closed=True)) most_acute_4_threshold = sorted(curvature)[3] hull_points = hull_points[curvature <= most_acute_4_threshold] # Vertices space is flipped in y. We need to change the order of the # hull_points vertices hull_points = hull_points[[1, 0, 3, 2], :, :] # Roll the hull_points vertices until we have the right orientation: # vertices space has its origin at the image center. Adding 1 to the # coordinates puts the origin at the top left. distance_to_top_left = np.sqrt((hull_points[:, :, 0] + 1)**2 + (hull_points[:, :, 1] + 1)**2) bot_left_idx = np.argmin(distance_to_top_left) + 1 hull_points = np.roll(hull_points, -bot_left_idx, axis=0) return hull_points
def _bounding_quadrangle(self, vertices): # According to OpenCV implementation, cv2.convexHull only accepts arrays with # 32bit floats (CV_32F) or 32bit signed ints (CV_32S). # See: https://github.com/opencv/opencv/blob/3.4/modules/imgproc/src/convhull.cpp#L137 # See: https://github.com/pupil-labs/pupil/issues/1544 vertices = np.asarray(vertices, dtype=np.float32) hull_points = cv2.convexHull(vertices, clockwise=False) # The convex hull of a list of markers must have at least 4 corners, since a # single marker already has 4 corners. If the convex hull has more than 4 # corners we reduce that number with approximations of the hull. if len(hull_points) > 4: new_hull = cv2.approxPolyDP(hull_points, epsilon=1, closed=True) if new_hull.shape[0] >= 4: hull_points = new_hull if len(hull_points) > 4: curvature = abs(methods.GetAnglesPolyline(hull_points, closed=True)) most_acute_4_threshold = sorted(curvature)[3] hull_points = hull_points[curvature <= most_acute_4_threshold] # Vertices space is flipped in y. We need to change the order of the # hull_points vertices hull_points = hull_points[[1, 0, 3, 2], :, :] # Roll the hull_points vertices until we have the right orientation: # vertices space has its origin at the image center. Adding 1 to the # coordinates puts the origin at the top left. distance_to_top_left = np.sqrt((hull_points[:, :, 0] + 1)**2 + (hull_points[:, :, 1] + 1)**2) bot_left_idx = np.argmin(distance_to_top_left) + 1 hull_points = np.roll(hull_points, -bot_left_idx, axis=0) return hull_points