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

Merged
merged 27 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
534ddec
fix: method signature
pedrokohler Feb 5, 2025
be0dfbd
feat: functional overlapping segs per image
pedrokohler Feb 5, 2025
2fbf21b
refactor: improve nomenclature and separate code
pedrokohler Feb 5, 2025
c591aaa
fix: test file name
pedrokohler Feb 5, 2025
4b6a761
fix: segmentationStack example
pedrokohler Feb 5, 2025
3113419
feat: make stack viewport work
pedrokohler Feb 12, 2025
ec8b82a
feat: use only necessary duplicated images
pedrokohler Feb 12, 2025
ce22e68
Merge branch 'main' of github.com:cornerstonejs/cornerstone3D into fe…
pedrokohler Feb 12, 2025
8b6ebfb
fix: wrong imports
pedrokohler Feb 12, 2025
7d3304e
chore: remove useless comment
pedrokohler Feb 12, 2025
5188892
fix: empty image filter
pedrokohler Feb 12, 2025
12b61ca
some wip
sedghi Feb 18, 2025
cfd78e0
Merge branch 'main' of github.com:cornerstonejs/cornerstone3D into fe…
pedrokohler Mar 7, 2025
5352864
fix: type error
pedrokohler Mar 7, 2025
033ef09
feat: skip overlapping checks
pedrokohler Mar 11, 2025
691cb0f
Merge remote-tracking branch 'origin' into feat-overlapping-segs
pedrokohler Mar 12, 2025
ea4c5b8
run test again
pedrokohler Mar 12, 2025
a67ccb7
fix: jest test environment
pedrokohler Mar 12, 2025
e351d05
fix: labelmap data modified callback
pedrokohler Mar 13, 2025
b52d1fa
temp: simulate test on example
pedrokohler Mar 13, 2025
c93c014
fix
sedghi Mar 13, 2025
7012781
run
sedghi Mar 13, 2025
b92d191
Revert "temp: simulate test on example"
pedrokohler Mar 13, 2025
ef76281
fix
sedghi Mar 13, 2025
86bbf31
Merge branch 'feat-overlapping-segs' of https://github.com/pedrokohle…
sedghi Mar 13, 2025
896a595
run checks again
pedrokohler Mar 13, 2025
7906c81
fix: ignore prettier
pedrokohler Mar 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@ function generateToolState(
function createFromDICOMSegBuffer(
referencedImageIds,
arrayBuffer,
{ metadataProvider, skipOverlapping = false, tolerance = 1e-3 }
{ metadataProvider, tolerance = 1e-3 }
) {
return createLabelmapsFromBufferInternal(
referencedImageIds,
arrayBuffer,
metadataProvider,
{
skipOverlapping,
tolerance
}
);
Expand Down
Loading