> ## Documentation Index
> Fetch the complete documentation index at: https://docs.datamarkin.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Polygon

> Draw polygon outlines on detected objects with segmentation masks.

## Overview

Renders precise polygon boundaries around detected objects using their segmentation data. Automatically adapts line thickness based on image size and supports custom color schemes for visual distinction between different object classes.

## Function Signature

```python theme={null}
polygon(
    image: np.ndarray,
    detections: Detections,
    thickness: Optional[int] = None,
    colors: Optional[List[Tuple[int, int, int]]] = None
) -> np.ndarray
```

## Parameters

**image** `np.ndarray` *required*

Input image to draw polygons on. Must be a valid BGR image array.

**detections** `Detections` *required*

Detections object containing segmentation data. Each detection must have a 'segments' attribute with polygon coordinates.

**thickness** `Optional[int]` (default: None)

Line thickness for polygon outlines in pixels. If None, automatically calculated based on image dimensions. Range: \[1-50]. Default is adaptive (typically 1-6).

**colors** `Optional[List[Tuple[int, int, int]]]` (default: None)

List of BGR color tuples to override default colors. Colors are cycled through unique class\_ids in order of appearance. Each tuple should be (B, G, R) with values \[0-255]. If None, uses default ColorManager colors.

## Returns

**result** `np.ndarray`

Image with polygon outlines drawn. Modifies the input image in-place and returns the same array reference.

## Examples

```python Example theme={null}
import cv2
import pixelflow as pf

# Load image and run segmentation model
image = cv2.imread("path/to/image.jpg")
model = YOLO("yolo11n-seg.pt")  # Segmentation model
outputs = model.predict(image)
results = pf.results.from_ultralytics(outputs)

# Draw polygon outlines with default settings
annotated = pf.annotators.polygon(image, results)

# Customize line thickness and colors
custom_colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]  # Blue, Green, Red
annotated = pf.annotators.polygon(image, results, thickness=3, colors=custom_colors)

# Use adaptive thickness on high-resolution image
annotated = pf.annotators.polygon(image, results, thickness=None)

```

## Error Handling

> [!WARNING]
>
> This function may raise the following exceptions:
>
> * **AssertionError**: If image is not a numpy array.
> * **AttributeError**: If detections don't contain segments data.
> * **ValueError**: If polygon coordinates are invalid or out of bounds.

## Notes

> [!NOTE]
>
> * Modifies the input image in-place for memory efficiency
> * Polygon coordinates are automatically converted to integer format for OpenCV
> * Line thickness is adaptively calculated based on image size when not specified
> * Colors are assigned consistently based on class\_id to maintain visual coherence
> * Requires segmentation model outputs (not just bounding boxes)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.