# Image segmentation tool

To work with image segmentation, use the Polygon tool. To make sure the polygon tool is available in the annotation workspace, when creating the task, make sure Object Detection is checked as the type of task, and Polygon is checked as one of the annotation tools you want to use.

<figure><img src="https://4219847035-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaFKXVbOeJ2H3b8HBGVBG%2Fuploads%2FGD3A0O0y3Ez1NowRmDnI%2Fimage.png?alt=media&#x26;token=ba4469ca-f776-48f6-9d0b-0ab9b81362d2" alt=""><figcaption></figcaption></figure>

### Semantic and Instance labeling

<figure><img src="https://4219847035-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaFKXVbOeJ2H3b8HBGVBG%2Fuploads%2FZqNxxSjTyAQWgdEh2CbC%2Fimage.png?alt=media&#x26;token=f2f18f30-8561-4a80-9c50-b4072810f205" alt=""><figcaption></figcaption></figure>

When setting up the labels in a task, you can specify if a label is Semantic, Instance or polygon. Only choose polygon if you require overlapping polygons rather than segmentation mask.

Choosing the right type of Mask is important to ensure the output masks are correctly formatted, and helpful features like snapping and merging of overlapping polygons can work properly.

**Snapping**

Both Snapping and Merging are automatically turned on for both Semantic Mask and Instance mask classes. However they work slightly differently for each. For Semantic segmentation classes, only objects of different classes are snapped to a common boundary if they overlap. For Instance Segmentation classes, all objects are snapped to ensure different instances of the same label have properly formed boundaries as well. This essentially amounts to *drawing under* the existing mask.&#x20;

**Merging**

Merging only applies to Semantic Segmentation classes. Objects of the same class are automatically merged into a single object if they overlap. This is intendes to reduce clutter and make managing annotations easier.

Both settings can be manually turned on/off from the Settings on the annotation workspace.

<figure><img src="https://4219847035-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaFKXVbOeJ2H3b8HBGVBG%2Fuploads%2FE9Jt9k9TI8jqGFZ8UlWP%2Fmerging_snapping_settings.png?alt=media&#x26;token=4d69a50b-d467-4e9b-9a8f-98dbe765aa4e" alt=""><figcaption></figcaption></figure>

### Creating masks

To start creating a segmentation mask, select the Polygon tool, and click anywhere on the image to add a point to the mask. When the mask is complete, press the `N` key on the keyboard to complete the mask.&#x20;

Instead of clicking to place each point, you can also press and hold the `Shift` key, and simply move your mouse. This will place points at regular intervals all along the path followed by the mouse.

Once created, you can copy the mask and paste it anywhere on the image. This can be helpful when you have multiple objects in the image that have very similar boundaries. To do this, first copy the object by pressing the copy button in object card in the right sidebar. This will create a movable mask outline. When ready, click anywhere in the image to place the copied mask.

<figure><img src="https://4219847035-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaFKXVbOeJ2H3b8HBGVBG%2Fuploads%2FluNwGPYLgouoYmGsIXP1%2FScreenshot%20from%202023-04-10%2018-02-15.png?alt=media&#x26;token=1edc174c-7e27-462c-acf8-7d20ebd33ecf" alt="Copy pasting a segmentation mask"><figcaption><p>Copy pasting a segmentation mask</p></figcaption></figure>

### Updating a mask

Once a mask has been created, you can update it in multiple different ways.

1. To **move a an existing point**. simply drag the point using your mouse.
2. To **delete a point**, right click on the point and click on delete in the context menu. When a point is deleted, it's neighboring points are joined to create a new path.
3. To **update part of the mask**
   1. Press and hold the `Shift` key.
   2. Click on the starting point of the path you want to update.
   3. Add the new points.
   4. Click on any existing point of the original mask to close the new path and update the mask.

<figure><img src="https://4219847035-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaFKXVbOeJ2H3b8HBGVBG%2Fuploads%2F2PCUQ81WgSXZnDgzFSJr%2FScreenshot%20from%202023-04-10%2018-07-09.png?alt=media&#x26;token=55e160b0-e0de-432b-acd4-62f48a3ac4c5" alt="Editing a mask by adding a new path"><figcaption><p>Editing a mask by adding a new path</p></figcaption></figure>

### Working with Layers

When there are a lot of objects in an image, they can often overlap with each other. While automatic snapping and merging fix this issue, you can also use layers to achieve a similar result. Layers are also useful if you have an object that is completely inside another, in which case the outer object's polygon will completely contain the smaller object. Using layers is the proper way to handle such a situation.

An object in a higher layer (e.g. Layer 1 is higher than Layer 0) will be placed over an object in a lower layer. When an object is placed over another, it effectively *masks it -* hiding any overlapping parts of the object in the lower laye&#x72;*.* By default all objects are drawn in Layer 0. In order to move them to a higher or lower layer, use the two buttons below the Label selection dropdown in the object card.

<figure><img src="https://4219847035-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaFKXVbOeJ2H3b8HBGVBG%2Fuploads%2FPBBKFARVuEoC3NZna42L%2FScreenshot%20from%202023-04-10%2017-56-38.png?alt=media&#x26;token=22801e31-7237-42ac-a215-1649ae6d30c9" alt="Arranging objects in layers"><figcaption><p>Objects arranged in layers</p></figcaption></figure>

At any point, if you want to preview the segmentation mask, open the Segmentation sub-menu in the left sidebar, and check "Show segmentation mask".

{% hint style="warning" %}
If you have turned off Snapping from Settings, objects in your image can overlap with each other. In this case, avoid placing overlapping objects in the same layer. Otherwise, exported masks can contain arbitrary order of the objects, placing some masks above others.
{% endhint %}

### Exporting masks

Once the masks have been created, you can export them in many different formats.

To export the annotations as PNG masks, choose Segmentation mask from the export annotations dialog box on the task page. The downloaded files will contain both semantic segmentation as well instance segmentation masks in separate directories. Do note that processing the masks can take some time. You will receive a notification when the export is ready.

If you wish to export the annotations in polygon format - choose one of the following formats.

1. COCO
2. Datumaro
3. Pascal VOC
4. YOLO
5. Mindkosh


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mindkosh.com/image-video-annotation/image-segmentation-tool.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
