Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adapters): overlapping segs with labelmap images #1815

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
12 changes: 7 additions & 5 deletions packages/adapters/examples/segmentationStack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const state = {
toolGroup: null,
toolGroupId: "MY_TOOL_GROUP_ID",
viewportIds: ["CT_AXIAL"],
segmentationId: "LOAD_SEG_ID:" + cornerstone.utilities.uuidv4(),
segmentationIds: [],
referenceImageIds: [],
segImageIds: [],
skipOverlapping: false,
Expand Down Expand Up @@ -135,10 +135,12 @@ function loadDicom() {
}

function createSegmentationRepresentation() {
csToolsSegmentation.addLabelmapRepresentationToViewport(
state.viewportIds[0],
[{ segmentationId: state.segmentationId }]
);
for (const segmentationId of state.segmentationIds) {
csToolsSegmentation.addLabelmapRepresentationToViewport(
state.viewportIds[0],
[{ segmentationId }]
);
}
}

// ============================= //
Expand Down
18 changes: 10 additions & 8 deletions packages/adapters/examples/segmentationVolume/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const state = {
toolGroupId: "MY_TOOL_GROUP_ID",
viewportIds: ["CT_AXIAL", "CT_SAGITTAL", "CT_CORONAL"],
volumeId: "",
segmentationId: "LOAD_SEG_ID:" + cornerstone.utilities.uuidv4(),
segmentationIds: [],
referenceImageIds: [],
skipOverlapping: false,
segImageIds: [],
Expand Down Expand Up @@ -161,13 +161,15 @@ async function loadDicom() {
}

function createSegmentationRepresentation() {
const segMap = {
[state.viewportIds[0]]: [{ segmentationId: state.segmentationId }],
[state.viewportIds[1]]: [{ segmentationId: state.segmentationId }],
[state.viewportIds[2]]: [{ segmentationId: state.segmentationId }]
};

csToolsSegmentation.addLabelmapRepresentationToViewportMap(segMap);
for (const segmentationId of state.segmentationIds) {
const segMap = {
[state.viewportIds[0]]: [{ segmentationId }],
[state.viewportIds[1]]: [{ segmentationId }],
[state.viewportIds[2]]: [{ segmentationId }]
};

csToolsSegmentation.addLabelmapRepresentationToViewportMap(segMap);
}
}
// ============================= //
addButtonToToolbar({
Expand Down
33 changes: 19 additions & 14 deletions packages/adapters/examples/segmentationVolume/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function readSegmentation(file: File, state) {
}

export async function loadSegmentation(arrayBuffer: ArrayBuffer, state) {
const { referenceImageIds, skipOverlapping, segmentationId } = state;
const { referenceImageIds, skipOverlapping, segmentationIds } = state;

const generateToolState =
await Cornerstone3D.Segmentation.createFromDICOMSegBuffer(
Expand All @@ -77,23 +77,28 @@ export async function loadSegmentation(arrayBuffer: ArrayBuffer, state) {
}
);

await createSegmentation(state);
for (let i = 0; i < generateToolState.labelMapImages.length; i++) {
const segmentationId = "LOAD_SEG_ID:" + cornerstone.utilities.uuidv4();
segmentationIds.push(segmentationId);
await createSegmentation({ ...state, segmentationId });

const segmentation =
csToolsSegmentation.state.getSegmentation(segmentationId);
const segmentation =
csToolsSegmentation.state.getSegmentation(segmentationId);

const { imageIds } = segmentation.representationData.Labelmap;
const derivedSegmentationImages = imageIds.map(imageId =>
cache.getImage(imageId)
);
const { imageIds } = segmentation.representationData.Labelmap;
const derivedSegmentationImages = imageIds.map(imageId =>
cache.getImage(imageId)
);

const labelmapImagesNonOverlapping = generateToolState.labelMapImages[0];
const labelmapImagesNonOverlapping =
generateToolState.labelMapImages[i];

for (let i = 0; i < derivedSegmentationImages.length; i++) {
const voxelManager = derivedSegmentationImages[i].voxelManager;
const scalarData = voxelManager.getScalarData();
scalarData.set(labelmapImagesNonOverlapping[i].getPixelData());
voxelManager.setScalarData(scalarData);
for (let j = 0; j < derivedSegmentationImages.length; j++) {
const voxelManager = derivedSegmentationImages[j].voxelManager;
const scalarData = voxelManager.getScalarData();
scalarData.set(labelmapImagesNonOverlapping[j].getPixelData());
voxelManager.setScalarData(scalarData);
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions packages/adapters/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable */
const base = require("../../jest.config.base.js");
const path = require("path");

module.exports = {
...base,
displayName: "adapters",
moduleNameMapper: {
"^@cornerstonejs/(.*)$": path.resolve(__dirname, "../$1/src")
},
testEnvironment: undefined
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const checkHasOverlapping = ({ largerArray, currentTestedArray, newArray }) =>
largerArray.some((_, currentImageIndex) => {
const originalImagePixelData = currentTestedArray[currentImageIndex];

const newImagePixelData = newArray[currentImageIndex];

if (!originalImagePixelData || !newImagePixelData) {
return false;
}

return originalImagePixelData.some(
(originalPixel, currentPixelIndex) => {
const newPixel = newImagePixelData[currentPixelIndex];
return originalPixel && newPixel;
}
);
});

export const compactMergeSegmentDataWithoutInformationLoss = ({
arrayOfSegmentData,
newSegmentData
}) => {
if (arrayOfSegmentData.length === 0) {
arrayOfSegmentData.push(newSegmentData);
return;
}

for (
let currentTestedIndex = 0;
currentTestedIndex < arrayOfSegmentData.length;
currentTestedIndex++
) {
const currentTestedArray = arrayOfSegmentData[currentTestedIndex];

const originalArrayIsLarger =
currentTestedArray.length > newSegmentData.length;
const largerArray = originalArrayIsLarger
? currentTestedArray
: newSegmentData;

const hasOverlapping = checkHasOverlapping({
currentTestedArray,
largerArray,
newArray: newSegmentData
});

if (hasOverlapping) {
continue;
}

largerArray.forEach((_, currentImageIndex) => {
const originalImagePixelData =
currentTestedArray[currentImageIndex];
const newImagePixelData = newSegmentData[currentImageIndex];

if (
(!originalImagePixelData && !newImagePixelData) ||
!newImagePixelData
) {
return;
}

if (!originalImagePixelData) {
currentTestedArray[currentImageIndex] = newImagePixelData;
return;
}

const mergedPixelData = originalImagePixelData.map(
(originalPixel, currentPixelIndex) => {
const newPixel = newImagePixelData[currentPixelIndex];
return originalPixel || newPixel;
}
);

currentTestedArray[currentImageIndex] = mergedPixelData;
});
return;
}

arrayOfSegmentData.push(newSegmentData);
};
Loading