使用 ITK-Snap 截取 3D 图像的一个干净的截面

本文介绍了如何通过编辑菜单中的步骤来去除视图上的标注、调整视图以便最大化显示内容,并分享了快速截取屏幕的方法。适合学习者掌握基本的图像处理和查看技巧。

1. 去掉视图的标注信息

edit -> Slice Annotations -> Toggle All Annotations

-1

2. zoom to fit and expend the view

0

3. snapshot

在这里插入图片描述

import os import numpy as np import SimpleITK as sitk # ====================== 功能函数 ====================== def dicom_to_nifti(dicom_dir, output_nifti_path): """将DICOM文件夹转换为NIfTI文件""" reader = sitk.ImageSeriesReader() dicom_files = reader.GetGDCMSeriesFileNames(dicom_dir) if not dicom_files: raise ValueError(f"未找到DICOM文件,请检查路径: {dicom_dir}") reader.SetFileNames(dicom_files) image = reader.Execute() sitk.WriteImage(image, output_nifti_path) print(f"DICOM转换完成 → {output_nifti_path}") def crop_or_pad_to_target(data, target_shape=(168, 512, 512)): """将3D数据裁剪或填充至目标尺寸(居中)""" result = np.zeros(target_shape, dtype=data.dtype) # 计算填充偏移量 offsets = [max(0, (t - s) // 2) for t, s in zip(target_shape, data.shape)] # 数据放置范围 target_slices = [ slice(offset, min(offset + size, target)) for offset, size, target in zip(offsets, data.shape, target_shape) ] # 数据截取范围(防止数据比目标大) data_slices = [ slice(max(0, -offset), min(size, target - offset)) for offset, size, target in zip(offsets, data.shape, target_shape) ] result[tuple(target_slices)] = data[tuple(data_slices)] return result def process_with_label(image_path, label_path, output_path, target_shape): # 加载数据并统一空间信息 image = sitk.ReadImage(image_path) label = sitk.ReadImage(label_path) # 强制同步空间属性(关键修复) label.SetDirection(image.GetDirection()) label.SetOrigin(image.GetOrigin()) label.SetSpacing(image.GetSpacing()) # 检查数据 label_data = sitk.GetArrayFromImage(label).astype(np.uint8) print(f"重采样前label=1的像素数:", np.sum(label_data == 1)) # 重采样标签 if image.GetSize() != label.GetSize(): print("正在对齐标签尺寸...") label = sitk.Resample( label, image, # 参考图像 sitk.Transform(), sitk.sitkNearestNeighbor, # 必须使用最近邻 0 ) label_data = sitk.GetArrayFromImage(label).astype(np.uint8) print(f"重采样后label=1的像素数:", np.sum(label_data == 1)) if np.sum(label_data == 1) == 0: raise ValueError("重采样后标签丢失!请检查图像和标签的空间属性是否匹") # 计算ROI non_zero = np.argwhere(label_data == 1) z_min, y_min, x_min = non_zero.min(axis=0) z_max, y_max, x_max = non_zero.max(axis=0) + 1 # 裁剪并调整尺寸 image_data = sitk.GetArrayFromImage(image) cropped = image_data[z_min:z_max, y_min:y_max, x_min:x_max] processed_data = crop_or_pad_to_target(cropped, target_shape) # 保存结果 output_image = sitk.GetImageFromArray(processed_data) output_image.CopyInformation(image) sitk.WriteImage(output_image, output_path) print(f"处理成功!输出尺寸: {processed_data.shape}") # ====================== 主流程 ====================== if __name__ == "__main__": # ---------- 第一步:DICOM转NIfTI ---------- dicom_dir = r"E:\Breast_NAC\解压后文件\GYMR10001481 2\D4"# 替换为你的DICOM文件夹 raw_nifti_path = "converted_image.nii.gz" if not os.path.exists(raw_nifti_path): # 避免重复转换 dicom_to_nifti(dicom_dir, raw_nifti_path) # ---------- 第二步:ITK-SNAP标注后的标签 ---------- label_path ="C:/Users/86191/AppData/Roaming/JetBrains/PyCharmCE2023.3/scratches/processed_breast_cancer.nii.gz" # 替换为ITK-SNAP保存的标签文件 # ---------- 第三步:处理并保存结果 ---------- output_path = r"C:\Users\86191\chulihoudwenjian\processed_roi.nii.gz" target_size = (168, 512, 512) # 根据需求调整 process_with_label( image_path=raw_nifti_path, label_path=label_path, output_path=output_path, target_shape=target_size )
07-26
### 使用 ITK-SNAP 进行 3D 图像裁剪 #### 安装与启动 ITK-SNAP 为了使用 ITK-SNAP3D 图像进行裁剪,首先需要安装该软件。可以从官方网站下载并按照说明完成安装过程[^1]。 #### 打开目标文件 启动 ITK-SNAP 后,在主界面点击 "File" -> "Open Image..." 来加载要编辑的三维医学图像数据集。支持多种常见格式如 NIfTI (.nii),DICOM 文件夹等。 #### 设置显示模式 切换至合适的视图以便于操作。通常会选择 MPR (Multiplanar Reconstruction) 模式来查看三个正交切面(轴向、冠状和矢状),这有助于更精确地定义裁剪边界。 #### 绘制ROI(Region of Interest) 通过左侧工具栏中的画笔或其他形状绘制工具,在不同视角上标记出想要保留下来的感兴趣区。可以调整画笔大小以适应具体需求;对于复杂结构还可以利用多边形或自由手绘方式勾勒轮廓。 #### 创建Mask Layer 当完成了 ROI 的描绘之后,应该创建一个新的 Mask 层并将刚才所做的标注转换成 binary mask。这样做的好处是可以方便后续步骤中应用此掩膜来进行实际的数据切割工作。 #### 应用掩模执行裁剪 回到原始图像层面上,选择菜单项 "Filters" 下拉列表里的 “Apply Binary Mask”,此时会弹出对话框询问是否确认将当前激活的mask应用于整个volume。一旦选择了肯定选项,则只有被选中的部分会被保存下来而其余区域则变为透明状态。 #### 导出处理后的结果 最后一步就是导出经过裁减的新图像了。“File”->“Export Volume As...”,可以选择不同的输出路径以及指定所需的文件类型。 ```python import SimpleITK as sitk def save_cropped_image(image, output_path): """ Save the cropped image using SimpleITK. Parameters: image : SimpleITK.Image The input image to be saved after cropping. output_path : str Path where the cropped image will be stored. """ writer = sitk.ImageFileWriter() writer.SetFileName(output_path) writer.Execute(image) # Example usage with a hypothetical variable 'cropped_img' save_cropped_image(cropped_img, "./output/cropped.nii.gz") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Skr.B

WUHOOO~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值