Polygons Utils¶
supervision.detection.utils.polygons.filter_polygons_by_area(polygons: list[npt.NDArray[np.number]], min_area: float | None = None, max_area: float | None = None) -> list[npt.NDArray[np.number]]
¶
Filters a list of polygons based on their area.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[NDArray[number]]
|
A list of polygons, where each polygon is
represented by a NumPy array of shape |
required |
|
float | None
|
The minimum area threshold. Only polygons with an area greater than or equal to this value will be included in the output. If set to None, no minimum area constraint will be applied. |
None
|
|
float | None
|
The maximum area threshold. Only polygons with an area less than or equal to this value will be included in the output. If set to None, no maximum area constraint will be applied. |
None
|
Returns:
| Type | Description |
|---|---|
list[NDArray[number]]
|
A new list of polygons containing only those with areas within the specified thresholds. |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> small = np.array([[0, 0], [2, 0], [2, 2], [0, 2]])
>>> big = np.array([[0, 0], [10, 0], [10, 10], [0, 10]])
>>> sv.filter_polygons_by_area([small, big], min_area=50)
[array([[ 0, 0],
[10, 0],
[10, 10],
[ 0, 10]])]
Source code in src/supervision/detection/utils/polygons.py
supervision.detection.utils.polygons.approximate_polygon(polygon: npt.NDArray[np.number], percentage: float, epsilon_step: float = 0.05) -> npt.NDArray[np.number]
¶
Approximates a given polygon by reducing a certain percentage of points.
This function uses the Ramer-Douglas-Peucker algorithm to simplify the input polygon by reducing the number of points while preserving the general shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[number]
|
A 2D NumPy array of shape |
required |
|
float
|
The percentage of points to be removed from the
input polygon, in the range |
required |
|
float
|
Approximation accuracy step, must be positive. Epsilon is the maximum distance between the original curve and its approximation. |
0.05
|
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
A new 2D NumPy array of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ValueError
|
If |
Examples:
Reduce a polygon to at most half its original point count:
>>> import numpy as np
>>> polygon = np.array([[0, 0], [10, 0], [10, 10], [0, 10],
... [5, 10], [5, 5], [3, 7], [1, 9]])
>>> result = approximate_polygon(polygon, percentage=0.5)
>>> result.shape[1]
2
>>> len(result) <= max(int(len(polygon) * 0.5), 3)
True
Polygon already at or below target — returned unchanged:
>>> tiny = np.array([[0, 0], [5, 0], [2, 4]])
>>> approximate_polygon(tiny, percentage=0.5) is tiny
True
Source code in src/supervision/detection/utils/polygons.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |