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
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 168 169 170 171 | |
supervision.detection.utils.polygons.merge_multi_segment(segments: list[npt.NDArray[np.number]]) -> npt.NDArray[np.number]
¶
Merge multiple disconnected polygon segments into a single continuous polygon.
This function connects adjacent segments at their closest pair of vertices with a zero-width seam, producing a single closed polygon suitable for YOLO instance segmentation and similar formats that require a single continuous contour per object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[NDArray[number]]
|
A list of 2D NumPy arrays, where each array has shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[number]
|
A single NumPy array of shape |
NDArray[number]
|
If |
NDArray[number]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any segment does not have 2D shape with 2 coordinates per vertex.
Also raised if an adjacent pair of integer segments spans more than
|
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> p1 = np.array([[5, 5], [15, 5], [15, 15], [5, 15]])
>>> p2 = np.array([[35, 5], [45, 5], [45, 15], [35, 15]])
>>> merged = sv.merge_multi_segment([p1, p2])
>>> merged.shape
(10, 2)
Source code in src/supervision/detection/utils/polygons.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |