All files / tools/src/utilities/segmentation/growCut runOneClickGrowCut.ts

1.37% Statements 2/145
0% Branches 0/85
0% Functions 0/6
1.4% Lines 2/142

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378                              428x                                             428x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
import { utilities as csUtils, cache, volumeLoader } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
import { run } from './runGrowCut';
import type { GrowCutOptions } from './runGrowCut';
import {
  POSITIVE_SEED_LABEL,
  NEGATIVE_SEED_LABEL,
  DEFAULT_NEIGHBORHOOD_RADIUS,
  DEFAULT_POSITIVE_STD_DEV_MULTIPLIER,
  DEFAULT_NEGATIVE_STD_DEV_MULTIPLIER,
  DEFAULT_NEGATIVE_SEED_MARGIN,
  DEFAULT_NEGATIVE_SEEDS_COUNT,
  MAX_NEGATIVE_SEED_ATTEMPTS_MULTIPLIER,
} from './constants';
 
const { transformWorldToIndex } = csUtils;
 
type GrowCutOneClickOptions = GrowCutOptions & {
  // Radius of the neighborhood (in voxels) around the click point used to calculate initial statistics (mean, stdDev). E.g., 1 means a 3x3x3 neighborhood.
  initialNeighborhoodRadius?: number;
  // Multiplier (k) for standard deviation used to define the positive seed intensity range (mean +/- k * stdDev).
  positiveStdDevMultiplier?: number;
  // Multiplier (negK) for standard deviation used to define negative seeds. A voxel is considered negative if its intensity is further than negK * stdDev away from the mean intensity of the identified positive seeds.
  negativeStdDevMultiplier?: number;
  // Margin (in voxels) around the bounding box of positive seeds where negative seeds are sampled.
  negativeSeedMargin?: number;
  // Target number of negative seeds to sample.
  negativeSeedsTargetPatches?: number;
  // The value assigned to positive seeds in the labelmap.
  positiveSeedValue?: number;
  // The value assigned to negative seeds in the labelmap.
  negativeSeedValue?: number;
  seeds?: {
    positiveSeedIndices: Set<number>;
    negativeSeedIndices: Set<number>;
  };
};
 
const MAX_POSITIVE_SEEDS = 100000; // Maximum number of positive seeds to prevent excessive growth
 
/**
 * Calculates positive and negative seed indices for the GrowCut algorithm based on a single click.
 * Does not modify any labelmap volume.
 * @param referencedVolume - Referenced volume
 * @param worldPosition - Coordinate where the user clicked in world space
 * @param options - Configuration options for seed generation
 * @returns An object containing sets of positive and negative seed indices, or null if seeding fails.
 */
function calculateGrowCutSeeds(
  referencedVolume: Types.IImageVolume,
  worldPosition: Types.Point3,
  options?: GrowCutOneClickOptions
): {
  positiveSeedIndices: Set<number>;
  negativeSeedIndices: Set<number>;
} | null {
  const { dimensions, imageData: refImageData } = referencedVolume;
  const [width, height, numSlices] = dimensions;
  const referenceVolumeVoxelManager = referencedVolume.voxelManager;
  const scalarData = referenceVolumeVoxelManager.getCompleteScalarDataArray();
  const numPixelsPerSlice = width * height;
 
  const neighborhoodRadius =
    options?.initialNeighborhoodRadius ?? DEFAULT_NEIGHBORHOOD_RADIUS;
  const positiveK =
    options?.positiveStdDevMultiplier ?? DEFAULT_POSITIVE_STD_DEV_MULTIPLIER;
  const negativeK =
    options?.negativeStdDevMultiplier ?? DEFAULT_NEGATIVE_STD_DEV_MULTIPLIER;
  const negativeSeedMargin =
    options?.negativeSeedMargin ?? DEFAULT_NEGATIVE_SEED_MARGIN;
  const negativeSeedsTargetPatches =
    options?.negativeSeedsTargetPatches ?? DEFAULT_NEGATIVE_SEEDS_COUNT;
 
  const ijkStart = transformWorldToIndex(refImageData, worldPosition).map(
    Math.round
  );
  const startIndex = referenceVolumeVoxelManager.toIndex(ijkStart);
 
  if (
    ijkStart[0] < 0 ||
    ijkStart[0] >= width ||
    ijkStart[1] < 0 ||
    ijkStart[1] >= height ||
    ijkStart[2] < 0 ||
    ijkStart[2] >= numSlices
  ) {
    console.warn('Click position is outside volume bounds.');
    return null;
  }
 
  const initialStats = csUtils.calculateNeighborhoodStats(
    scalarData as Types.PixelDataTypedArray,
    dimensions,
    ijkStart,
    neighborhoodRadius
  );
 
  if (initialStats.count === 0) {
    initialStats.mean = scalarData[startIndex];
    initialStats.stdDev = 0;
  }
 
  const positiveIntensityMin =
    initialStats.mean - positiveK * initialStats.stdDev;
  const positiveIntensityMax =
    initialStats.mean + positiveK * initialStats.stdDev;
 
  const neighborsCoordDelta = [
    [-1, 0, 0],
    [1, 0, 0],
    [0, -1, 0],
    [0, 1, 0],
    [0, 0, -1],
    [0, 0, 1],
  ];
 
  let minX = Infinity,
    minY = Infinity,
    minZ = Infinity;
  let maxX = -Infinity,
    maxY = -Infinity,
    maxZ = -Infinity;
 
  const positiveSeedIndices = new Set<number>();
  const queue: Array<[number, number, number]> = [];
 
  const startValue = scalarData[startIndex];
  if (
    startValue >= positiveIntensityMin &&
    startValue <= positiveIntensityMax
  ) {
    positiveSeedIndices.add(startIndex);
    queue.push(ijkStart);
    minX = maxX = ijkStart[0];
    minY = maxY = ijkStart[1];
    minZ = maxZ = ijkStart[2];
  } else {
    console.warn(
      'Clicked voxel intensity is outside the calculated positive range. No positive seeds generated.'
    );
    return { positiveSeedIndices: new Set(), negativeSeedIndices: new Set() };
  }
 
  // ---------------------------------
  // 1) BFS FOR POSITIVE SEEDS using local stats
  // ---------------------------------
  let currentQueueIndex = 0;
  while (
    currentQueueIndex < queue.length &&
    positiveSeedIndices.size < MAX_POSITIVE_SEEDS
  ) {
    const [x, y, z] = queue[currentQueueIndex++];
 
    minX = Math.min(x, minX);
    minY = Math.min(y, minY);
    minZ = Math.min(z, minZ);
    maxX = Math.max(x, maxX);
    maxY = Math.max(y, maxY);
    maxZ = Math.max(z, maxZ);
 
    for (let i = 0; i < neighborsCoordDelta.length; i++) {
      const [dx, dy, dz] = neighborsCoordDelta[i];
      const nx = x + dx;
      const ny = y + dy;
      const nz = z + dz;
 
      if (
        nx < 0 ||
        nx >= width ||
        ny < 0 ||
        ny >= height ||
        nz < 0 ||
        nz >= numSlices
      ) {
        continue;
      }
 
      const neighborIndex = nz * numPixelsPerSlice + ny * width + nx;
      if (positiveSeedIndices.has(neighborIndex)) {
        continue;
      }
 
      const neighborValue = scalarData[neighborIndex];
      if (
        neighborValue >= positiveIntensityMin &&
        neighborValue <= positiveIntensityMax
      ) {
        positiveSeedIndices.add(neighborIndex);
        if (positiveSeedIndices.size < MAX_POSITIVE_SEEDS) {
          queue.push([nx, ny, nz]);
        }
      }
    }
  }
 
  if (positiveSeedIndices.size >= MAX_POSITIVE_SEEDS) {
    console.debug(
      `Reached maximum number of positive seeds (${MAX_POSITIVE_SEEDS}). Stopping BFS.`
    );
  }
 
  if (positiveSeedIndices.size === 0) {
    console.warn('No positive seeds found after BFS.');
    return { positiveSeedIndices: new Set(), negativeSeedIndices: new Set() };
  }
 
  // ---------------------------------
  // 2) Calculate Statistics of the Found Positive Seeds
  // ---------------------------------
  let positiveSum = 0;
  let positiveSumSq = 0;
  positiveSeedIndices.forEach((index) => {
    const value = scalarData[index];
    positiveSum += value;
    positiveSumSq += value * value;
  });
 
  const positiveCount = positiveSeedIndices.size;
  const positiveMean = positiveSum / positiveCount;
  const positiveVariance =
    positiveSumSq / positiveCount - positiveMean * positiveMean;
  const positiveStdDev = Math.sqrt(Math.max(0, positiveVariance));
  const negativeDiffThreshold = negativeK * positiveStdDev;
 
  // ---------------------------------
  // 3) SAMPLE NEGATIVE SEED PATCHES (3x3x1)
  // ---------------------------------
  const minXm = Math.max(0, minX - negativeSeedMargin);
  const minYm = Math.max(0, minY - negativeSeedMargin);
  const minZm = Math.max(0, minZ - negativeSeedMargin);
  const maxXm = Math.min(width - 1, maxX + negativeSeedMargin);
  const maxYm = Math.min(height - 1, maxY + negativeSeedMargin);
  const maxZm = Math.min(numSlices - 1, maxZ + negativeSeedMargin);
 
  const negativeSeedIndices = new Set<number>();
  let attempts = 0;
  let patchesAdded = 0;
  const maxAttempts =
    negativeSeedsTargetPatches * MAX_NEGATIVE_SEED_ATTEMPTS_MULTIPLIER;
 
  while (patchesAdded < negativeSeedsTargetPatches && attempts < maxAttempts) {
    attempts++;
 
    // Sample a *center* point for the potential patch
    const rx = Math.floor(Math.random() * (maxXm - minXm + 1) + minXm);
    const ry = Math.floor(Math.random() * (maxYm - minYm + 1) + minYm);
    const rz = Math.floor(Math.random() * (maxZm - minZm + 1) + minZm);
 
    const centerIndex = rz * numPixelsPerSlice + ry * width + rx;
 
    if (
      positiveSeedIndices.has(centerIndex) ||
      negativeSeedIndices.has(centerIndex)
    ) {
      continue;
    }
 
    const centerValue = scalarData[centerIndex];
    if (Math.abs(centerValue - positiveMean) > negativeDiffThreshold) {
      let patchContributed = false;
      for (let dy = -1; dy <= 1; dy++) {
        const ny = ry + dy;
        if (ny < 0 || ny >= height) {
          continue;
        }
 
        for (let dx = -1; dx <= 1; dx++) {
          const nx = rx + dx;
          // Bounds check X
          if (nx < 0 || nx >= width) {
            continue;
          }
 
          // Z coordinate is fixed at rz (slice of the center point)
          const neighborIndex = rz * numPixelsPerSlice + ny * width + nx;
 
          if (
            positiveSeedIndices.has(neighborIndex) ||
            negativeSeedIndices.has(neighborIndex)
          ) {
            continue;
          }
 
          negativeSeedIndices.add(neighborIndex);
          patchContributed = true;
        }
      }
 
      if (patchContributed) {
        patchesAdded++;
      }
    }
  }
 
  if (negativeSeedIndices.size === 0) {
    console.warn(
      'Could not find any negative seeds. GrowCut might fail or produce poor results.'
    );
  }
 
  console.debug('positiveSeedIndices', positiveSeedIndices.size);
  console.debug('negativeSeedIndices', negativeSeedIndices.size);
 
  return { positiveSeedIndices, negativeSeedIndices };
}
 
/**
 * Runs one click grow cut segmentation algorithm
 * @param referencedVolumeId - The volume ID to segment
 * @param worldPosition - The clicked position in world coordinates
 * @param options - Configuration options for the grow cut algorithm and seed generation
 * @returns The segmented labelmap volume
 */
async function runOneClickGrowCut({
  referencedVolumeId,
  worldPosition,
  options,
}: {
  referencedVolumeId: string;
  worldPosition: Types.Point3;
  options?: GrowCutOneClickOptions;
}): Promise<Types.IImageVolume | null> {
  const referencedVolume = cache.getVolume(referencedVolumeId);
  const labelmap =
    volumeLoader.createAndCacheDerivedLabelmapVolume(referencedVolumeId);
 
  // reset the volume
  labelmap.voxelManager.forEach(({ index, value }) => {
    if (value !== 0) {
      labelmap.voxelManager.setAtIndex(index, 0);
    }
  });
 
  const seeds =
    options.seeds ??
    calculateGrowCutSeeds(referencedVolume, worldPosition, options);
 
  const positiveSeedLabel = options?.positiveSeedValue ?? POSITIVE_SEED_LABEL;
  const negativeSeedLabel = options?.negativeSeedValue ?? NEGATIVE_SEED_LABEL;
 
  if (!seeds) {
    return null;
  }
 
  const { positiveSeedIndices, negativeSeedIndices } = seeds;
 
  if (
    positiveSeedIndices.size < 10 ||
    positiveSeedIndices.size > MAX_POSITIVE_SEEDS ||
    negativeSeedIndices.size < 10
  ) {
    console.warn(
      'Not enough seeds found. GrowCut might fail or produce poor results.'
    );
    return labelmap;
  }
 
  // Apply the calculated seeds to the labelmap
  positiveSeedIndices.forEach((index) => {
    labelmap.voxelManager.setAtIndex(index, positiveSeedLabel);
  });
 
  negativeSeedIndices.forEach((index) => {
    labelmap.voxelManager.setAtIndex(index, negativeSeedLabel);
  });
 
  await run(referencedVolumeId, labelmap.volumeId, options);
 
  return labelmap;
}
 
export {
  runOneClickGrowCut as default,
  runOneClickGrowCut,
  calculateGrowCutSeeds,
};
export type { GrowCutOneClickOptions };