simpleitk和numpy的转化问题 SimpleITK 读取 NIfTI 后数组默认是zhw顺序(z, y, x)若需(x, y, z)顺序需手动转置np.transpose(array, (2, 1, 0))。元数据如GetSize()仍遵循 NIfTI 的(x, y, z)约定需注意区分数组顺序和元数据顺序。1 simpleitk和numpy的维度问题NumPy 数组始终使用(D, H, W)顺序3D或(H, W)顺序2D。SimpleITK 图像GetImageFromArray和GetArrayFromImage会自动处理 DHW ↔ WHD 的转换无需手动转置。# 正确用法示例 arr_dhw np.random.rand(64, 256, 256) # (D, H, W) itk_img sitk.GetImageFromArray(arr_dhw) # 自动转为 ITK (W, H, D) arr_back sitk.GetArrayFromImage(itk_img) # 自动转回 (D, H, W)GetImageFromArray会丢失原始图像的物理坐标信息如像素间距、原点。解决手动设置元数据itk_img sitk.GetImageFromArray(arr_dhw) itk_img.SetSpacing([1.0, 1.0, 1.0]) # 设置体素间距 (x, y, z) itk_img.SetOrigin([0, 0, 0]) # 设置原点验证交互正确性的方法# 检查 NumPy 和 ITK 的维度一致性 arr_dhw np.random.rand(64, 256, 256) itk_img sitk.GetImageFromArray(arr_dhw) # 验证转换无失真 assert arr_dhw.shape (64, 256, 256) # NumPy (D, H, W) assert itk_img.GetSize() (256, 256, 64) # ITK (W, H, D) assert np.allclose(arr_dhw, sitk.GetArrayFromImage(itk_img))坚持使用(D, H, W)顺序的 NumPy 数组与 SimpleITK 交互可避免维度错误。2 SetDirection()和GetDirection()的矛盾性使用过程发现读取nii后GetDirection得到实施一个一维16个元素的元组直接SetDirection失败。原因如下direction src.GetDirection() # 如果是 3D 图像9 个元素 if len(direction) 9: direction_matrix tuple(direction) # 直接传入 9 个元素的元组 # 或者构造 3x3 矩阵可选 # direction_matrix ( # direction[0], direction[1], direction[2], # direction[3], direction[4], direction[5], # direction[6], direction[7], direction[8] # ) # 如果是 2D 图像4 个元素 elif len(direction) 4: direction_matrix tuple(direction) # 直接传入 4 个元素的元组 # 或者构造 2x2 矩阵可选 # direction_matrix ( # direction[0], direction[1], # direction[2], direction[3] # ) # 如果是 16 个元素4x4 矩阵需要提取前 9 个3x3 elif len(direction) 16: direction_matrix ( direction[0], direction[1], direction[2], direction[4], direction[5], direction[6], direction[8], direction[9], direction[10] ) else: raise ValueError(Unsupported direction format!) # 设置方向 target_img sitk.GetImageFromArray(arr) # 你的目标图像 target_img.SetDirection(direction_matrix) # 正确设置方向import SimpleITK as sitk import numpy as np # 读取源图像 src sitk.ReadImage(source.nii) direction src.GetDirection() # 检查方向矩阵的形状 if len(direction) 9: # 3D 图像 direction_matrix direction elif len(direction) 4: # 2D 图像 direction_matrix direction elif len(direction) 16: # 4x4 矩阵提取 3x3 direction_matrix ( direction[0], direction[1], direction[2], direction[4], direction[5], direction[6], direction[8], direction[9], direction[10] ) else: raise ValueError(Unsupported direction format!) # 创建目标图像并设置方向 arr sitk.GetArrayFromImage(src) # 获取 NumPy 数组 target_img sitk.GetImageFromArray(arr) target_img.SetDirection(direction_matrix) # 正确设置方向 # 保存 sitk.WriteImage(target_img, output.nii)如果是9个元素直接target_img.SetDirection(src.GetDirection())如果是 16 个元素4x4 矩阵提取前 9 个构造 3x3 方向矩阵

相关新闻

最新新闻

深入ReactN源码:GlobalStateManager如何管理状态队列、Dispatcher与组件订阅

深入ReactN源码:GlobalStateManager如何管理状态队列、Dispatcher与组件订阅

深入ReactN源码:GlobalStateManager如何管理状态队列、Dispatcher与组件订阅 【免费下载链接】reactn React, but with built-in global state management. 项目地址: https://gitcode.com/gh_mirrors/re/reactn ReactN 是一个把全局状态管理内置于 React 的…

2026/8/26 15:01:24
PDFPatcher PDF补丁丁:三步快速解除PDF复制与打印限制的完整指南

PDFPatcher PDF补丁丁:三步快速解除PDF复制与打印限制的完整指南

PDFPatcher PDF补丁丁:三步快速解除PDF复制与打印限制的完整指南 【免费下载链接】PDFPatcher PDF补丁丁——PDF工具箱,可以编辑书签、剪裁旋转页面、解除限制、提取或合并文档,探查文档结构,提取图片、转成图片等等 项目地址: …

2026/8/26 15:01:24
Maka Agent诊断日志系统怎么运作?AI助手故障排查完全指南

Maka Agent诊断日志系统怎么运作?AI助手故障排查完全指南

Maka Agent诊断日志系统怎么运作?AI助手故障排查完全指南 【免费下载链接】maka Apache Maka (Incubating) is a local-first AI agent workspace. Model messages, tool calls, tool results, permission decisions, and termination events are recorded as an ap…

2026/8/26 15:01:24
人工智能MOE钻石架构(Diamond Architecture)技术白皮书

人工智能MOE钻石架构(Diamond Architecture)技术白皮书

TOC钻石架构(Diamond Architecture)技术白皮书 钻石架构(Diamond Architecture)技术白皮书 基于硬核参数替换的稀疏微调部署框架 一、摘要 大语言模型微调与部署面临根本性矛盾:全参数微调性能最优但存储成本高&am…

2026/8/26 15:01:24
第六期:shell脚本编程

第六期:shell脚本编程

大家好,接下来的一段时间我将开始学习野火的Linux系统课程并将学习到的干货逐步更新到我的CSDN博客中。没时间刷课的同学可以把我的博客喂给AI 突击一下。 目录 一、shell脚本简介 1. Shell 脚本是什么 2. Shell 脚本有什么用 3. Shell 命令的本质 4. Shell 脚…

2026/8/26 15:01:24
MozStumbler构建与发布快速指南:一条命令搞定三渠道(GitHub/PlayStore/F-Droid)Gradle构建

MozStumbler构建与发布快速指南:一条命令搞定三渠道(GitHub/PlayStore/F-Droid)Gradle构建

MozStumbler构建与发布快速指南:一条命令搞定三渠道(GitHub/PlayStore/F-Droid)Gradle构建 【免费下载链接】MozStumbler Android Stumbler for Mozilla 项目地址: https://gitcode.com/gh_mirrors/mo/MozStumbler MozStumbler&#x…

2026/8/26 14:56:24