Created
March 13, 2025 13:42
-
-
Save minimal-scouser/e55f7f58465c868c7bdd4e5a34de3945 to your computer and use it in GitHub Desktop.
Create labelmap segmentations from a nifti mask file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // load the mask file as a volume | |
| const maskUrl = 'mask-url' | |
| const imageIds = await createNiftiImageIdsAndCacheMetadata({ | |
| url: maskUrl, | |
| }) | |
| const maskVolumeId = 'mask-volume-nifti' | |
| const maskVolume = await volumeLoader.createAndCacheVolume(maskVolumeId, { | |
| imageIds, | |
| }) | |
| maskVolume.load() | |
| // create a segmentation, refering to the mask volume | |
| const segmentationId = 'nifti_segmentation' | |
| const segVolume = await volumeLoader.createAndCacheDerivedLabelmapVolume( | |
| maskVolume, | |
| { | |
| volumeId: segmentationId, | |
| } | |
| ) | |
| await cornerstoneTools.segmentation.addSegmentations([ | |
| { | |
| segmentationId, | |
| representation: { | |
| type: SegmentationRepresentations.Labelmap, | |
| data: { | |
| volumeId: segmentationId, | |
| }, | |
| }, | |
| }, | |
| ]) | |
| // iterate over the voxels of nifti volume and add non-zero values to the segmentation | |
| let i = 0 | |
| while (1) { | |
| if (i === maskVolume.numVoxels) { | |
| break | |
| } | |
| const value = maskVolume.voxelManager.getAtIndex(i) | |
| if (value !== 0) { | |
| segVolume.voxelManager.setAtIndex(i, value) | |
| } | |
| ++i | |
| } | |
| await segmentation.addSegmentationRepresentations(viewportIds, [ | |
| { | |
| segmentationId, | |
| type: SegmentationRepresentations.Labelmap, | |
| }, | |
| ]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment