转换器工具¶
supervision.detection.utils.converters.xyxy_to_xywh(xyxy)
¶
Converts bounding box coordinates from (x_min, y_min, x_max, y_max)
format to (x, y, width, height)
format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A numpy array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A numpy array of shape |
Examples:
import numpy as np
import supervision as sv
xyxy = np.array([
[10, 20, 40, 60],
[15, 25, 50, 70]
])
sv.xyxy_to_xywh(xyxy=xyxy)
# array([
# [10, 20, 30, 40],
# [15, 25, 35, 45]
# ])
Source code in supervision/detection/utils/converters.py
supervision.detection.utils.converters.xywh_to_xyxy(xywh)
¶
Converts bounding box coordinates from (x, y, width, height)
format to (x_min, y_min, x_max, y_max)
format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A numpy array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A numpy array of shape |
Examples:
import numpy as np
import supervision as sv
xywh = np.array([
[10, 20, 30, 40],
[15, 25, 35, 45]
])
sv.xywh_to_xyxy(xywh=xywh)
# array([
# [10, 20, 40, 60],
# [15, 25, 50, 70]
# ])
Source code in supervision/detection/utils/converters.py
supervision.detection.utils.converters.xyxy_to_xcycarh(xyxy)
¶
Converts bounding box coordinates from (x_min, y_min, x_max, y_max)
into measurement space to format (center x, center y, aspect ratio, height)
,
where the aspect ratio is width / height
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
Bounding box in format |
required |
Returns:
np.ndarray: Bounding box in format
(center x, center y, aspect ratio, height)
. Shape (N, 4)
.
Examples:
import numpy as np
import supervision as sv
xyxy = np.array([
[10, 20, 40, 60],
[15, 25, 50, 70]
])
sv.xyxy_to_xcycarh(xyxy=xyxy)
# array([
# [25. , 40. , 0.75, 40. ],
# [32.5 , 47.5 , 0.77777778, 45. ]
# ])
Source code in supervision/detection/utils/converters.py
supervision.detection.utils.converters.xcycwh_to_xyxy(xcycwh)
¶
Converts bounding box coordinates from (center_x, center_y, width, height)
format to (x_min, y_min, x_max, y_max)
format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A numpy array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A numpy array of shape |
Examples:
import numpy as np
import supervision as sv
xcycwh = np.array([
[50, 50, 20, 30],
[30, 40, 10, 15]
])
sv.xcycwh_to_xyxy(xcycwh=xcycwh)
# array([
# [40, 35, 60, 65],
# [25, 32.5, 35, 47.5]
# ])
Source code in supervision/detection/utils/converters.py
supervision.detection.utils.converters.xyxy_to_polygons(box)
¶
Convert an array of boxes to an array of polygons. Retains the input datatype.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
An array of boxes (N, 4), where each box is represented as a
list of four coordinates in the format |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: An array of polygons (N, 4, 2), where each polygon is
represented as a list of four coordinates in the format |
Source code in supervision/detection/utils/converters.py
supervision.detection.utils.converters.mask_to_xyxy(masks)
¶
Converts a 3D np.array
of 2D bool masks into a 2D np.array
of bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A 3D |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A 2D |
Source code in supervision/detection/utils/converters.py
supervision.detection.utils.converters.mask_to_polygons(mask)
¶
Converts a binary mask to a list of polygons.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A binary mask represented as a 2D NumPy array of
shape |
required |
Returns:
Type | Description |
---|---|
list[ndarray]
|
List[np.ndarray]: A list of polygons, where each polygon is represented by a
NumPy array of shape |
Source code in supervision/detection/utils/converters.py
supervision.detection.utils.converters.polygon_to_mask(polygon, resolution_wh)
¶
Generate a mask from a polygon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
The polygon for which the mask should be generated, given as a list of vertices. |
required |
|
Tuple[int, int]
|
The width and height of the desired resolution. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The generated 2D mask, where the polygon is marked with
|
Source code in supervision/detection/utils/converters.py
supervision.detection.utils.converters.polygon_to_xyxy(polygon)
¶
Converts a polygon represented by a NumPy array into a bounding box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ndarray
|
A polygon represented by a NumPy array of shape |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A 1D NumPy array containing the bounding box
|