Skip to content

Commit

Permalink
feat(segmentGroups): create segments from converted image
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHax committed Apr 11, 2024
1 parent c19dcf2 commit b089616
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
30 changes: 0 additions & 30 deletions src/io/dicom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 39 additions & 4 deletions src/store/segmentGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b089616

Please sign in to comment.