def to_bbox(self): ''' The function to_bbox create Rectangle class object from current Bitmap class object :return: Rectangle class object ''' return Rectangle.from_array(self._data).translate( drow=self._origin.row, dcol=self._origin.col)
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 to_bbox(self): return Rectangle.from_array(self._data).translate( drow=self._origin.row, dcol=self._origin.col)
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)