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

# Segmentation

> Assign a class to every pixel of an image

`RunAnywhere.segmentation.segment` returns a per-pixel class mask.

```dart theme={null}
final image = ImageInput.rawRgb(rgbBytes, width, height);
final result = await RunAnywhere.segmentation.segment(image);

for (final info in result.classes) {
  print('${info.label}: ${(info.fraction * 100).toStringAsFixed(1)}%');
}
```

```dart theme={null}
Future<SegmentationResult> segment(
  ImageInput image, {
  SegmentationOptions? options,
});
```

<Warning>
  The input must be `ImageInput.rawRgb(data, width, height)`. Packed RGB8 is the only layout the
  segmentation ABI accepts, so `ImageInput.file` and `ImageInput.bytes` throw here. Decode the image
  yourself first.
</Warning>

It also throws `SDKException` when the SDK is not initialized or no segmentation model is loaded.
`segment` does not auto-load: load the model yourself with `RunAnywhere.models.load(id)`.

## SegmentationResult

| Field             | Type              | Notes                                            |
| ----------------- | ----------------- | ------------------------------------------------ |
| `classMask`       | `Uint16List`      | One class id per pixel, row-major at source size |
| `width`           | `int`             | Mask width                                       |
| `height`          | `int`             | Mask height                                      |
| `classes`         | `List<ClassInfo>` | Classes present in the mask                      |
| `diagnosticImage` | `Uint8List?`      | Class-colour RGBA overlay, when requested        |

`ClassInfo` carries `classId` (the value used in the mask), `label`, `pixelCount`, and `fraction`, the
share of the image the class covers.

Read a single pixel's class with `classMask[y * width + x]`.

## SegmentationOptions

```dart theme={null}
final result = await RunAnywhere.segmentation.segment(
  image,
  options: const SegmentationOptions(includeDiagnosticImage: true),
);

final overlay = result.diagnosticImage;
if (overlay != null) {
  // overlay is RGBA at result.width x result.height
}
```

`includeDiagnosticImage` defaults to false. When true you also get a deterministic class-colour RGBA
image, which is useful for debugging but costs extra bytes across the bridge.

## Counting a class

```dart theme={null}
final result = await RunAnywhere.segmentation.segment(image);

for (final info in result.classes) {
  if (info.label == 'person') {
    print('${info.pixelCount} person pixels');
  }
}
```

## Preparing raw RGB

Any decoder works as long as you end up with three bytes per pixel and no padding.

```dart theme={null}
import 'package:image/image.dart' as img;

ImageInput toRawRgb(Uint8List encoded) {
  final decoded = img.decodeImage(encoded)!;
  final rgb = Uint8List(decoded.width * decoded.height * 3);
  var i = 0;
  for (final pixel in decoded) {
    rgb[i++] = pixel.r.toInt();
    rgb[i++] = pixel.g.toInt();
    rgb[i++] = pixel.b.toInt();
  }
  return ImageInput.rawRgb(rgb, decoded.width, decoded.height);
}
```

## Models

Segmentation models register under `MODEL_CATEGORY_SEMANTIC_SEGMENTATION`.

```dart theme={null}
await RunAnywhere.models.register(
  ModelRegistration.url(
    id: 'deeplabv3-mobilenet',
    name: 'DeepLabV3 MobileNet',
    url: 'https://example.com/deeplabv3-mobilenet.onnx',
    framework: InferenceFramework.INFERENCE_FRAMEWORK_ONNX,
    category: ModelCategory.MODEL_CATEGORY_SEMANTIC_SEGMENTATION,
    memoryRequirementBytes: 45000000,
  ),
);

await RunAnywhere.models.load('deeplabv3-mobilenet');
```

Register the ONNX backend with `await Onnx.register()` first.

## See also

<CardGroup cols={2}>
  <Card title="Vision language" icon="image" href="/flutter/vlm">
    Describing images in words
  </Card>

  <Card title="Models" icon="box" href="/flutter/models">
    Registration and loading
  </Card>
</CardGroup>
