(self, box, remove_empty=False)
return bbox.convert(self.mode)
Cropss a rectangular region from this bounding box. The box is a
        4-tuple defining the left, upper, right, and lower pixel
        coordinate.
Cropss a rectangular region from this bounding box. The box is a
        4-tuple defining the left, upper, right, and lower pixel
        coordinate.
Cropss a rectangular region from this bounding box . The box is a 4 - tuple defining the left upper right and lower pixel coordinate .
def crop(self, box, remove_empty=False):
        """
        Cropss a rectangular region from this bounding box. The box is a
        4-tuple defining the left, upper, right, and lower pixel
        coordinate.
        """
        xmin, ymin, xmax, ymax = self._split_into_xyxy()
        w, h = box[2] - box[0], box[3] - box[1]
        cropped_xmin = (xmin - box[0]).clamp(min=0, max=w)
        cropped_ymin = (ymin - box[1]).clamp(min=0, max=h)
        cropped_xmax = (xmax - box[0]).clamp(min=0, max=w)
        cropped_ymax = (ymax - box[1]).clamp(min=0, max=h)

        # TODO should I filter empty boxes here?
        if False:
            is_empty = (cropped_xmin == cropped_xmax) | (cropped_ymin == cropped_ymax)

        cropped_box = torch.cat(
            (cropped_xmin, cropped_ymin, cropped_xmax, cropped_ymax), dim=-1
        )
        bbox = BoxList(cropped_box, (w, h), mode="xyxy")
        # bbox._copy_extra_fields(self)
        for k, v in self.extra_fields.items():
            if not isinstance(v, torch.Tensor):
                v = v.crop(box)
            bbox.add_field(k, v)

        if remove_empty:
            box = bbox.bbox
            keep = (box[:, 3] > box[:, 1]) & (box[:, 2] > box[:, 0])
            bbox = bbox[keep]
        return bbox.convert(self.mode)
def crop ( self , box , remove_empty = False ) : xmin , ymin , xmax , ymax = self . _split_into_xyxy ( ) w , h = box [ 2 ] - box [ 0 ] , box [ 3 ] - box [ 1 ] cropped_xmin = ( xmin - box [ 0 ] ) . clamp ( min = 0 , max = w ) cropped_ymin = ( ymin - box [ 1 ] ) . clamp ( min = 0 , max = h ) cropped_xmax = ( xmax - box [ 0 ] ) . clamp ( min = 0 , max = w ) cropped_ymax = ( ymax - box [ 1 ] ) . clamp ( min = 0 , max = h ) # TODO should I filter empty boxes here? if False : is_empty = ( cropped_xmin == cropped_xmax ) | ( cropped_ymin == cropped_ymax ) cropped_box = torch . cat ( ( cropped_xmin , cropped_ymin , cropped_xmax , cropped_ymax ) , dim = - 1 ) bbox = BoxList ( cropped_box , ( w , h ) , mode = "xyxy" ) # bbox._copy_extra_fields(self) for k , v in self . extra_fields . items ( ) : if not isinstance ( v , torch . Tensor ) : v = v . crop ( box ) bbox . add_field ( k , v ) if remove_empty : box = bbox . bbox keep = ( box [ : , 3 ] > box [ : , 1 ] ) & ( box [ : , 2 ] > box [ : , 0 ] ) bbox = bbox [ keep ] return bbox . convert ( self . mode )