Converting segmentation masks to visual layer format
This guide explains how to convert polygon-based segmentation mask annotations into a CSV file that conforms to Visual Layer’s object annotation format.
In this dataset, the images and annotations are organized as follows:
Images: Located in a directory (PNG/JPG format).
Annotations: JSON files containing polygon-based segmentation masks.
Each JSON file corresponds to an image and contains polygon annotations with coordinate points defining object boundaries.
Segmentation Mask Annotation Format
Each annotation file contains polygon shapes in the following JSON structure:
Example annotation structure:
label
: Represents the object class (e.g., “QSBD”).
points
: Array of [x, y] coordinates defining the polygon boundary.
shape_type
: Type of annotation (“polygon” for segmentation masks).
imagePath
: Corresponding image filename.
Conversion Process
The conversion script performs the following steps:
- Load Each JSON File:
The script reads each JSON annotation file to extract polygon coordinates. - Extract Polygon Points:
For each shape, it extracts the array of [x, y] coordinate points. - Calculate Bounding Box:
The script converts polygon coordinates to bounding boxes by finding the minimum and maximum x,y values:
- Generate the CSV File:
The CSV is generated with the following columns:
filename
: The image file name.
col_x
: The x‑coordinate (column) of the top‑left corner.
row_y
: The y‑coordinate (row) of the top‑left corner.
width
: Bounding box width (pixels).
height
: Bounding box height (pixels).
label
: The object label (e.g., “QSBD”).
Python Conversion Script
Script Explanation
- Input Processing:
The script iterates over all .json files in the annotations folder and extracts polygon shapes from each file. - Polygon Extraction:
For each polygon shape, the script extracts the coordinate points and validates the data structure. - Bounding Box Calculation:
The polygon coordinates are converted to bounding boxes by finding the minimum and maximum x,y values to determine the rectangular bounds. - CSV Output:
The CSV file is written with each bounding box as a row containing the image filename, top-left coordinates, dimensions, and object label.
Example CSV Output
After running the script, the CSV output will look similar to this:
filename | col_x | row_y | width | height | label |
---|---|---|---|---|---|
example.png | 64 | 10 | 4 | 5 | QSBD |
example.png | 64 | 26 | 14 | 17 | QSBD |
example.png | 65 | 68 | 13 | 24 | QSBD |
filename
: Name of the image file.
col_x
& row_y
: Top‑left pixel coordinates of the bounding box.
width
& height
: Width and height of the bounding box in pixels.
label
: Object label from the segmentation mask.
Usage
- Place your JSON annotation files in the
annotations
folder - Run the conversion script:
python convert_segmentation.py
- The output CSV file
segmentation_annotations.csv
will be generated - Import this CSV file into Visual Layer for object detection tasks
The converted bounding boxes provide approximate rectangular bounds for the original polygon segmentation masks, making them compatible with Visual Layer’s object annotation format.