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
23 changes: 23 additions & 0 deletions packages/adapters/examples/segmentationStack/demo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
/* eslint-disable */
const dicomMap = new Map();

dicomMap.set(
"1.3.6.1.4.1.14519.5.2.1.3671.4754.298665348758363466150039312520",
{
fetchDicom: {
StudyInstanceUID:
"1.3.6.1.4.1.14519.5.2.1.3671.4754.298665348758363466150039312520",
SeriesInstanceUID:
"1.3.6.1.4.1.14519.5.2.1.3671.4754.235188122843915982710753948536",
wadoRsRoot: "https://d14fa38qiwhyfd.cloudfront.net/dicomweb"
},
fetchSegmentation: {
StudyInstanceUID:
"1.3.6.1.4.1.14519.5.2.1.3671.4754.298665348758363466150039312520",
SeriesInstanceUID:
"1.2.276.0.7230010.3.1.3.1426846371.15380.1513205183.303",
SOPInstanceUID:
"1.2.276.0.7230010.3.1.4.1426846371.15380.1513205183.304",
wadoRsRoot: "https://d14fa38qiwhyfd.cloudfront.net/dicomweb"
}
}
);

dicomMap.set(
"1.3.6.1.4.1.14519.5.2.1.256467663913010332776401703474716742458",
{
Expand All @@ -22,6 +44,7 @@ dicomMap.set(
}
}
);

dicomMap.set("1.3.12.2.1107.5.2.32.35162.30000015050317233592200000046", {
fetchDicom: {
StudyInstanceUID:
Expand Down
8 changes: 2 additions & 6 deletions packages/adapters/examples/segmentationStack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ console.warn(
"Click on index.ts to open source code for this example --------->"
);

const { Enums: csEnums, RenderingEngine, utilities: csUtilities } = cornerstone;
const { segmentation: csToolsSegmentation } = cornerstoneTools;
import {
readDicom,
Expand All @@ -32,14 +31,11 @@ import {
loadSegmentation,
exportSegmentation,
restart,
getSegmentationIds,
handleFileSelect,
handleDragOver,
createSegmentation
createEmptySegmentation
} from "../segmentationVolume/utils";

const referenceImageIds: string[] = [];
const segImageIds: string[] = [];
// ======== Set up page ======== //

setTitleAndDescription(
Expand Down Expand Up @@ -194,7 +190,7 @@ addButtonToToolbar({
id: "CREATE_SEGMENTATION",
title: "Create Empty SEG",
onClick: async () => {
await createSegmentation(state);
await createEmptySegmentation(state);
createSegmentationRepresentation();
},
container: group2
Expand Down
4 changes: 2 additions & 2 deletions packages/adapters/examples/segmentationVolume/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
handleFileSelect,
handleDragOver,
restart,
createSegmentation
createEmptySegmentation
} from "../segmentationVolume/utils";
import addDropDownToToolbar from "../../../../utils/demo/helpers/addDropdownToToolbar";

Expand Down Expand Up @@ -227,7 +227,7 @@ addButtonToToolbar({
onClick: async () => {
const segmentationId = cornerstone.utilities.uuidv4();
state.segmentationId = segmentationId;
await createSegmentation(state);
await createEmptySegmentation(state);
createSegmentationRepresentation();
updateSegmentationDropdown();
},
Expand Down
42 changes: 22 additions & 20 deletions packages/adapters/examples/segmentationVolume/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function readDicom(files: FileList, state) {
}
}

export async function createSegmentation(state) {
export async function createEmptySegmentation(state) {
const { referenceImageIds, segmentationId } = state;

const derivedSegmentationImages =
Expand All @@ -44,6 +44,25 @@ export async function createSegmentation(state) {
]);
}

export async function createSegmentation({ state, labelMapImages }) {
const { segmentationId } = state;

const imageIds = labelMapImages?.flat().map(image => image.imageId);

csToolsSegmentation.addSegmentations([
{
segmentationId,
representation: {
type: cornerstoneTools.Enums.SegmentationRepresentations
.Labelmap,
data: {
imageIds
}
}
}
]);
}

export async function readSegmentation(file: File, state) {
const imageId = wadouri.fileManager.add(file);
const image = await imageLoader.loadAndCacheImage(imageId);
Expand All @@ -67,7 +86,7 @@ export async function readSegmentation(file: File, state) {
export async function loadSegmentation(arrayBuffer: ArrayBuffer, state) {
const { referenceImageIds, skipOverlapping, segmentationId } = state;

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

await createSegmentation(state);

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

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

const labelmapImagesNonOverlapping = generateToolState.labelMapImages[0];

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);
}
await createSegmentation({ state, labelMapImages });
}

export async function exportSegmentation(state) {
Expand Down
3 changes: 2 additions & 1 deletion packages/adapters/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ module.exports = {
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
Loading