From b089616434dcce4d70061a2bdca209bc02923600 Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Thu, 11 Apr 2024 14:48:10 -0400 Subject: [PATCH] feat(segmentGroups): create segments from converted image --- src/io/dicom.ts | 30 -------------------------- src/store/segmentGroups.ts | 43 ++++++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/io/dicom.ts b/src/io/dicom.ts index af54476da..1dc91a36d 100644 --- a/src/io/dicom.ts +++ b/src/io/dicom.ts @@ -219,36 +219,6 @@ export class DICOMIO { return result.outputs[0].data as Image; } - async resample(fixed: SpatialParameters, moving: Image) { - await this.initialize(); - - const { size, spacing, origin, direction } = fixed; - const args = [ - '--action', - 'resample', - '0', // space for input image - - '--size', - size.join(','), - '--spacing', - spacing.join(','), - '--origin', - origin.join(','), - '--direction', - direction.join(','), - - '--memory-io', - '0', - ]; - - const inputs = [{ type: InterfaceTypes.Image, data: moving }]; - const outputs = [{ type: InterfaceTypes.Image }]; - - const result = await this.runTask('dicom', args, inputs, outputs); - const image = result.outputs[0].data as Image; - return image; - } - /** * Builds a volume for a set of files. * @async diff --git a/src/store/segmentGroups.ts b/src/store/segmentGroups.ts index 4b89b47b5..dc1f3c73b 100644 --- a/src/store/segmentGroups.ts +++ b/src/store/segmentGroups.ts @@ -28,6 +28,7 @@ import { selectionEquals, } from './datasets'; import { ensureSameSpace } from '../io/resample/resample'; +import { useDICOMStore } from './datasets-dicom'; const LabelmapArrayType = Uint8Array; export type LabelmapArrayType = Uint8Array; @@ -205,6 +206,41 @@ export const useSegmentGroupStore = defineStore('segmentGroup', () => { delete metadataByID[id]; } + let lastColorIndex = 0; + function getNextColor() { + const color = DEFAULT_SEGMENT_MASKS[lastColorIndex].color; + lastColorIndex = (lastColorIndex + 1) % DEFAULT_SEGMENT_MASKS.length; + return color; + } + + function decodeSegments(image: DataSelection) { + if (image.type === 'image') { + return structuredClone(DEFAULT_SEGMENT_MASKS); + } + + const dicomStore = useDICOMStore(); + const volumeInfo = dicomStore.volumeInfo[image.volumeKey]; + const segmentSequence = undefined; // volumeInfo.SegmentSequence; + if (!segmentSequence) { + return [ + { + value: 255, + name: volumeInfo.SeriesDescription || 'Unknown Segment', + color: getNextColor(), + }, + ]; + } + // TODO convert Recommended Display CIELab Value (0062,000D) tag to a segment color + // TODO convert SegmentDescription (0062,0006) tag to a segment name + return [ + { + value: 255, + name: volumeInfo.SeriesDescription || 'Unknown Segment', + color: [255, 0, 255, 255], + }, + ]; + } + /** * Converts an image to a labelmap. */ @@ -241,10 +277,9 @@ export const useSegmentGroupStore = defineStore('segmentGroup', () => { const resampled = await ensureSameSpace(parentImage, childImage, true); const labelmapImage = toLabelMap(resampled); - const { order, byKey } = normalizeForStore( - structuredClone(DEFAULT_SEGMENT_MASKS), - 'value' - ); + const segments = decodeSegments(image); + + const { order, byKey } = normalizeForStore(segments, 'value'); const segmentGroupStore = useSegmentGroupStore(); segmentGroupStore.addLabelmap(labelmapImage, { name: imageStore.metadata[imageID].name,