def _get_annotation_for_bbox(img: np.ndarray, roi: Rectangle, model) -> Annotation: """Runs inference within the given roi; moves resulting figures to global reference frame.""" img_cropped = roi.get_cropped_numpy_slice(img) # TODO pass through image and parent figure tags via roi_ann. roi_ann = Annotation(img_size=(roi.height, roi.width)) raw_result_ann = model.inference(img_cropped, roi_ann) return Annotation(img_size=img.shape[:2], labels=[label.translate(drow=roi.top, dcol=roi.left) for label in raw_result_ann.labels], img_tags=raw_result_ann.img_tags, img_description=raw_result_ann.img_description, pixelwise_scores_labels=[label.translate(drow=roi.top, dcol=roi.left) for label in raw_result_ann.pixelwise_scores_labels])
def crop(img: np.ndarray, rect: Rectangle) -> np.ndarray: ''' The function crop cut out part of the image with rectangle size. If rectangle for crop is out of image area it generates exception error(ValueError). :param img: image(numpy matrix) to be cropped :param rect: class object Rectangle of a certain size :return: cropped image ''' img_rect = Rectangle.from_array(img) if not img_rect.contains(rect): raise ValueError('Rectangle for crop out of image area!') return rect.get_cropped_numpy_slice(img)
def crop_with_padding(img: np.ndarray, rect: Rectangle) -> np.ndarray: ''' The function crop cut out part of the image with rectangle size. If rectangle for crop is out of image area it generates additional padding. :param img: image(numpy matrix) to be cropped :param rect: class object Rectangle of a certain size :return: cropped image ''' img_rect = Rectangle.from_array(img) if not img_rect.contains(rect): row, col = img.shape[:2] new_img = cv2.copyMakeBorder(img, top=rect.height, bottom=rect.height, left=rect.width, right=rect.width, borderType=cv2.BORDER_CONSTANT) new_rect = rect.translate(drow=rect.height, dcol=rect.width) return new_rect.get_cropped_numpy_slice(new_img) else: return rect.get_cropped_numpy_slice(img)
def crop(img: np.ndarray, rect: Rectangle) -> np.ndarray: img_rect = Rectangle.from_array(img) if not img_rect.contains(rect): raise ValueError('Rectangle for crop out of image area!') return rect.get_cropped_numpy_slice(img)