You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cornerstone.js中鼠标选择分割并改色及DICOM视图分割选中方法咨询

实现Cornerstone.js分割区域的鼠标点击选中功能

要在DICOM查看器上点击选中已加载的RLE分割区域,核心是通过自定义交互工具结合分割数据的命中检测来实现,以下是具体步骤:

1. 确认分割数据的关联状态

从RLE加载分割数据后,需确保分割层已添加到cornerstone视图元素中,且每个分割区域都带有唯一的segmentId标识,这是后续选中逻辑的基础。

2. 自定义鼠标点击交互工具

cornerstoneTools没有内置的分割选中工具,需要自定义工具监听点击事件并检测点击位置是否落在分割区域内:

// 自定义分割选中工具
const segmentationSelectTool = cornerstoneTools.import('base/BaseTool').extend({
  constructor: function() {
    super();
    this.name = 'segmentationSelect';
    this.enabled = true;
    this.active = true;
  },

  mouseClickCallback: function(evt) {
    const { element, currentPoints } = evt.detail;
    const imageCoords = currentPoints.image;

    // 获取当前视图的活跃分割数据
    const segmentationModule = cornerstoneTools.getModule('segmentation');
    const activeSegmentation = segmentationModule.segmentation.getActiveSegmentation(element);
    if (!activeSegmentation) return;

    let selectedSegmentId = null;
    const { segments, imageSize } = activeSegmentation;
    const [width, height] = imageSize;

    // 遍历所有分割区域,检测点击命中
    for (const segmentId in segments) {
      const segment = segments[segmentId];
      // 将RLE转换为掩码数组
      const mask = segmentationModule.utils.rleToMask(segment.rle, width, height);
      // 计算点击坐标对应的掩码索引
      const maskIndex = Math.floor(imageCoords.y) * width + Math.floor(imageCoords.x);
      // 判断是否命中当前分割区域
      if (mask[maskIndex]) {
        selectedSegmentId = segmentId;
        break;
      }
    }

    if (selectedSegmentId) {
      // 标记选中状态:修改分割区域颜色
      const targetSegment = activeSegmentation.segments[selectedSegmentId];
      targetSegment.color = [255, 0, 0]; // 用红色高亮选中区域
      // 更新视图渲染
      cornerstone.updateImage(element);
      // 触发自定义事件,向外传递选中信息
      element.dispatchEvent(new CustomEvent('segmentSelected', {
        detail: { segmentId: selectedSegmentId }
      }));
    }
  }
});

// 注册并激活工具
cornerstoneTools.addTool(segmentationSelectTool);
cornerstoneTools.setToolActive('segmentationSelect', { mouseButtonMask: 1 });

3. 性能优化(可选)

如果分割区域数量多、尺寸大,每次点击都转换RLE为掩码会影响性能,可以提前将所有分割区域的RLE转换为掩码并缓存,或者引入空间索引(比如四叉树)来减少命中检测的遍历次数。

4. 选中状态的可视化扩展

除了修改颜色,还可以通过自定义渲染逻辑给选中区域添加边框、半透明覆盖层等效果,进一步强化选中的视觉反馈。

内容的提问来源于stack exchange,提问作者Wimal Weerawansa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 22:20:01