Skip to main content

Convert STL segmentations to binary masks.

Project description

stl2mask

Convert segmentations in mesh format (e.g. STL, OBJ) to binary masks in image format (e.g. NIfTI, MHA).

Installation

stl2mask is intended to be used as a standalone command-line tool.

Install with pipx:

pipx install stl2mask

Or with uv:

uv tool install stl2mask

Usage

stl2mask

Convert STL mesh files to binary mask images. A reference image is required to define the output image space. Based on this reference image, a distance volume is created from the mesh, and a binary mask is generated by thresholding the distance volume.

stl2mask path/to/mesh.stl path/to/image.nii.gz

Parameters:

  • -o, --output: Path to the output mask image file. If not provided, the output file will be saved in the current directory with the same name as the input mesh file but with a .nii.gz extension.
  • -s, --suffix: Suffix for the output file if --output is not provided. Default is .nii.gz.
  • -t, --threshold: Threshold value for converting the mesh to a binary mask. Default is 0.0.
  • -f, --offset: Offset to apply to the origin of the distance volume, in terms of voxel spacing. This can help with alignment issues. Default is 0.5.
  • -m, --mask-value: Value to assign to the mask voxels inside the mesh. Default is 255.
  • -l, --log-level: Set the logging level. Options are DEBUG, INFO, WARNING, or ERROR. Default is INFO.

mask2stl

Convert binary mask images to STL mesh files. The binary mask is converted to a mesh using the Marching Cubes algorithm. Optionally, a reference image can be provided to define the output mesh space. This is not required if the mask image already has the correct spacing, origin, and direction.

mask2stl path/to/mask.nii.gz

Parameters:

  • -i, --image: Path to a reference image file to define the output mesh space.
  • -o, --output: Path to the output mesh file. If not provided, the output file will be saved in the current directory with the same name as the input mask file but with a .stl extension.
  • -s, --suffix: Suffix for the output file if --output is not provided. Default is .stl.
  • -v, --iso-value: Iso-value for the Marching Cubes algorithm. Defaults to the midpoint between the minimum and maximum values in the mask image.
  • -l, --log-level: Set the logging level. Options are DEBUG, INFO, WARNING, or ERROR. Default is INFO.

Examples

Converting a mesh to a mask

Basic conversion with default parameters:

stl2mask tumor.stl reference_image.nii.gz

Specify custom output location and mask value:

stl2mask tumor.stl reference_image.nii.gz -o output/tumor_mask.nii.gz -m 1

Adjust threshold for better mesh alignment:

stl2mask tumor.stl reference_image.nii.gz -t 0.5 -f 0.25

Converting a mask to a mesh

Basic conversion:

mask2stl tumor_mask.nii.gz

With a reference image for coordinate system transformation:

mask2stl tumor_mask.nii.gz -i reference_image.nii.gz -o tumor.stl

Troubleshooting

For detailed debugging information, use the --log-level DEBUG option:

stl2mask mesh.stl image.nii.gz --log-level DEBUG

This will show detailed information about image dimensions, spacing, and processing steps.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stl2mask-0.1.0.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stl2mask-0.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file stl2mask-0.1.0.tar.gz.

File metadata

  • Download URL: stl2mask-0.1.0.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.8

File hashes

Hashes for stl2mask-0.1.0.tar.gz
Algorithm Hash digest
SHA256 846f2db2ed58479775a0d48d8e22b43c8a79591406b6ab747d3b193f81e04758
MD5 b40436e7f3360373499d251fda2f680e
BLAKE2b-256 95673ca5e2affd8c01ecb21d7da7279cfd568c6018a1db65bb682aad04138f00

See more details on using hashes here.

File details

Details for the file stl2mask-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: stl2mask-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.8

File hashes

Hashes for stl2mask-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40d79d930e830027b49cb4001f7f236f73e7a3ac8bd126cdaff58f6f81f1c7d2
MD5 5c03c1817c65ebd763f66884b6048643
BLAKE2b-256 cb9f8acd5c3dd754b40e52888fccb3609ef1fc317573dc9dd8dcd53fa7de565a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page