Box#
- class Box[source]#
Bases:
object
A multi-purpose box (ie. rectangle) representation .
Attributes
- __init__(ymin, xmin, ymax, xmax)[source]#
Construct a bounding box.
Unless otherwise stated, the convention is that these coordinates are in pixel coordinates and represent boxes that lie within a RasterSource.
- Parameters
ymin – minimum y value (y is row)
xmin – minimum x value (x is column)
ymax – maximum y value
xmax – maximum x value
Methods
__init__
(ymin, xmin, ymax, xmax)Construct a bounding box.
buffer
(buffer_sz, max_extent)Return new Box whose sides are buffered by buffer_sz.
center_crop
(edge_offset_y, edge_offset_x)Return Box whose sides are eroded by the given offsets.
copy
()erode
(erosion_sz)Return new Box whose sides are eroded by erosion_sz.
filter_by_aoi
(windows, aoi_polygons[, within])Filters windows by a list of AOI polygons
from_dict
(d)from_npbox
(npbox)Return new Box based on npbox format.
from_rasterio
(rio_window)from_shapely
(shape)Return Box as GeoJSON coordinates.
get_windows
(size, stride[, padding, ...])Returns a list of boxes representing windows generated using a sliding window traversal with the specified size, stride, and padding.
intersection
(other)Return the intersection of this Box and the other.
intersects
(other)make_random_box_container
(out_h, out_w)Return a new rectangular Box that contains this Box.
make_random_square
(size)Return new randomly positioned square Box that lies inside this Box.
Return a new square Box that contains this Box.
make_square
(ymin, xmin, size)Return new square Box.
Return Box in npbox format used by TF Object Detection API.
pad
(ymin, xmin, ymax, xmax)Pad sides by the given amount.
Return Box in Rasterio format: ((ymin, ymax), (xmin, xmax)).
reproject
(transform_fn)Reprojects this box based on a transform function.
shift_origin
(extent)Shift origin of window coords to (extent.xmin, extent.ymin).
to_dict
()to_int
()to_npboxes
(boxes)Return nx4 numpy array from list of Box.
to_offsets
(container)Convert coords to offsets from (container.xmin, container.ymin).
Get (x, y) coords of each vertex as a 4x2 numpy array.
Convert to a Rasterio Window.
Convert to shapely Polygon.
Convert to slices: ymin:ymax, xmin:xmax
to_xywh
()to_xyxy
()translate
(dy, dx)Translate window along y and x axes by the given distances.
within_aoi
(window, aoi_polygons)Check if window is within a list of AOI polygons.
- __init__(ymin, xmin, ymax, xmax)[source]#
Construct a bounding box.
Unless otherwise stated, the convention is that these coordinates are in pixel coordinates and represent boxes that lie within a RasterSource.
- Parameters
ymin – minimum y value (y is row)
xmin – minimum x value (x is column)
ymax – maximum y value
xmax – maximum x value
- buffer(buffer_sz: float, max_extent: Box) Box [source]#
Return new Box whose sides are buffered by buffer_sz.
The resulting box is clipped so that the values of the corners are always greater than zero and less than the height and width of max_extent.
- center_crop(edge_offset_y: int, edge_offset_x: int) Box [source]#
Return Box whose sides are eroded by the given offsets.
Box(0, 0, 10, 10).center_crop(2, 4) == Box(2, 4, 8, 6)
- static filter_by_aoi(windows: List[Box], aoi_polygons: List[Polygon], within: bool = True) List[Box] [source]#
Filters windows by a list of AOI polygons
- static from_npbox(npbox)[source]#
Return new Box based on npbox format.
- Parameters
npbox – Numpy array of form [ymin, xmin, ymax, xmax] with float type
- get_windows(size: Union[PositiveInt, Tuple[PositiveInt, PositiveInt]], stride: Union[PositiveInt, Tuple[PositiveInt, PositiveInt]], padding: Optional[Union[ConstrainedIntValue, Tuple[ConstrainedIntValue, ConstrainedIntValue]]] = None, pad_direction: Literal['both', 'start', 'end'] = 'end') List[Box] [source]#
Returns a list of boxes representing windows generated using a sliding window traversal with the specified size, stride, and padding.
Each of size, stride, and padding can be either a positive int or a tuple (vertical-componet, horizontal-component) of positive ints.
Padding currently only applies to the right and bottom edges.
- Parameters
size (Union[PosInt, Tuple[PosInt, PosInt]]) – Size (h, w) of the windows.
stride (Union[PosInt, Tuple[PosInt, PosInt]]) – Step size between windows. Can be 2-tuple (h_step, w_step) or positive int.
padding (Optional[Union[PosInt, Tuple[PosInt, PosInt]]], optional) – Optional padding to accomodate windows that overflow the extent. Can be 2-tuple (h_pad, w_pad) or non-negative int. If None, will be set to (size[0]//2, size[1]//2). Defaults to None.
pad_direction (Literal['both', 'start', 'end']) – If ‘end’, only pad ymax and xmax (bottom and right). If ‘start’, only pad ymin and xmin (top and left). If ‘both’, pad all sides. Has no effect if paddiong is zero. Defaults to ‘end’.
- Returns
List of Box objects.
- Return type
List[Box]
- make_random_box_container(out_h: int, out_w: int) Box [source]#
Return a new rectangular Box that contains this Box.
- make_random_square(size: int) Box [source]#
Return new randomly positioned square Box that lies inside this Box.
- make_random_square_container(size: int) Box [source]#
Return a new square Box that contains this Box.
- npbox_format()[source]#
Return Box in npbox format used by TF Object Detection API.
- Returns
Numpy array of form [ymin, xmin, ymax, xmax] with float type
- rasterio_format() Tuple[Tuple[int, int], Tuple[int, int]] [source]#
Return Box in Rasterio format: ((ymin, ymax), (xmin, xmax)).
- shift_origin(extent: Box) Box [source]#
Shift origin of window coords to (extent.xmin, extent.ymin).
- to_offsets(container: Box) Box [source]#
Convert coords to offsets from (container.xmin, container.ymin).
- translate(dy: int, dx: int) Box [source]#
Translate window along y and x axes by the given distances.