@cornerstonejs/adapters 5.11.7 → 5.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/adapters/Cornerstone/Segmentation_3X.js +1 -79
- package/dist/esm/adapters/Cornerstone/Segmentation_4X.d.ts +3 -2
- package/dist/esm/adapters/Cornerstone/Segmentation_4X.js +334 -149
- package/dist/esm/adapters/Cornerstone3D/Segmentation/generateSegmentation.js +221 -54
- package/dist/esm/adapters/Cornerstone3D/Segmentation/labelmapImagesFromBuffer.js +4 -4
- package/dist/esm/adapters/Cornerstone3D/Segmentation/perFrameFunctionalGroups.d.ts +7 -0
- package/dist/esm/adapters/Cornerstone3D/Segmentation/perFrameFunctionalGroups.js +64 -3
- package/dist/esm/adapters/Cornerstone3D/Segmentation/sourceFrame.d.ts +14 -0
- package/dist/esm/adapters/Cornerstone3D/Segmentation/sourceFrame.js +60 -0
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +7 -7
|
@@ -1,19 +1,20 @@
|
|
|
1
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
1
2
|
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
2
|
-
import { utilities as utilities$1,
|
|
3
|
+
import { data, normalizers, derivations, utilities as utilities$1, log } from 'dcmjs';
|
|
3
4
|
import ndarray from 'ndarray';
|
|
5
|
+
import { vec3 } from 'gl-matrix';
|
|
4
6
|
import getDatasetsFromImages from '../helpers/getDatasetsFromImages.js';
|
|
5
7
|
import checkOrientation from '../helpers/checkOrientation.js';
|
|
6
8
|
import { utilities } from '@cornerstonejs/core';
|
|
7
|
-
import {
|
|
9
|
+
import { utilities as utilities$2 } from '@cornerstonejs/metadata';
|
|
10
|
+
import { applyPerFrameFunctionalGroups, setPlaneOrientations, getReferencedSourceImageSequenceItem } from '../Cornerstone3D/Segmentation/perFrameFunctionalGroups.js';
|
|
11
|
+
import { getSourceFrame } from '../Cornerstone3D/Segmentation/sourceFrame.js';
|
|
8
12
|
import { getBitmapFramesFromDataset, encodeFramesToTransferSyntax, RLE_LOSSLESS_TRANSFER_SYNTAX_UID as RLE_LOSSLESS_TRANSFER_SYNTAX_UID$1 } from '../Cornerstone3D/encodePixelData.js';
|
|
9
13
|
import { Events } from '../enums/Events.js';
|
|
10
14
|
|
|
15
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
16
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
11
17
|
const cs3dLogger = utilities.logger.adaptersLog.getLogger('Cornerstone.Segmentation_4X');
|
|
12
|
-
const _utilities$orientatio = utilities$1.orientation,
|
|
13
|
-
rotateDirectionCosinesInPlane = _utilities$orientatio.rotateDirectionCosinesInPlane,
|
|
14
|
-
flipIOP = _utilities$orientatio.flipImageOrientationPatient,
|
|
15
|
-
flipMatrix2D = _utilities$orientatio.flipMatrix2D,
|
|
16
|
-
rotateMatrix902D = _utilities$orientatio.rotateMatrix902D;
|
|
17
18
|
const BitArray = data.BitArray,
|
|
18
19
|
DicomMessage = data.DicomMessage,
|
|
19
20
|
DicomMetaDictionary = data.DicomMetaDictionary;
|
|
@@ -54,6 +55,47 @@ function generateSegmentation(images, inputLabelmaps3D) {
|
|
|
54
55
|
return fillSegmentation(segmentation, inputLabelmaps3D, userOptions);
|
|
55
56
|
}
|
|
56
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Logs one warning for the SEG frames whose source image has no position or
|
|
60
|
+
* no orientation. Such a frame keeps the plane value that dcmjs gave it.
|
|
61
|
+
*/
|
|
62
|
+
function warnMissingPlanes(frameDescriptors, getSource) {
|
|
63
|
+
const missing = {
|
|
64
|
+
position: [],
|
|
65
|
+
orientation: []
|
|
66
|
+
};
|
|
67
|
+
frameDescriptors.forEach((desc, frameIndex) => {
|
|
68
|
+
const source = getSource(desc.sourceFrameIndex);
|
|
69
|
+
Object.keys(missing).forEach(key => {
|
|
70
|
+
if (!source[key]) {
|
|
71
|
+
missing[key].push(frameIndex + 1);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
const details = [['ImagePositionPatient', missing.position], ['ImageOrientationPatient', missing.orientation]].filter(_ref => {
|
|
76
|
+
let _ref2 = _slicedToArray(_ref, 2),
|
|
77
|
+
frames = _ref2[1];
|
|
78
|
+
return frames.length;
|
|
79
|
+
}).map(_ref3 => {
|
|
80
|
+
let _ref4 = _slicedToArray(_ref3, 2),
|
|
81
|
+
name = _ref4[0],
|
|
82
|
+
frames = _ref4[1];
|
|
83
|
+
return "no ".concat(name, " for SEG frames ").concat(frames.join(', '));
|
|
84
|
+
});
|
|
85
|
+
if (details.length) {
|
|
86
|
+
cs3dLogger.warn("The source images have ".concat(details.join('; '), ". These frames keep the ") + "values that dcmjs gave them, which can be those of other source images.");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The segment index of a voxel value of one labelmap3D. A value that is not in
|
|
92
|
+
* `labelmap3D.labelToSegmentIndex` is its own segment index.
|
|
93
|
+
*/
|
|
94
|
+
function getSegmentIndexForLabel(labelmap3D, labelValue) {
|
|
95
|
+
var _labelmap3D$labelToSe, _labelmap3D$labelToSe2;
|
|
96
|
+
return (_labelmap3D$labelToSe = labelmap3D === null || labelmap3D === void 0 || (_labelmap3D$labelToSe2 = labelmap3D.labelToSegmentIndex) === null || _labelmap3D$labelToSe2 === void 0 ? void 0 : _labelmap3D$labelToSe2[labelValue]) !== null && _labelmap3D$labelToSe !== void 0 ? _labelmap3D$labelToSe : labelValue;
|
|
97
|
+
}
|
|
98
|
+
|
|
57
99
|
/**
|
|
58
100
|
* The set of non-zero segment values actually present in a frame's pixel data,
|
|
59
101
|
* collected in a single pass. `segmentsOnLabelmap` can be stale, so presence is
|
|
@@ -79,6 +121,12 @@ function segmentsPresentInFrame(pixelData) {
|
|
|
79
121
|
*
|
|
80
122
|
* @param segmentation - The segmentation object to be filled.
|
|
81
123
|
* @param inputLabelmaps3D - An array of 3D labelmaps, or a single 3D labelmap.
|
|
124
|
+
* The voxels of a labelmap3D hold label values. `labelToSegmentIndex` maps a
|
|
125
|
+
* label value to its segment index, for a layer that stores a segment under
|
|
126
|
+
* another label value; a value that is not in the map is its own segment
|
|
127
|
+
* index. `segmentsOnLabelmap` of each labelmap2D lists label values. Two
|
|
128
|
+
* label values with the same segment index, in one labelmap3D or in two,
|
|
129
|
+
* give one SEG segment.
|
|
82
130
|
* @param userOptions - Optional configuration settings. Will override the default options.
|
|
83
131
|
* - `transferSyntaxUid` — the output transfer syntax. Defaults to RLE Lossless
|
|
84
132
|
* (`1.2.840.10008.1.2.5`); Explicit VR Little Endian
|
|
@@ -88,11 +136,23 @@ function segmentsPresentInFrame(pixelData) {
|
|
|
88
136
|
* valid RLE-encoded SEG), so it has been dropped rather than fixed. RLE
|
|
89
137
|
* output is now selected via `transferSyntaxUid` (RLE Lossless is the
|
|
90
138
|
* default), and encoding is handled by `encodeFramesToTransferSyntax`.
|
|
139
|
+
* @param images - The source images, in the order of the derived frames.
|
|
140
|
+
* @param metadata - A metadata provider for the `imageId` of each image.
|
|
141
|
+
*
|
|
142
|
+
* With `images` and `metadata`, each SEG frame gets the source reference, the
|
|
143
|
+
* position and the orientation of its own source image. dcmjs gives a frame
|
|
144
|
+
* the position of the referenced multiframe frame with the same index, but it
|
|
145
|
+
* sorts that multiframe by position, so for a stack in the other scan
|
|
146
|
+
* direction the frame gets the position of the mirrored slice. dcmjs also
|
|
147
|
+
* writes one shared orientation, from one source image. A frame without a
|
|
148
|
+
* source SOP Instance UID throws, so that the per-frame groups always match
|
|
149
|
+
* the PixelData frames. Without `images` or `metadata`, as from the legacy
|
|
150
|
+
* `generateSegmentation`, the frames keep the planes of dcmjs.
|
|
91
151
|
*
|
|
92
152
|
* @returns {object} The filled segmentation object.
|
|
93
153
|
*/
|
|
94
154
|
function fillSegmentation(segmentation, inputLabelmaps3D) {
|
|
95
|
-
var
|
|
155
|
+
var _segmentation$dataset, _segmentation$dataset2, _ref5, _options$transferSynt;
|
|
96
156
|
let userOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
97
157
|
let images = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
|
|
98
158
|
let metadata = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null;
|
|
@@ -100,97 +160,102 @@ function fillSegmentation(segmentation, inputLabelmaps3D) {
|
|
|
100
160
|
|
|
101
161
|
// Use another variable so we don't redefine labelmaps3D.
|
|
102
162
|
const labelmaps3D = Array.isArray(inputLabelmaps3D) ? inputLabelmaps3D : [inputLabelmaps3D];
|
|
103
|
-
const referencedFramesPerLabelmap = [];
|
|
104
163
|
const frameDescriptors = [];
|
|
164
|
+
const entriesBySegmentIndex = new Map();
|
|
105
165
|
for (let labelmapIndex = 0; labelmapIndex < labelmaps3D.length; labelmapIndex++) {
|
|
106
166
|
const labelmap3D = labelmaps3D[labelmapIndex];
|
|
107
167
|
const labelmaps2D = labelmap3D.labelmaps2D,
|
|
108
168
|
segmentMetadata = labelmap3D.metadata;
|
|
109
|
-
const referencedFramesPerSegment = [];
|
|
110
|
-
for (let i = 1; i < segmentMetadata.length; i++) {
|
|
111
|
-
if (segmentMetadata[i]) {
|
|
112
|
-
referencedFramesPerSegment[i] = [];
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
169
|
for (let i = 0; i < labelmaps2D.length; i++) {
|
|
116
170
|
const labelmap2D = labelmaps2D[i];
|
|
117
|
-
if (!
|
|
171
|
+
if (!labelmap2D) {
|
|
118
172
|
continue;
|
|
119
173
|
}
|
|
120
174
|
const segmentsOnLabelmap = labelmap2D.segmentsOnLabelmap;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (
|
|
124
|
-
|
|
175
|
+
segmentsPresentInFrame(labelmap2D.pixelData).forEach(labelValue => {
|
|
176
|
+
const segmentIndex = getSegmentIndexForLabel(labelmap3D, labelValue);
|
|
177
|
+
if (!segmentMetadata[segmentIndex] || !segmentsOnLabelmap.includes(labelValue)) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (!entriesBySegmentIndex.has(segmentIndex)) {
|
|
181
|
+
entriesBySegmentIndex.set(segmentIndex, {
|
|
182
|
+
segmentIndex,
|
|
183
|
+
metadata: segmentMetadata[segmentIndex],
|
|
184
|
+
sources: [],
|
|
185
|
+
frameIndices: new Set()
|
|
186
|
+
});
|
|
125
187
|
}
|
|
188
|
+
const entry = entriesBySegmentIndex.get(segmentIndex);
|
|
189
|
+
if (!entry.sources.some(source => source.labelmap3D === labelmap3D && source.labelValue === labelValue)) {
|
|
190
|
+
entry.sources.push({
|
|
191
|
+
labelmap3D,
|
|
192
|
+
labelValue
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
entry.frameIndices.add(i);
|
|
126
196
|
});
|
|
127
197
|
}
|
|
128
|
-
referencedFramesPerLabelmap[labelmapIndex] = referencedFramesPerSegment;
|
|
129
|
-
}
|
|
130
|
-
let numberOfFrames = 0;
|
|
131
|
-
for (let labelmapIndex = 0; labelmapIndex < labelmaps3D.length; labelmapIndex++) {
|
|
132
|
-
const referencedFramesPerSegment = referencedFramesPerLabelmap[labelmapIndex];
|
|
133
|
-
const labelmap3D = labelmaps3D[labelmapIndex];
|
|
134
|
-
const segmentMetadata = labelmap3D.metadata;
|
|
135
|
-
for (let segmentIndex = 1; segmentIndex < referencedFramesPerSegment.length; segmentIndex++) {
|
|
136
|
-
const referencedFrameIndicies = referencedFramesPerSegment[segmentIndex];
|
|
137
|
-
if (!(referencedFrameIndicies !== null && referencedFrameIndicies !== void 0 && referencedFrameIndicies.length) || !segmentMetadata[segmentIndex]) {
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
const labelmaps = _getLabelmapsFromReferencedFrameIndicies(labelmap3D, referencedFrameIndicies);
|
|
141
|
-
if (!labelmaps.length || !labelmaps.some(frame => (frame === null || frame === void 0 ? void 0 : frame.length) && frame.some(v => v !== 0))) {
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
numberOfFrames += referencedFrameIndicies.length;
|
|
145
|
-
}
|
|
146
198
|
}
|
|
199
|
+
|
|
200
|
+
// dcmjs numbers the segments in the order they are added.
|
|
201
|
+
const segmentEntries = [...entriesBySegmentIndex.values()].sort((a, b) => a.segmentIndex - b.segmentIndex);
|
|
202
|
+
const numberOfFrames = segmentEntries.reduce((total, entry) => total + entry.frameIndices.size, 0);
|
|
147
203
|
if (numberOfFrames === 0) {
|
|
148
204
|
throw new Error('No non-empty segmentation frames found for SEG export');
|
|
149
205
|
}
|
|
150
206
|
segmentation.setNumberOfFrames(numberOfFrames);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const referencedFrameNumbers = referencedFrameIndicies.map(element => element + 1);
|
|
167
|
-
referencedFrameNumbers.forEach(frameNumber => {
|
|
168
|
-
frameDescriptors.push({
|
|
169
|
-
referencedSegmentNumber: segmentIndex,
|
|
170
|
-
sourceFrameIndex: frameNumber - 1
|
|
171
|
-
});
|
|
207
|
+
const firstSegmentNumber = ((_segmentation$dataset = (_segmentation$dataset2 = segmentation.dataset.SegmentSequence) === null || _segmentation$dataset2 === void 0 ? void 0 : _segmentation$dataset2.length) !== null && _segmentation$dataset !== void 0 ? _segmentation$dataset : 0) + 1;
|
|
208
|
+
segmentEntries.forEach((entry, entryIndex) => {
|
|
209
|
+
const referencedFrameIndicies = [...entry.frameIndices].sort((a, b) => a - b);
|
|
210
|
+
const _getSegmentLabelmaps2 = _getSegmentLabelmaps(entry.sources, referencedFrameIndicies),
|
|
211
|
+
labelmaps = _getSegmentLabelmaps2.labelmaps,
|
|
212
|
+
labelValue = _getSegmentLabelmaps2.labelValue;
|
|
213
|
+
|
|
214
|
+
// Frame numbers start from 1.
|
|
215
|
+
const referencedFrameNumbers = referencedFrameIndicies.map(element => element + 1);
|
|
216
|
+
segmentation.addSegmentFromLabelmap(entry.metadata, labelmaps, labelValue, referencedFrameNumbers);
|
|
217
|
+
const referencedSegmentNumber = firstSegmentNumber + entryIndex;
|
|
218
|
+
referencedFrameNumbers.forEach(frameNumber => {
|
|
219
|
+
frameDescriptors.push({
|
|
220
|
+
referencedSegmentNumber,
|
|
221
|
+
sourceFrameIndex: frameNumber - 1
|
|
172
222
|
});
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
223
|
+
});
|
|
224
|
+
});
|
|
176
225
|
if (frameDescriptors.length && images !== null && images !== void 0 && images.length && metadata) {
|
|
177
|
-
//
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
226
|
+
// Read each source image one time, also when it holds many segments.
|
|
227
|
+
const sources = new Map();
|
|
228
|
+
const getSource = sourceFrameIndex => {
|
|
229
|
+
if (!sources.has(sourceFrameIndex)) {
|
|
230
|
+
const image = images[sourceFrameIndex];
|
|
231
|
+
sources.set(sourceFrameIndex, _objectSpread(_objectSpread({}, getSourceFrame(image, metadata)), {}, {
|
|
232
|
+
sourceImageSequenceItem: getReferencedSourceImageSequenceItem(image, metadata)
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
return sources.get(sourceFrameIndex);
|
|
236
|
+
};
|
|
181
237
|
const perFrameInputs = frameDescriptors.map(desc => {
|
|
182
|
-
const
|
|
238
|
+
const _getSource = getSource(desc.sourceFrameIndex),
|
|
239
|
+
position = _getSource.position,
|
|
240
|
+
sourceImageSequenceItem = _getSource.sourceImageSequenceItem;
|
|
241
|
+
|
|
242
|
+
// Throw, so that the per-frame groups always match the PixelData frames.
|
|
183
243
|
if (!(sourceImageSequenceItem !== null && sourceImageSequenceItem !== void 0 && sourceImageSequenceItem.ReferencedSOPInstanceUID)) {
|
|
184
244
|
throw new Error("Cannot resolve a source ReferencedSOPInstanceUID for SEG frame " + "(sourceFrameIndex ".concat(desc.sourceFrameIndex, ", segment ") + "".concat(desc.referencedSegmentNumber, "). Refusing to write a SEG with ") + "unreliable source image references.");
|
|
185
245
|
}
|
|
186
246
|
return {
|
|
187
247
|
referencedSegmentNumber: desc.referencedSegmentNumber,
|
|
188
|
-
sourceImageSequenceItem
|
|
248
|
+
sourceImageSequenceItem,
|
|
249
|
+
planePositionSequence: position ? {
|
|
250
|
+
ImagePositionPatient: position
|
|
251
|
+
} : undefined
|
|
189
252
|
};
|
|
190
253
|
});
|
|
191
254
|
applyPerFrameFunctionalGroups(segmentation.dataset, perFrameInputs);
|
|
255
|
+
setPlaneOrientations(segmentation.dataset, frameDescriptors.map(desc => getSource(desc.sourceFrameIndex).orientation));
|
|
256
|
+
warnMissingPlanes(frameDescriptors, getSource);
|
|
192
257
|
}
|
|
193
|
-
const transferSyntaxUid = (
|
|
258
|
+
const transferSyntaxUid = (_ref5 = (_options$transferSynt = options.transferSyntaxUid) !== null && _options$transferSynt !== void 0 ? _options$transferSynt : options.transferSyntaxUID) !== null && _ref5 !== void 0 ? _ref5 : RLE_LOSSLESS_TRANSFER_SYNTAX_UID;
|
|
194
259
|
if (transferSyntaxUid === RLE_LOSSLESS_TRANSFER_SYNTAX_UID) {
|
|
195
260
|
const isBinaryBitmap = segmentation.dataset.SegmentationType === 'BINARY' && Number(segmentation.dataset.BitsAllocated) === 1;
|
|
196
261
|
if (isBinaryBitmap) {
|
|
@@ -251,6 +316,48 @@ function fillSegmentation(segmentation, inputLabelmaps3D) {
|
|
|
251
316
|
}
|
|
252
317
|
return segmentation;
|
|
253
318
|
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* The frames of one SEG segment and the value that marks the segment in them.
|
|
322
|
+
* A segment with one source uses the frames of its labelmap3D. A segment with
|
|
323
|
+
* more sources gets new binary frames, with 1 where any source has its label
|
|
324
|
+
* value.
|
|
325
|
+
*/
|
|
326
|
+
function _getSegmentLabelmaps(sources, referencedFrameIndicies) {
|
|
327
|
+
if (sources.length === 1) {
|
|
328
|
+
const _sources = _slicedToArray(sources, 1),
|
|
329
|
+
_sources$ = _sources[0],
|
|
330
|
+
labelmap3D = _sources$.labelmap3D,
|
|
331
|
+
labelValue = _sources$.labelValue;
|
|
332
|
+
return {
|
|
333
|
+
labelmaps: _getLabelmapsFromReferencedFrameIndicies(labelmap3D, referencedFrameIndicies),
|
|
334
|
+
labelValue
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
const labelmaps = referencedFrameIndicies.map(frameIndex => {
|
|
338
|
+
let merged;
|
|
339
|
+
sources.forEach(_ref6 => {
|
|
340
|
+
var _labelmap3D$labelmaps;
|
|
341
|
+
let labelmap3D = _ref6.labelmap3D,
|
|
342
|
+
labelValue = _ref6.labelValue;
|
|
343
|
+
const pixelData = (_labelmap3D$labelmaps = labelmap3D.labelmaps2D[frameIndex]) === null || _labelmap3D$labelmaps === void 0 ? void 0 : _labelmap3D$labelmaps.pixelData;
|
|
344
|
+
if (!pixelData) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
merged || (merged = new Uint8Array(pixelData.length));
|
|
348
|
+
for (let i = 0; i < pixelData.length; i++) {
|
|
349
|
+
if (pixelData[i] === labelValue) {
|
|
350
|
+
merged[i] = 1;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
return merged;
|
|
355
|
+
});
|
|
356
|
+
return {
|
|
357
|
+
labelmaps,
|
|
358
|
+
labelValue: 1
|
|
359
|
+
};
|
|
360
|
+
}
|
|
254
361
|
function _getLabelmapsFromReferencedFrameIndicies(labelmap3D, referencedFrameIndicies) {
|
|
255
362
|
const labelmaps2D = labelmap3D.labelmaps2D;
|
|
256
363
|
const labelmaps = [];
|
|
@@ -690,9 +797,9 @@ function checkSEGsOverlapping(pixelData, multiframe, imageIds, validOrientations
|
|
|
690
797
|
frameSegmentsMapping.set(imageIdIndex, [frameSegment]);
|
|
691
798
|
}
|
|
692
799
|
}
|
|
693
|
-
for (let
|
|
694
|
-
var
|
|
695
|
-
let role =
|
|
800
|
+
for (let _ref7 of frameSegmentsMapping.entries()) {
|
|
801
|
+
var _ref8 = _slicedToArray(_ref7, 2);
|
|
802
|
+
let role = _ref8[1];
|
|
696
803
|
let temp2DArray = new TypedArrayConstructor(sliceLength).fill(0);
|
|
697
804
|
for (let i = 0; i < role.length; ++i) {
|
|
698
805
|
const frameSegment = role[i];
|
|
@@ -773,7 +880,9 @@ function insertOverlappingPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray,
|
|
|
773
880
|
continue;
|
|
774
881
|
}
|
|
775
882
|
const sourceImageMetadata = metadataProvider.get('instance', imageId);
|
|
776
|
-
|
|
883
|
+
// Compare the aligned frame: a frame rotated by 90 degrees in-plane has
|
|
884
|
+
// its Rows and Columns swapped relative to the source image.
|
|
885
|
+
if (alignedPixelDataI.shape[0] !== sourceImageMetadata.Rows || alignedPixelDataI.shape[1] !== sourceImageMetadata.Columns) {
|
|
777
886
|
throw new Error('Individual SEG frames have different geometry dimensions (Rows and Columns) ' + 'respect to the source image reference frame. This is not yet supported. ' + 'Aborting segmentation loading. ');
|
|
778
887
|
}
|
|
779
888
|
const imageIdIndex = imageIds.findIndex(element => element === imageId);
|
|
@@ -885,7 +994,7 @@ function insertPixelDataPlanar(segmentsOnFrame, segmentsOnFrameArray, labelmapBu
|
|
|
885
994
|
continue;
|
|
886
995
|
}
|
|
887
996
|
const sourceImageMetadata = imageIdMaps.metadata[imageId];
|
|
888
|
-
if (
|
|
997
|
+
if (alignedPixelDataI.shape[0] !== sourceImageMetadata.Rows || alignedPixelDataI.shape[1] !== sourceImageMetadata.Columns) {
|
|
889
998
|
throw new Error('Individual SEG frames have different geometry dimensions (Rows and Columns) ' + 'respect to the source image reference frame. This is not yet supported. ' + 'Aborting segmentation loading. ');
|
|
890
999
|
}
|
|
891
1000
|
const imageIdIndex = imageIdMaps.indices[imageId];
|
|
@@ -1049,6 +1158,12 @@ function isMultiframeImage(imageMetadata) {
|
|
|
1049
1158
|
/**
|
|
1050
1159
|
* getImageIdOfSourceImagebyGeometry - Returns the Cornerstone imageId of the source image.
|
|
1051
1160
|
*
|
|
1161
|
+
* The lookup first matches the SEG frame position exactly with the source
|
|
1162
|
+
* image position. If no image matches, the lookup selects the source image
|
|
1163
|
+
* whose plane holds the SEG frame position. A SEG frame that is flipped or
|
|
1164
|
+
* rotated in-plane has its origin at a different corner of the source slice,
|
|
1165
|
+
* so only the plane match finds that frame.
|
|
1166
|
+
*
|
|
1052
1167
|
* @param {String} ReferencedSeriesInstanceUID Referenced series of the source image.
|
|
1053
1168
|
* @param {String} FrameOfReferenceUID Frame of reference.
|
|
1054
1169
|
* @param {Object} PerFrameFunctionalGroup Sequence describing segmentation reference attributes per frame.
|
|
@@ -1063,25 +1178,43 @@ function getImageIdOfSourceImagebyGeometry(ReferencedSeriesInstanceUID, FrameOfR
|
|
|
1063
1178
|
if (!ReferencedSeriesInstanceUID || !((_PerFrameFunctionalGr = PerFrameFunctionalGroup.PlanePositionSequence) !== null && _PerFrameFunctionalGr !== void 0 && (_PerFrameFunctionalGr = _PerFrameFunctionalGr[0]) !== null && _PerFrameFunctionalGr !== void 0 && _PerFrameFunctionalGr.ImagePositionPatient)) {
|
|
1064
1179
|
return undefined;
|
|
1065
1180
|
}
|
|
1066
|
-
const segFramePosition = PerFrameFunctionalGroup.PlanePositionSequence[0].ImagePositionPatient;
|
|
1181
|
+
const segFramePosition = utilities$2.toNumber(PerFrameFunctionalGroup.PlanePositionSequence[0].ImagePositionPatient);
|
|
1182
|
+
const candidates = [];
|
|
1067
1183
|
for (let imageId of imageIds) {
|
|
1068
1184
|
const sourceImageMetadata = metadataProvider.get('instance', imageId);
|
|
1069
1185
|
if (!sourceImageMetadata) {
|
|
1070
1186
|
continue;
|
|
1071
1187
|
}
|
|
1072
|
-
const isMultiframe = isMultiframeImage(sourceImageMetadata);
|
|
1073
1188
|
if (!sourceImageMetadata.ImagePositionPatient || sourceImageMetadata.FrameOfReferenceUID !== FrameOfReferenceUID || sourceImageMetadata.SeriesInstanceUID !== ReferencedSeriesInstanceUID) {
|
|
1074
1189
|
continue;
|
|
1075
1190
|
}
|
|
1076
|
-
|
|
1191
|
+
const imagePlane = metadataProvider.get('imagePlaneModule', imageId);
|
|
1077
1192
|
// For multiframe images, check each frame's position
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
}
|
|
1193
|
+
const position = utilities$2.toNumber(isMultiframeImage(sourceImageMetadata) ? imagePlane === null || imagePlane === void 0 ? void 0 : imagePlane.imagePositionPatient : sourceImageMetadata.ImagePositionPatient);
|
|
1194
|
+
if (!position) {
|
|
1195
|
+
continue;
|
|
1196
|
+
}
|
|
1197
|
+
if (utilities.isEqual(segFramePosition, position, tolerance)) {
|
|
1198
|
+
return imageId;
|
|
1199
|
+
}
|
|
1200
|
+
candidates.push({
|
|
1201
|
+
imageId,
|
|
1202
|
+
position,
|
|
1203
|
+
imagePlane
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1206
|
+
for (const _ref9 of candidates) {
|
|
1207
|
+
const imageId = _ref9.imageId;
|
|
1208
|
+
const position = _ref9.position;
|
|
1209
|
+
const imagePlane = _ref9.imagePlane;
|
|
1210
|
+
const rowCosines = utilities$2.toNumber(imagePlane === null || imagePlane === void 0 ? void 0 : imagePlane.rowCosines);
|
|
1211
|
+
const columnCosines = utilities$2.toNumber(imagePlane === null || imagePlane === void 0 ? void 0 : imagePlane.columnCosines);
|
|
1212
|
+
if (!rowCosines || !columnCosines) {
|
|
1213
|
+
continue;
|
|
1214
|
+
}
|
|
1215
|
+
const normal = vec3.cross([0, 0, 0], rowCosines, columnCosines);
|
|
1216
|
+
const distance = utilities.voxelSlab.signedDistanceToPlane(segFramePosition, position, normal);
|
|
1217
|
+
if (Math.abs(distance) < tolerance) {
|
|
1085
1218
|
return imageId;
|
|
1086
1219
|
}
|
|
1087
1220
|
}
|
|
@@ -1089,77 +1222,129 @@ function getImageIdOfSourceImagebyGeometry(ReferencedSeriesInstanceUID, FrameOfR
|
|
|
1089
1222
|
}
|
|
1090
1223
|
|
|
1091
1224
|
/**
|
|
1092
|
-
* getValidOrientations - returns
|
|
1225
|
+
* getValidOrientations - returns the 8 in-plane orientations of a source
|
|
1226
|
+
* image, as the row (r) and column (c) cosines of a SEG frame in that plane.
|
|
1227
|
+
*
|
|
1228
|
+
* | Index | SEG IOP | SEG frame relative to the source |
|
|
1229
|
+
* | ----- | ---------- | -------------------------------- |
|
|
1230
|
+
* | 0 | (r, c) | identity |
|
|
1231
|
+
* | 1 | (r, -c) | flipped vertically |
|
|
1232
|
+
* | 2 | (-r, c) | flipped horizontally |
|
|
1233
|
+
* | 3 | (c, -r) | rotated 90 degrees |
|
|
1234
|
+
* | 4 | (c, r) | transposed |
|
|
1235
|
+
* | 5 | (-c, -r) | anti-transposed |
|
|
1236
|
+
* | 6 | (-r, -c) | rotated 180 degrees |
|
|
1237
|
+
* | 7 | (-c, r) | rotated 270 degrees |
|
|
1093
1238
|
*
|
|
1094
|
-
*
|
|
1095
|
-
*
|
|
1239
|
+
* The list is built from the cosines directly: dcmjs
|
|
1240
|
+
* rotateDirectionCosinesInPlane negates its result, so it never yields the
|
|
1241
|
+
* 180 degree orientation. See
|
|
1242
|
+
* https://github.com/cornerstonejs/cornerstone3D/issues/2959
|
|
1243
|
+
*
|
|
1244
|
+
* @param {Number[6]|String[6]} iop The row (0..2) and column (3..5) direction
|
|
1245
|
+
* cosines. DS strings are converted to numbers.
|
|
1246
|
+
* @return {Number[8][6]} The valid orientations, in the order of the table.
|
|
1096
1247
|
*/
|
|
1097
1248
|
function getValidOrientations(iop) {
|
|
1098
|
-
const
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
orientations[0] = iop;
|
|
1105
|
-
orientations[1] = flipIOP.h(iop);
|
|
1106
|
-
orientations[2] = flipIOP.v(iop);
|
|
1107
|
-
const iop90 = rotateDirectionCosinesInPlane(iop, Math.PI / 2);
|
|
1108
|
-
orientations[3] = iop90;
|
|
1109
|
-
orientations[4] = flipIOP.h(iop90);
|
|
1110
|
-
orientations[5] = flipIOP.v(iop90);
|
|
1111
|
-
orientations[6] = rotateDirectionCosinesInPlane(iop, Math.PI);
|
|
1112
|
-
orientations[7] = rotateDirectionCosinesInPlane(iop, 1.5 * Math.PI);
|
|
1113
|
-
return orientations;
|
|
1249
|
+
const numericIop = utilities$2.toNumber(iop);
|
|
1250
|
+
const r = numericIop.slice(0, 3);
|
|
1251
|
+
const c = numericIop.slice(3, 6);
|
|
1252
|
+
const negR = vec3.negate([0, 0, 0], r);
|
|
1253
|
+
const negC = vec3.negate([0, 0, 0], c);
|
|
1254
|
+
return [[...r, ...c], [...r, ...negC], [...negR, ...c], [...c, ...negR], [...c, ...r], [...negC, ...negR], [...negR, ...negC], [...negC, ...r]];
|
|
1114
1255
|
}
|
|
1115
1256
|
|
|
1116
1257
|
/**
|
|
1117
|
-
*
|
|
1258
|
+
* The pixel operation that aligns a SEG frame with its source image, per entry
|
|
1259
|
+
* of getValidOrientations. The aligned pixel (a, b) reads the SEG pixel (i, j),
|
|
1260
|
+
* where (i, j) is (a, b), or (b, a) when `transpose` is set, and then
|
|
1261
|
+
* `flipRows` and `flipColumns` mirror i and j.
|
|
1262
|
+
*/
|
|
1263
|
+
const ORIENTATION_ALIGNMENTS = [{
|
|
1264
|
+
transpose: false,
|
|
1265
|
+
flipRows: false,
|
|
1266
|
+
flipColumns: false
|
|
1267
|
+
}, {
|
|
1268
|
+
transpose: false,
|
|
1269
|
+
flipRows: true,
|
|
1270
|
+
flipColumns: false
|
|
1271
|
+
}, {
|
|
1272
|
+
transpose: false,
|
|
1273
|
+
flipRows: false,
|
|
1274
|
+
flipColumns: true
|
|
1275
|
+
}, {
|
|
1276
|
+
transpose: true,
|
|
1277
|
+
flipRows: true,
|
|
1278
|
+
flipColumns: false
|
|
1279
|
+
}, {
|
|
1280
|
+
transpose: true,
|
|
1281
|
+
flipRows: false,
|
|
1282
|
+
flipColumns: false
|
|
1283
|
+
}, {
|
|
1284
|
+
transpose: true,
|
|
1285
|
+
flipRows: true,
|
|
1286
|
+
flipColumns: true
|
|
1287
|
+
}, {
|
|
1288
|
+
transpose: false,
|
|
1289
|
+
flipRows: true,
|
|
1290
|
+
flipColumns: true
|
|
1291
|
+
}, {
|
|
1292
|
+
transpose: true,
|
|
1293
|
+
flipRows: false,
|
|
1294
|
+
flipColumns: true
|
|
1295
|
+
}];
|
|
1296
|
+
|
|
1297
|
+
/**
|
|
1298
|
+
* alignPixelDataWithSourceData - finds the orientation of a SEG frame in the
|
|
1299
|
+
* list from getValidOrientations, and returns the frame in the pixel order of
|
|
1300
|
+
* the source image.
|
|
1301
|
+
*
|
|
1302
|
+
* The result keeps the typed array type of the input, so 16-bit LABELMAP
|
|
1303
|
+
* values are not truncated. A frame that is not aligned already is copied in
|
|
1304
|
+
* one pass.
|
|
1118
1305
|
*
|
|
1119
|
-
* @param {Ndarray} pixelData2D - The
|
|
1120
|
-
* @param {Number[6]} iop - The orientation of the
|
|
1121
|
-
*
|
|
1122
|
-
* @param {Number}
|
|
1123
|
-
* @
|
|
1306
|
+
* @param {Ndarray} pixelData2D - The SEG frame, with shape [Rows, Columns].
|
|
1307
|
+
* @param {Number[6]|String[6]} iop - The orientation of the SEG frame. DS
|
|
1308
|
+
* strings are converted to numbers.
|
|
1309
|
+
* @param {Number[8][6]} orientations - The result of getValidOrientations.
|
|
1310
|
+
* @param {Number} tolerance - The tolerance of the IOP comparison.
|
|
1311
|
+
* @return {Ndarray|undefined} The aligned frame, with shape
|
|
1312
|
+
* [source Rows, source Columns], or undefined when the frame is not in-plane.
|
|
1124
1313
|
*/
|
|
1125
1314
|
function alignPixelDataWithSourceData(pixelData2D, iop, orientations, tolerance) {
|
|
1126
|
-
|
|
1315
|
+
const numericIop = utilities$2.toNumber(iop);
|
|
1316
|
+
const index = orientations.findIndex(orientation => utilities.isEqual(numericIop, orientation, tolerance));
|
|
1317
|
+
if (index === -1) {
|
|
1318
|
+
return undefined;
|
|
1319
|
+
}
|
|
1320
|
+
if (index === 0) {
|
|
1127
1321
|
return pixelData2D;
|
|
1128
|
-
} else if (utilities.isEqual(iop, orientations[1], tolerance)) {
|
|
1129
|
-
// Flipped vertically.
|
|
1130
|
-
|
|
1131
|
-
// Undo Flip
|
|
1132
|
-
return flipMatrix2D.v(pixelData2D);
|
|
1133
|
-
} else if (utilities.isEqual(iop, orientations[2], tolerance)) {
|
|
1134
|
-
// Flipped horizontally.
|
|
1135
|
-
|
|
1136
|
-
// Unfo flip
|
|
1137
|
-
return flipMatrix2D.h(pixelData2D);
|
|
1138
|
-
} else if (utilities.isEqual(iop, orientations[3], tolerance)) {
|
|
1139
|
-
//Rotated 90 degrees
|
|
1140
|
-
|
|
1141
|
-
// Rotate back
|
|
1142
|
-
return rotateMatrix902D(pixelData2D);
|
|
1143
|
-
} else if (utilities.isEqual(iop, orientations[4], tolerance)) {
|
|
1144
|
-
//Rotated 90 degrees and fliped horizontally.
|
|
1145
|
-
|
|
1146
|
-
// Undo flip and rotate back.
|
|
1147
|
-
return rotateMatrix902D(flipMatrix2D.h(pixelData2D));
|
|
1148
|
-
} else if (utilities.isEqual(iop, orientations[5], tolerance)) {
|
|
1149
|
-
// Rotated 90 degrees and fliped vertically
|
|
1150
|
-
|
|
1151
|
-
// Unfo flip and rotate back.
|
|
1152
|
-
return rotateMatrix902D(flipMatrix2D.v(pixelData2D));
|
|
1153
|
-
} else if (utilities.isEqual(iop, orientations[6], tolerance)) {
|
|
1154
|
-
// Rotated 180 degrees. // TODO -> Do this more effeciently, there is a 1:1 mapping like 90 degree rotation.
|
|
1155
|
-
|
|
1156
|
-
return rotateMatrix902D(rotateMatrix902D(pixelData2D));
|
|
1157
|
-
} else if (utilities.isEqual(iop, orientations[7], tolerance)) {
|
|
1158
|
-
// Rotated 270 degrees
|
|
1159
|
-
|
|
1160
|
-
// Rotate back.
|
|
1161
|
-
return rotateMatrix902D(rotateMatrix902D(rotateMatrix902D(pixelData2D)));
|
|
1162
1322
|
}
|
|
1323
|
+
const _ORIENTATION_ALIGNMEN = ORIENTATION_ALIGNMENTS[index],
|
|
1324
|
+
transpose = _ORIENTATION_ALIGNMEN.transpose,
|
|
1325
|
+
flipRows = _ORIENTATION_ALIGNMEN.flipRows,
|
|
1326
|
+
flipColumns = _ORIENTATION_ALIGNMEN.flipColumns;
|
|
1327
|
+
const _pixelData2D$shape = _slicedToArray(pixelData2D.shape, 2),
|
|
1328
|
+
rows = _pixelData2D$shape[0],
|
|
1329
|
+
columns = _pixelData2D$shape[1];
|
|
1330
|
+
const alignedRows = transpose ? columns : rows;
|
|
1331
|
+
const alignedColumns = transpose ? rows : columns;
|
|
1332
|
+
const TypedArrayConstructor = pixelData2D.data.constructor;
|
|
1333
|
+
const aligned = ndarray(new TypedArrayConstructor(rows * columns), [alignedRows, alignedColumns]);
|
|
1334
|
+
for (let a = 0; a < alignedRows; a++) {
|
|
1335
|
+
for (let b = 0; b < alignedColumns; b++) {
|
|
1336
|
+
let i = transpose ? b : a;
|
|
1337
|
+
let j = transpose ? a : b;
|
|
1338
|
+
if (flipRows) {
|
|
1339
|
+
i = rows - 1 - i;
|
|
1340
|
+
}
|
|
1341
|
+
if (flipColumns) {
|
|
1342
|
+
j = columns - 1 - j;
|
|
1343
|
+
}
|
|
1344
|
+
aligned.set(a, b, pixelData2D.get(i, j));
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
return aligned;
|
|
1163
1348
|
}
|
|
1164
1349
|
function getSegmentMetadata(multiframe, seriesInstanceUid) {
|
|
1165
1350
|
const segmentSequence = multiframe.SegmentSequence;
|
|
@@ -1259,10 +1444,10 @@ function calculateCentroid(imageIdIndexBufferIndex, multiframe, metadataProvider
|
|
|
1259
1444
|
let worldYAcc = 0;
|
|
1260
1445
|
let worldZAcc = 0;
|
|
1261
1446
|
let count = 0;
|
|
1262
|
-
for (const
|
|
1263
|
-
var
|
|
1264
|
-
const imageIdIndex =
|
|
1265
|
-
const bufferIndices =
|
|
1447
|
+
for (const _ref0 of Object.entries(imageIdIndexBufferIndex)) {
|
|
1448
|
+
var _ref1 = _slicedToArray(_ref0, 2);
|
|
1449
|
+
const imageIdIndex = _ref1[0];
|
|
1450
|
+
const bufferIndices = _ref1[1];
|
|
1266
1451
|
const z = Number(imageIdIndex);
|
|
1267
1452
|
if (!bufferIndices || bufferIndices.length === 0) {
|
|
1268
1453
|
continue;
|
|
@@ -1320,4 +1505,4 @@ const Segmentation = {
|
|
|
1320
1505
|
fillSegmentation
|
|
1321
1506
|
};
|
|
1322
1507
|
|
|
1323
|
-
export { _createSegFromImages, _getLabelmapsFromReferencedFrameIndicies, alignPixelDataWithSourceData, calculateCentroid, checkSEGsOverlapping, Segmentation as default, fillSegmentation, findReferenceSourceImageId, generateSegmentation, generateToolState, getImageIdOfSourceImageBySourceImageSequence, getImageIdOfSourceImagebyGeometry, getSegmentIndex, getSegmentMetadata, getUnpackedChunks, getUnpackedOffsetAndLength, getValidOrientations, insertOverlappingPixelDataPlanar, insertPixelDataPlanar, readFromUnpackedChunks, unpackPixelData };
|
|
1508
|
+
export { _createSegFromImages, _getLabelmapsFromReferencedFrameIndicies, alignPixelDataWithSourceData, calculateCentroid, checkSEGsOverlapping, Segmentation as default, fillSegmentation, findReferenceSourceImageId, generateSegmentation, generateToolState, getImageIdOfSourceImageBySourceImageSequence, getImageIdOfSourceImagebyGeometry, getSegmentIndex, getSegmentIndexForLabel, getSegmentMetadata, getUnpackedChunks, getUnpackedOffsetAndLength, getValidOrientations, insertOverlappingPixelDataPlanar, insertPixelDataPlanar, readFromUnpackedChunks, unpackPixelData };
|