Box
box
#
Functions:
Name | Description |
---|---|
calculate_image_boundaries |
Calculate boundary RegionBox of a SimpleITK image. |
BoundingBoxOutsideImageError
#
BoxPadMethod
#
Bases: str
, enum.Enum
Enum for padding methods.
RegionBox
dataclass
#
RegionBox(
min: imgtools.coretypes.spatial_types.Coordinate3D,
max: imgtools.coretypes.spatial_types.Coordinate3D,
)
Represents a box in 3D space.
Attributes:
Name | Type | Description |
---|---|---|
min |
imgtools.coretypes.spatial_types.Coordinate3D
|
The minimum coordinate of the box. |
max |
imgtools.coretypes.spatial_types.Coordinate3D
|
The maximum coordinate of the box. |
size |
imgtools.coretypes.spatial_types.Size3D
|
The size of the box, calculated from the min and max coordinates. |
Methods:
Name | Description |
---|---|
check_out_of_bounds_coordinates |
Adjust the coordinates to ensure that the max values are not greater than the image size. |
copy |
Create a copy of the RegionBox. |
crop_image |
Crop an image to the coordinates defined by the box. |
crop_image_and_mask |
Crop an image and mask to the coordinates defined by the box. |
expand_to_cube |
Convert the bounding box to a cube by making the size equal along all dimensions. |
expand_to_min_size |
Ensure that the bounding box has a minimum size along each dimension. |
from_mask_bbox |
Creates a RegionBox from the bounding box of a mask image. |
from_mask_centroid |
Creates a RegionBox from the centroid of a mask image. |
from_tuple |
Creates a RegionBox from a tuple of min and max coordinates. |
pad |
Expand the bounding box by a specified padding value. |
check_out_of_bounds_coordinates
#
Adjust the coordinates to ensure that the max values are not greater than the image size.
Source code in src/imgtools/coretypes/box.py
copy
#
crop_image
#
crop_image(image: SimpleITK.Image) -> SimpleITK.Image
Crop an image to the coordinates defined by the box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SimpleITK.Image
|
The image to crop. |
required |
Returns:
Type | Description |
---|---|
SimpleITK.Image
|
The cropped image. |
Source code in src/imgtools/coretypes/box.py
crop_image_and_mask
#
crop_image_and_mask(
image: SimpleITK.Image, mask: SimpleITK.Image
) -> tuple[SimpleITK.Image, SimpleITK.Image]
Crop an image and mask to the coordinates defined by the box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SimpleITK.Image
|
The image to crop. |
required |
|
SimpleITK.Image
|
The mask to crop. |
required |
Returns:
Type | Description |
---|---|
tuple[SimpleITK.Image, SimpleITK.Image]
|
The cropped image and mask. |
Source code in src/imgtools/coretypes/box.py
expand_to_cube
#
expand_to_cube(
desired_size: int | None = None,
) -> imgtools.coretypes.box.RegionBox
Convert the bounding box to a cube by making the size equal along all dimensions.
This is done by finding which dimension is the largest, and then pad the other dimensions to make them equal to the desired size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int | None
|
The desired size of the cube. If None, the maximum dimension size is used |
None
|
Returns:
Type | Description |
---|---|
imgtools.coretypes.box.RegionBox
|
The bounding box converted to a cube. |
Source code in src/imgtools/coretypes/box.py
expand_to_min_size
#
Ensure that the bounding box has a minimum size along each dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
The minimum size of the bounding box along each dimension. |
5
|
Returns:
Type | Description |
---|---|
imgtools.coretypes.box.RegionBox
|
The bounding box with a minimum size along each dimension. |
Notes
Validation is done to ensure that any min coordinates that are negative are set to 0, and the difference is added to the maximum coordinates.
If an extra dimension is not an integer (e.g. 1.5), the bounding box is shifted 1 voxel towards the minimum in that dimension.
Source code in src/imgtools/coretypes/box.py
from_mask_bbox
classmethod
#
Creates a RegionBox from the bounding box of a mask image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SimpleITK.Image
|
The input mask image. |
required |
|
int
|
|
1
|
Returns:
Type | Description |
---|---|
imgtools.coretypes.box.RegionBox
|
The bounding box coordinates as a RegionBox object. |
Source code in src/imgtools/coretypes/box.py
from_mask_centroid
classmethod
#
from_mask_centroid(
mask: SimpleITK.Image,
label: int = 1,
desired_size: int | None = None,
) -> imgtools.coretypes.box.RegionBox
Creates a RegionBox from the centroid of a mask image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SimpleITK.Image
|
The input mask image. |
required |
|
int
|
label in the mask image to calculate the centroid. |
1
|
|
int | None
|
The desired size of the box. If None, the minimum size default from |
None
|
Returns:
Type | Description |
---|---|
imgtools.coretypes.box.RegionBox
|
The bounding box coordinates as a RegionBox object. |
Source code in src/imgtools/coretypes/box.py
from_tuple
classmethod
#
from_tuple(
coordmin: tuple[int, int, int],
coordmax: tuple[int, int, int],
) -> imgtools.coretypes.box.RegionBox
Creates a RegionBox from a tuple of min and max coordinates.
Source code in src/imgtools/coretypes/box.py
pad
#
pad(
padding: int,
method: imgtools.coretypes.box.BoxPadMethod = imgtools.coretypes.box.BoxPadMethod.SYMMETRIC,
) -> imgtools.coretypes.box.RegionBox
Expand the bounding box by a specified padding value.
Can be applied symmetrically on both sides or only at the end of the box. If the padded result has negative coordinates, they region is adjusted by shifting the min coordinates to 0 and adding the difference to the max coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
int
|
The padding value to expand the bounding box. |
required |
|
imgtools.coretypes.box.BoxPadMethod
|
The padding method to use. Default is BoxPadMethod.SYMMETRIC. Options are: - BoxPadMethod.SYMMETRIC: Pad symmetrically on both sides. - BoxPadMethod.END: Pad only at the end of the box (furthest from the origin). |
imgtools.coretypes.box.BoxPadMethod.SYMMETRIC
|
Returns:
Type | Description |
---|---|
imgtools.coretypes.box.RegionBox
|
The expanded bounding box. |
Examples:
>>> box = RegionBox(
... Coordinate3D(5, 5, 5),
... Coordinate3D(10, 10, 10),
... )
>>> box.pad(5)
RegionBox(
... min=Coordinate3D(x=0, y=0, z=0),
... max=Coordinate3D(x=15, y=15, z=15)
... size=(15, 15, 15)
)
>>> box.pad(5, method=BoxPadMethod.END)
RegionBox(
... min=Coordinate3D(x=5, y=5, z=5),
... max=Coordinate3D(x=15, y=15, z=15)
... size=(10, 10, 10)
)
Source code in src/imgtools/coretypes/box.py
calculate_image_boundaries
#
calculate_image_boundaries(
image: SimpleITK.Image,
use_world_coordinates: bool = False,
) -> imgtools.coretypes.box.RegionBox
Calculate boundary RegionBox of a SimpleITK image.
Calculates the origin and size of the image in either index or world coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
SimpleITK.Image
|
The input SimpleITK image. |
required |
|
bool
|
If True, the origin and size are calculated in world coordinates. Meant to be used internally and for debugging purposes. Use with caution as it may not work as the resulting RegionBox may not work as expected. |
False
|
Returns:
Type | Description |
---|---|
imgtools.coretypes.box.RegionBox
|
|
Examples:
>>> calculate_image_boundaries(image)
RegionBox(
min=Coordinate3D(x=0, y=0, z=0),
max=Coordinate3D(x=512, y=512, z=135)
size=Size3D(w=512, h=512, d=135)
)