3D Slicer ROI to YOLO Bounding Box Coordinates Converter for Medical Imaging
Project description
roi2bb
"3D Slicer ROI" to "YOLO Bounding Box" Coordinates Converter for Medical Imaging
This repository provides a Python class, roi2bb, bridging the gap between ground truth preparation and model training for 3D volumetric medical imaging, by converting Regions of Interest (ROI) bounding boxes stored in 3D Slicer JSON format into YOLO models input format.
When it comes to volumetric medical imaging data, there are few tools (if any) available for 3D bounding box annotation. 3D Slicer is a robust open-source tool for all types of data visualization, processing and annotation. 3D Slicer's ROI function of markups module, offers a user-friendly interface to generate 3D bounding boxes around the object/region of interest, edit and rotate them in axial, coronal and sagittal planes for object-oriented tasks and visualize them in 3D view. The central coordinates and the dimensions of these ROI boxes can be extracted as a JSON file, but the output is not compatible with the "Image coordinates System", which is the format compatible with the well-known YOLO family of deep learning models.
These two coordinate systems have 3 main differences that should be addressed while converting:
-
The Slicer output follows the "Patient coordinate system", in which its origin is located at an anatomical landmark not necessarily in the image boundaries, while the YOLO-compatible input format is based on the "Image coordinate system", in which its origin is located at the upper-left corner of the image. Also, the axis directions are not the same in different coordinate systems, changing the point coordinate values.
-
The Slicer format dimensions are the actual ROI dimensions, while the Yolo format dimensions are a ratio of ROI dimension to image dimensions.
-
The Slicer output is in JSON format with the ROI 'center' coordinates (x,y,z) and ROI dimensions 'size' (x_length, y_length,z_length) reported under 'markups' tag, while YOLO-compatible input is a text file with each line presenting one ROI containing:
"class center_z center_x center_y width height depth"The Slicer gives a separate JSON file for each ROI, while a single YOLO text file contains multiple ROIs for multiple classes, each class defined by a unique index and each ROI reported in a separate line.
roi2bb offers CLI support for single annotations and python API for several images and multi-class annotations.
Table of Contents:
Requirements:
- Python 3.x
- os
- glob
- numpy
- json
- argparse
- nibabel/pydicom/PIL/cv2/SimpleITK #depending on your images format
Installation
pip install roi2bb
or
Simply download the roi2bb repository from the upper-right "Code" button dropdown menu, navigate to the roi2bb folder, organize your data to be compatible with the tool (see Directory Structure).
Here is a stepwise guide to use roi2bb:
Directory Structure:
Since, yolo text format contains all classes and all labels per class in a single file, roi2bb has to process the whole directory of all annotations at once. roi2bb expects the below structure for images and labels. It automatically defines unique class IDs (1,2,3) by mapping the unique classes detected in JSON file names. You can also define your desired mapping using the Python API.
project_directory/
├── images/
│ ├── Patient_001.nii or .nii.gz
│ ├── Patient_002.nii
│ │──...
├── labels/
│ ├── Patient_001/
│ │ ├── left_atrium.json
│ │ ├── lymph_node_1.json
│ │ ├── lymph_node_2.json
│ │ ├── lymph_node_3.json
│ │ └── trachea.json
│ ├── Patient_002/
│ │ ├── left_atrium.json
│ │ ├── lymph_node.json
│ │ └── trachea.json
│ │── ...
| └── Patient_n
└── output/
│── Patient_001.txt
│── Patient_002.txt
|── ...
└── Patient_n
Class Index Mapping:
converter = roi2bb("path_to_image_file.nii", "path_to_json_folder", "output_yolo_format.txt", class_map:dic = class_map)
class_map = {
"left_atrium": 1,
"lymph_node": 2,
"trachea": 3,
...
}
Example Usage:
Now that you downloaded the repository, organized your data and customized your labels, you can use the following commands in a command line interface (CLI) or the next one in a Python interface to convert ROIs to YOLO format by roi2bb:
CLI
roi2bb example.nii.gz annotations/ output.txt
Python API
from roi2bb.converter import Converter
image_files_list = Path to the medical images
json_folders_list = Folders containing the 3D Slicer JSON annotation files corresponding to ptient_n
output_files_list = Path to save the .txt outputs containing YOLO format annotations
for image_file_path, json_folder_path, output_file_path in zip(image_files_list, json_folders_list, output_files_list)
converter = Converter(image_file_path, json_folder_path, output_file_path)
converter.run()
Example Output:
0 0.523 0.312 0.532 0.128 0.276 0.345 # left_atrium
1 0.734 0.512 0.723 0.132 0.254 0.367 # lymph_node_1
1 0.834 0.612 0.823 0.152 0.274 0.447 # lymph_node_2
2 0.634 0.412 0.623 0.112 0.234 0.287 # trachea
License:
roi2bb is released under the MIT License. See the LICENSE file for more details.
Useful Links
3D Slicer web page for volumetric medical imaging annotation
Learn more about the coordinates systems
Contact:
Developer: Elham Mahmoudi
Email: mahmoudi.elham91@gmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file roi2bb-0.1.0.tar.gz.
File metadata
- Download URL: roi2bb-0.1.0.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f08f07eaba354a7e84125e4f6363abdf6b4e09466a15c9b1c89e8d33aab4ad0
|
|
| MD5 |
1bb6f1a0b5d04d4fa03d207d6addd0ef
|
|
| BLAKE2b-256 |
caca0d2112f90b2b5f641c41e964b8245c213fa30df069fc3f24cb7209cc4920
|
File details
Details for the file roi2bb-0.1.0-py3-none-any.whl.
File metadata
- Download URL: roi2bb-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e81e2e8cef59b95c37996504c2df36336397cd025a2844b44c45cd674a2f6ed1
|
|
| MD5 |
b480ac51bac4e1c29970c1cfe626b074
|
|
| BLAKE2b-256 |
fb21621ac1d958d6e39c498fd469157307f8226b4e45792064b9e212152861dc
|