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 | 428x 428x 428x 428x 428x | import { quat, vec3 } from 'gl-matrix'; import { utilities as csUtils, cache, volumeLoader } from '@cornerstonejs/core'; import type { Types } from '@cornerstonejs/core'; import { run, type GrowCutOptions } from './runGrowCut'; import type { SphereBoundsInfo } from '../../getSphereBoundsInfo'; import { getSphereBoundsInfo } from '../../getSphereBoundsInfo'; const { transformWorldToIndex } = csUtils; const POSITIVE_SEED_VALUE = 254; const NEGATIVE_SEED_VALUE = 255; const POSITIVE_SEED_VARIANCE = 0.1; const NEGATIVE_SEED_VARIANCE = 0.8; type SphereInfo = { center: Types.Point3; radius: number; }; type GrowCutSphereBoundsInfo = SphereBoundsInfo & { topLeftIJK: Types.Point3; bottomRightIJK: Types.Point3; }; function _getGrowCutSphereBoundsInfo( referencedVolume: Types.IImageVolume, sphereBoundsInfo: SphereBoundsInfo ): GrowCutSphereBoundsInfo { const { topLeftWorld, bottomRightWorld } = sphereBoundsInfo; const topLeftIJK = transformWorldToIndex( referencedVolume.imageData, topLeftWorld ); const bottomRightIJK = transformWorldToIndex( referencedVolume.imageData, bottomRightWorld ); return { ...sphereBoundsInfo, topLeftIJK, bottomRightIJK, }; } function _getSphereBoundsInfo( referencedVolume: Types.IImageVolume, sphereInfo: SphereInfo ): SphereBoundsInfo { const direction = referencedVolume.imageData.getDirection(); const vecColumn = vec3.fromValues(direction[3], direction[4], direction[5]); const { center: sphereCenterPoint, radius: sphereRadius } = sphereInfo; const refVolImageData = referencedVolume.imageData; const topCirclePoint = vec3.scaleAndAdd( vec3.create(), sphereCenterPoint, vecColumn, -sphereRadius ) as Types.Point3; const bottomCirclePoint = vec3.scaleAndAdd( vec3.create(), sphereCenterPoint, vecColumn, sphereRadius ) as Types.Point3; // Gets the sphere bounds info in acquired orientation (no viewport needed) const sphereBoundsInfo = getSphereBoundsInfo( [bottomCirclePoint, topCirclePoint], refVolImageData ); return _getGrowCutSphereBoundsInfo(referencedVolume, sphereBoundsInfo); } function _createSubVolumeFromSphere( referencedVolume: Types.IImageVolume, sphereInfo: SphereInfo, viewport: Types.IViewport ) { const refVolImageData = referencedVolume.imageData; const camera = viewport.getCamera(); const { ijkVecRowDir, ijkVecColDir } = csUtils.getVolumeDirectionVectors( refVolImageData, camera ); // If two of the three vectors are aligned to X, Y or Z then the 3rd vector // is also aligned (non-oblique) const obliqueView = [ijkVecRowDir, ijkVecColDir].some( (vec) => !csUtils.isEqual(Math.abs(vec[0]), 1) && !csUtils.isEqual(Math.abs(vec[1]), 1) && !csUtils.isEqual(Math.abs(vec[2]), 1) ); if (obliqueView) { console.warn('Oblique view is not supported!'); return; } const { boundsIJK: sphereBoundsIJK /*, topLeftWorld, bottomRightWorld */ } = _getSphereBoundsInfo(referencedVolume, sphereInfo); const subVolumeBoundsIJK: Types.AABB3 = { minX: sphereBoundsIJK[0][0], maxX: sphereBoundsIJK[0][1] + 1, minY: sphereBoundsIJK[1][0], maxY: sphereBoundsIJK[1][1] + 1, minZ: sphereBoundsIJK[2][0], maxZ: sphereBoundsIJK[2][1] + 1, }; return csUtils.createSubVolume( referencedVolume.volumeId, subVolumeBoundsIJK, { targetBuffer: { type: 'Float32Array', }, } ); } function _setPositiveSeedValues( referencedVolume: Types.IImageVolume, labelmap: Types.IImageVolume, sphereInfo: SphereInfo, options?: GrowCutOptions ) { const refVolumePixelData = referencedVolume.voxelManager.getCompleteScalarDataArray(); const worldStartPos = sphereInfo.center; const [width, height, numSlices] = referencedVolume.dimensions; const numPixelsPerSlice = width * height; const ijkStartPosition = transformWorldToIndex( referencedVolume.imageData, worldStartPos ); const referencePixelValue = refVolumePixelData[ ijkStartPosition[2] * numPixelsPerSlice + ijkStartPosition[1] * width + ijkStartPosition[0] ]; const positiveSeedValue = options.positiveSeedValue ?? POSITIVE_SEED_VALUE; const positiveSeedVariance = options.positiveSeedVariance ?? POSITIVE_SEED_VARIANCE; const positiveSeedVarianceValue = Math.abs( referencePixelValue * positiveSeedVariance ); const minPositivePixelValue = referencePixelValue - positiveSeedVarianceValue; const maxPositivePixelValue = referencePixelValue + positiveSeedVarianceValue; // Neighbors distance that will be visited for every pixel const neighborsCoordDelta = [ [-1, 0, 0], [1, 0, 0], [0, -1, 0], [0, 1, 0], [0, 0, -1], [0, 0, 1], ]; const startVoxelIndex = ijkStartPosition[2] * numPixelsPerSlice + ijkStartPosition[1] * width + ijkStartPosition[0]; // Update the label map for the start voxel // labelmapData[startVoxelIndex] = positiveSeedValue; labelmap.voxelManager.setAtIndex(startVoxelIndex, positiveSeedValue); // Add the start point to the queue and traverse all neighbor pixels that are not visited yet and within the positive range const queue = [ijkStartPosition]; // Run breadth first search in 3D space to update the positive and negative seed values while (queue.length) { const ijkVoxel = queue.shift(); const [x, y, z] = ijkVoxel; for (let i = 0, len = neighborsCoordDelta.length; i < len; i++) { const neighborCoordDelta = neighborsCoordDelta[i]; const nx = x + neighborCoordDelta[0]; const ny = y + neighborCoordDelta[1]; const nz = z + neighborCoordDelta[2]; // Continue if it is out of bounds. if ( nx < 0 || nx >= width || ny < 0 || ny >= height || nz < 0 || nz >= numSlices ) { continue; } const neighborVoxelIndex = nz * numPixelsPerSlice + ny * width + nx; const neighborPixelValue = refVolumePixelData[neighborVoxelIndex]; // const neighborLabelmapValue = labelmapData[neighborVoxelIndex]; const neighborLabelmapValue = labelmap.voxelManager.getAtIndex(neighborVoxelIndex); if ( neighborLabelmapValue === positiveSeedValue || neighborPixelValue < minPositivePixelValue || neighborPixelValue > maxPositivePixelValue ) { continue; } // labelmapData[neighborVoxelIndex] = positiveSeedValue; labelmap.voxelManager.setAtIndex(neighborVoxelIndex, positiveSeedValue); queue.push([nx, ny, nz]); } } } function _setNegativeSeedValues( subVolume: Types.IImageVolume, labelmap: Types.IImageVolume, sphereInfo: SphereInfo, viewport: Types.IViewport, options: GrowCutOptions ) { const subVolPixelData = subVolume.voxelManager.getCompleteScalarDataArray(); const [columns, rows, numSlices] = labelmap.dimensions; const numPixelsPerSlice = columns * rows; // The camera has the same orientation for the labelmap volume because this // volume has the same orientation as the referenced volume and there is no // need to convert from refVolume to labelmap spaces. const { worldVecRowDir, worldVecSliceDir } = csUtils.getVolumeDirectionVectors(labelmap.imageData, viewport.getCamera()); const ijkSphereCenter = transformWorldToIndex( subVolume.imageData, sphereInfo.center ); const referencePixelValue = subVolPixelData[ ijkSphereCenter[2] * columns * rows + ijkSphereCenter[1] * columns + ijkSphereCenter[0] ]; const negativeSeedVariance = options.negativeSeedVariance ?? NEGATIVE_SEED_VARIANCE; const negativeSeedValue = options?.negativeSeedValue ?? NEGATIVE_SEED_VALUE; const negativeSeedVarianceValue = Math.abs( referencePixelValue * negativeSeedVariance ); const minNegativePixelValue = referencePixelValue - negativeSeedVarianceValue; const maxNegativePixelValue = referencePixelValue + negativeSeedVarianceValue; const numCirclePoints = 360; const rotationAngle = (2 * Math.PI) / numCirclePoints; const worldQuat = quat.setAxisAngle( quat.create(), worldVecSliceDir, rotationAngle ); const vecRotation = vec3.clone(worldVecRowDir); for (let i = 0; i < numCirclePoints; i++) { const worldCircleBorderPoint = vec3.scaleAndAdd( vec3.create(), sphereInfo.center, vecRotation, sphereInfo.radius ); const ijkCircleBorderPoint = transformWorldToIndex( labelmap.imageData, worldCircleBorderPoint as Types.Point3 ); const [x, y, z] = ijkCircleBorderPoint; vec3.transformQuat(vecRotation, vecRotation, worldQuat); if ( x < 0 || x >= columns || y < 0 || y >= rows || z < 0 || z >= numSlices ) { continue; } const offset = x + y * columns + z * numPixelsPerSlice; const pixelValue = subVolPixelData[offset]; if ( pixelValue < minNegativePixelValue || pixelValue > maxNegativePixelValue ) { // labelmapData[offset] = negativeSeedValue; labelmap.voxelManager.setAtIndex(offset, negativeSeedValue); } } } async function _createAndCacheSegmentationSubVolumeForSphere( subVolume: Types.IImageVolume, sphereInfo: SphereInfo, viewport: Types.IViewport, options?: GrowCutOptions ): Promise<Types.IImageVolume> { const labelmap = await volumeLoader.createAndCacheDerivedLabelmapVolume( subVolume.volumeId ); _setPositiveSeedValues(subVolume, labelmap, sphereInfo, options); _setNegativeSeedValues(subVolume, labelmap, sphereInfo, viewport, options); return labelmap; } /** * Run grow cut for a given volume and a sphere. A new sub-volume is created * based on sphere's AABB, a labelmap is created for that sub-volume, some pixels * are set as positive/negative and the labelmap is then updated in the gpu. The * positive and negative seed pixels are set based on the center and radius of * the sphere where pixels close o the center of the sphere are set as positive * and up to 360 pixels at the border are set as negative. * * @param referencedVolumeId - Referenced volume id * @param sphereInfo - Sphere information (center and radius) * @param viewport - Viewport * @param options - Options * @returns A new labelmap created that shall have the size of the sphere's AABB */ async function runGrowCutForSphere( referencedVolumeId: string, sphereInfo: SphereInfo, viewport: Types.IViewport, options?: GrowCutOptions ): Promise<Types.IImageVolume> { const referencedVolume = cache.getVolume(referencedVolumeId); const subVolume = _createSubVolumeFromSphere( referencedVolume, sphereInfo, viewport ); const labelmap = await _createAndCacheSegmentationSubVolumeForSphere( subVolume, sphereInfo, viewport, options ); await run(subVolume.volumeId, labelmap.volumeId); return labelmap; } export { runGrowCutForSphere as default, runGrowCutForSphere }; export type { SphereInfo, GrowCutOptions as GrowCutSphereOptions }; |