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

fix(segmentation): fix segmentation for video viewports #1595

Merged
merged 7 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion common/reviews/api/ai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Corners } from '@kitware/vtk.js/Interaction/Widgets/OrientationMarkerWi
import type { IColorMapPreset } from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction/ColorMaps';
import type { mat3 } from 'gl-matrix';
import { mat4 } from 'gl-matrix';
import { PixelDataTypedArray as PixelDataTypedArray_2 } from 'packages/core/dist/esm/types';
import type { Range as Range_2 } from '@kitware/vtk.js/types';
import { vec3 } from 'gl-matrix';
import type vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/RenderingEngine/VideoViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ class VideoViewport extends Viewport {

let rowCosines = imagePlaneModule.rowCosines as Point3;
let columnCosines = imagePlaneModule.columnCosines as Point3;
const usingDefaultValues = imagePlaneModule.usingDefaultValues;

// if null or undefined
if (rowCosines == null || columnCosines == null) {
if (usingDefaultValues || rowCosines == null || columnCosines == null) {
rowCosines = [1, 0, 0] as Point3;
columnCosines = [0, 1, 0] as Point3;
}
Expand Down Expand Up @@ -836,6 +837,9 @@ class VideoViewport extends Viewport {
return false;
}
const match = referencedImageId.match(VideoViewport.frameRangeExtractor);
if (!match) {
return true;
}
if (!match[2]) {
return true;
}
Expand Down Expand Up @@ -1130,16 +1134,17 @@ class VideoViewport extends Viewport {
public addImages(stackInputs: IStackInput[]) {
const actors = this.getActors();
stackInputs.forEach((stackInput) => {
const image = cache.getImage(stackInput.imageId);
const { imageId, ...rest } = stackInput;
const image = cache.getImage(imageId);

const imageActor = this.createActorMapper(image);
const uid = stackInput.actorUID ?? uuidv4();
if (imageActor) {
actors.push({ uid, actor: imageActor });
actors.push({ uid, actor: imageActor, referencedId: imageId, ...rest });
if (stackInput.callback) {
stackInput.callback({
imageActor: imageActor as unknown as ImageActor,
imageId: stackInput.imageId,
imageId,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
IStackViewport,
IStackInput,
IRenderingEngine,
IVideoViewport,
} from '../../types';

/**
Expand All @@ -21,15 +22,14 @@ function addImageSlicesToViewports(
stackInputs: IStackInput[],
viewportIds: string[]
): Promise<void> {
// Check if all viewports are volumeViewports
for (const viewportId of viewportIds) {
const viewport = renderingEngine.getStackViewport(viewportId);
const viewport = renderingEngine.getViewport(viewportId) as IStackViewport;

if (!viewport) {
throw new Error(`Viewport with Id ${viewportId} does not exist`);
}

// if not instance of BaseVolumeViewport, throw
// if the viewport does not support addImages, log a warning and skip
if (!viewport.addImages) {
console.warn(
`Viewport with Id ${viewportId} does not have addImages. Cannot add image segmentation to this viewport.`
Expand All @@ -40,7 +40,9 @@ function addImageSlicesToViewports(
}

viewportIds.forEach((viewportId) => {
const viewport = renderingEngine.getStackViewport(viewportId);
const viewport = renderingEngine.getViewport(viewportId) as
| IStackViewport
| IVideoViewport;
viewport.addImages(stackInputs);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function (
canvasImageDataData: Uint8ClampedArray
): void {
let start = now();
const pixelData = image.voxelManager.getScalarData();
const pixelData = image.getPixelData();

image.stats.lastGetPixelDataTime = now() - start;

Expand Down
13 changes: 8 additions & 5 deletions packages/tools/examples/videoSegmentation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const {
Enums: csToolsEnums,
BrushTool,
segmentation,
utilities: cstUtils,
} = cornerstoneTools;

const { MouseBindings } = csToolsEnums;
Expand Down Expand Up @@ -223,10 +222,14 @@ async function run() {
addVideoTime(viewportGrid, viewport);
// We need the map on all image ids
const allImageIds = viewport.getImageIds();
const segImages = await imageLoader.createAndCacheDerivedImages(allImageIds, {
skipCreateBuffer: true,
onCacheAdd: csUtils.VoxelManager.addInstanceToImage,
});
const firstImage = allImageIds[0];
const segImages = await imageLoader.createAndCacheDerivedImages(
[firstImage],
{
skipCreateBuffer: true,
onCacheAdd: csUtils.VoxelManager.addInstanceToImage,
}
);

const segmentationImageIds = segImages.map((it) => it.imageId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ function _setLabelmapColorAndOpacity(
const labelmapActor = labelmapActorEntry.actor as vtkVolume;

// @ts-ignore - fix type in vtk
const { preLoad } = labelmapActor.get('preLoad') || { preLoad: null };
const { preLoad } = labelmapActor.get?.('preLoad') || { preLoad: null };

if (preLoad) {
preLoad({ cfun, ofun, actor: labelmapActor });
Expand Down
Loading