Skip to content

Commit f1392ee

Browse files
scouserscouser
authored andcommitted
initial commit for fixing video segmentations
1 parent 5c61d76 commit f1392ee

File tree

3 files changed

+121
-111
lines changed

3 files changed

+121
-111
lines changed

packages/tools/examples/videoSegmentation/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ async function run() {
223223
// We need the map on all image ids
224224
const allImageIds = viewport.getImageIds();
225225
const firstImage = allImageIds[0];
226-
const segImages = await imageLoader.createAndCacheDerivedImages(
227-
[firstImage],
226+
const segImages = await imageLoader.createAndCacheDerivedLabelmapImages(
227+
allImageIds,
228228
{
229229
skipCreateBuffer: true,
230230
onCacheAdd: csUtils.VoxelManager.addInstanceToImage,

packages/tools/src/eventListeners/segmentation/imageChangeEventListener.ts

Lines changed: 95 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,22 @@ function _imageChangeEventListener(evt) {
110110
// is there any extra actor that needs to be removed
111111
// or any actor that needs to be added (which it is added later down), but remove
112112
// it here if it is not needed
113-
labelmapActors.forEach((actor) => {
114-
// if cannot find a representation for this actor means it has stuck around
115-
// form previous renderings and should be removed
116-
const validActor = labelmapRepresentations.find((representation) => {
117-
const derivedImageId = getCurrentLabelmapImageIdForViewport(
118-
viewportId,
119-
representation.segmentationId
120-
);
121-
122-
return derivedImageId === actor.referencedId;
123-
});
124-
125-
if (!validActor) {
126-
viewport.removeActors([actor.uid]);
127-
}
128-
});
113+
// labelmapActors.forEach((actor) => {
114+
// // if cannot find a representation for this actor means it has stuck around
115+
// // form previous renderings and should be removed
116+
// const validActor = labelmapRepresentations.find((representation) => {
117+
// const derivedImageId = getCurrentLabelmapImageIdForViewport(
118+
// viewportId,
119+
// representation.segmentationId
120+
// );
121+
122+
// return derivedImageId === actor.referencedId;
123+
// });
124+
125+
// if (!validActor) {
126+
// viewport.removeActors([actor.uid]);
127+
// }
128+
// });
129129

130130
labelmapRepresentations.forEach((representation) => {
131131
const { segmentationId } = representation;
@@ -149,85 +149,86 @@ function _imageChangeEventListener(evt) {
149149
}
150150

151151
// re-use the old labelmap actor for the new image labelmap for speed and memory
152-
const segmentationActorInput = actors.find(
153-
(actor) => actor.referencedId === derivedImageId
154-
);
155-
156-
if (!segmentationActorInput) {
157-
// i guess we need to create here
158-
const { dimensions, spacing, direction } =
159-
viewport.getImageDataMetadata(derivedImage);
160-
161-
const currentImage =
162-
cache.getImage(currentImageId) ||
163-
({
164-
imageId: currentImageId,
165-
} as Types.IImage);
166-
167-
const { origin: currentOrigin } =
168-
viewport.getImageDataMetadata(currentImage);
169-
170-
// IMPORTANT: We need to make sure that the origin of the segmentation
171-
// is the same as the current image origin. This is because due to some
172-
// floating point precision issues, when coming from volume to stack
173-
// the origin of the segmentation can be slightly different from the
174-
// current image origin. This can cause the segmentation to be rendered
175-
// in the wrong location.
176-
// Todo: This will not work for segmentations that are not in the same frame
177-
// of reference or derived from the same image. This can happen when we have
178-
// a segmentation that happens to exist in the same space as the image but is
179-
// not derived from it. We need to find a way to handle this case, but don't think
180-
// it makes sense to do it for the stack viewport, as the volume viewport is designed to handle this case.
181-
const originToUse = currentOrigin;
182-
const constructor = derivedImage.voxelManager.getConstructor();
183-
const newPixelData = derivedImage.voxelManager.getScalarData();
184-
185-
const scalarArray = vtkDataArray.newInstance({
186-
name: 'Pixels',
187-
numberOfComponents: 1,
188-
// @ts-expect-error
189-
values: new constructor(newPixelData),
190-
});
191-
192-
const imageData = vtkImageData.newInstance();
193-
194-
imageData.setDimensions(dimensions[0], dimensions[1], 1);
195-
imageData.setSpacing(spacing);
196-
imageData.setDirection(direction);
197-
imageData.setOrigin(originToUse);
198-
imageData.getPointData().setScalars(scalarArray);
199-
imageData.modified();
200-
201-
viewport.addImages([
202-
{
203-
imageId: derivedImageId,
204-
representationUID: `${segmentationId}-${SegmentationRepresentations.Labelmap}`,
205-
callback: ({ imageActor }) => {
206-
imageActor.getMapper().setInputData(imageData);
207-
},
208-
},
209-
]);
210-
211-
triggerSegmentationRender(viewportId);
212-
return;
152+
// there is only one actor in this case. and its referenceId does not match with derivedImageId
153+
// in 1.0 we did not have this logic of filtering actors by their reference ids
154+
const segmentationActorInput = actors.find((actor) => actor);
155+
156+
// if (!segmentationActorInput) {
157+
// // i guess we need to create here
158+
// const { dimensions, spacing, direction } =
159+
// viewport.getImageDataMetadata(derivedImage);
160+
161+
// const currentImage =
162+
// cache.getImage(currentImageId) ||
163+
// ({
164+
// imageId: currentImageId,
165+
// } as Types.IImage);
166+
167+
// const { origin: currentOrigin } =
168+
// viewport.getImageDataMetadata(currentImage);
169+
170+
// // IMPORTANT: We need to make sure that the origin of the segmentation
171+
// // is the same as the current image origin. This is because due to some
172+
// // floating point precision issues, when coming from volume to stack
173+
// // the origin of the segmentation can be slightly different from the
174+
// // current image origin. This can cause the segmentation to be rendered
175+
// // in the wrong location.
176+
// // Todo: This will not work for segmentations that are not in the same frame
177+
// // of reference or derived from the same image. This can happen when we have
178+
// // a segmentation that happens to exist in the same space as the image but is
179+
// // not derived from it. We need to find a way to handle this case, but don't think
180+
// // it makes sense to do it for the stack viewport, as the volume viewport is designed to handle this case.
181+
// const originToUse = currentOrigin;
182+
// const constructor = derivedImage.voxelManager.getConstructor();
183+
// const newPixelData = derivedImage.voxelManager.getScalarData();
184+
185+
// const scalarArray = vtkDataArray.newInstance({
186+
// name: 'Pixels',
187+
// numberOfComponents: 1,
188+
// // @ts-expect-error
189+
// values: new constructor(newPixelData),
190+
// });
191+
192+
// const imageData = vtkImageData.newInstance();
193+
194+
// imageData.setDimensions(dimensions[0], dimensions[1], 1);
195+
// imageData.setSpacing(spacing);
196+
// imageData.setDirection(direction);
197+
// imageData.setOrigin(originToUse);
198+
// imageData.getPointData().setScalars(scalarArray);
199+
// imageData.modified();
200+
201+
// viewport.addImages([
202+
// {
203+
// imageId: derivedImageId,
204+
// representationUID: `${segmentationId}-${SegmentationRepresentations.Labelmap}`,
205+
// callback: ({ imageActor }) => {
206+
// imageActor.getMapper().setInputData(imageData);
207+
// },
208+
// },
209+
// ]);
210+
211+
// triggerSegmentationRender(viewportId);
212+
// return;
213+
// } else {
214+
// // if actor found
215+
// // update the image data
216+
217+
// }
218+
219+
const segmentationImageData = segmentationActorInput.actor
220+
.getMapper()
221+
.getInputData();
222+
223+
if (segmentationImageData.setDerivedImage) {
224+
// Update the derived image data, whether vtk or other as appropriate
225+
// to the actor(s) displaying the data.
226+
segmentationImageData.setDerivedImage(derivedImage);
213227
} else {
214-
// if actor found
215-
// update the image data
216-
217-
const segmentationImageData = segmentationActorInput.actor
218-
.getMapper()
219-
.getInputData();
220-
221-
if (segmentationImageData.setDerivedImage) {
222-
// Update the derived image data, whether vtk or other as appropriate
223-
// to the actor(s) displaying the data.
224-
segmentationImageData.setDerivedImage(derivedImage);
225-
} else {
226-
utilities.updateVTKImageDataWithCornerstoneImage(
227-
segmentationImageData,
228-
derivedImage
229-
);
230-
}
228+
utilities.updateVTKImageDataWithCornerstoneImage(
229+
segmentationImageData,
230+
derivedImage
231+
);
231232
}
232233

233234
viewport.render();

packages/tools/src/stateManagement/segmentation/SegmentationStateManager.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -451,21 +451,30 @@ export default class SegmentationStateManager {
451451
updateCallback
452452
): string | undefined {
453453
const currentImageId = viewport.getCurrentImageId();
454-
455-
let viewableLabelmapImageIdFound = false;
456-
for (const labelmapImageId of labelmapImageIds) {
457-
const viewableImageId = viewport.isReferenceViewable(
458-
{ referencedImageId: labelmapImageId },
459-
{ asOverlay: true }
460-
);
461-
462-
if (viewableImageId) {
463-
viewableLabelmapImageIdFound = true;
464-
this._stackLabelmapImageIdReferenceMap
465-
.get(segmentationId)
466-
.set(currentImageId, labelmapImageId);
467-
}
468-
}
454+
const allImages = viewport.getImageIds();
455+
456+
const idx = allImages.findIndex((s) => s === currentImageId);
457+
const labelmapImageId = labelmapImageIds[idx];
458+
459+
const viewableLabelmapImageIdFound = true;
460+
this._stackLabelmapImageIdReferenceMap
461+
.get(segmentationId)
462+
.set(currentImageId, labelmapImageId);
463+
464+
// let viewableLabelmapImageIdFound = false;
465+
// for (const labelmapImageId of labelmapImageIds) {
466+
// const viewableImageId = viewport.isReferenceViewable(
467+
// { referencedImageId: labelmapImageId },
468+
// { asOverlay: true }
469+
// );
470+
471+
// if (viewableImageId) {
472+
// viewableLabelmapImageIdFound = true;
473+
// this._stackLabelmapImageIdReferenceMap
474+
// .get(segmentationId)
475+
// .set(currentImageId, labelmapImageId);
476+
// }
477+
// }
469478

470479
if (updateCallback) {
471480
updateCallback(viewport, segmentationId, labelmapImageIds);

0 commit comments

Comments
 (0)