Skip to content

Instantly share code, notes, and snippets.

@minimal-scouser
Created March 13, 2025 13:42
Show Gist options
  • Save minimal-scouser/e55f7f58465c868c7bdd4e5a34de3945 to your computer and use it in GitHub Desktop.
Save minimal-scouser/e55f7f58465c868c7bdd4e5a34de3945 to your computer and use it in GitHub Desktop.
Create labelmap segmentations from a nifti mask file
// 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