【问题标题】:How to modify orientation of mgh/dicom/nifti file using nibabel如何使用 nibabel 修改 mgh/dicom/nifti 文件的方向
【发布时间】:2019-05-13 11:28:00
【问题描述】:

我很难为 3 个不同的视图(即冠状、轴向和矢状)找出适当的仿射变换,每个视图都有如下不同的问题:

1:轴向颜色图与矢状原始视图重叠。

2:同样,矢状彩色图与轴向原始图像重叠。

3:每个人都有一些方向问题,比如当日冕的颜色图和原始图像正确但方向错误时,这里最容易看到。

我正在保存要发送到服务器以进行某种预测的原始文件,该文件会生成颜色图并返回该文件以进行可视化,稍后我会将所有内容一起显示。

预测后在服务器中,这里是保存文件的代码。

 nifti_img = nib.MGHImage(idx, affine, header=header)

affineheader 是从我发送的文件中提取的原始仿射和标头。

我需要处理 "idx" 值,该值以 Numpy 数组格式保存原始数据,但不确定具体要做什么。在这里需要帮助。

正在努力使用 nibabel python 库解决问题,但由于我对这些文件的工作原理和仿射变换的了解非常有限,我很难弄清楚应该做什么我这样做是为了使它们正确。

我在前端使用带有 threejs 支持的 AMI js,在后端使用带有 python 的 nibabel。前端或后端任何地方的解决方案都是可以接受的。

请帮忙。提前致谢。

【问题讨论】:

    标签: python-3.x three.js numpy-ndarray nibabel ami.js


    【解决方案1】:
    img = nib.load(img_path)
    
    # check the orientation you wanna reorient.
    # For example, the original orientation of img is RPI,
    # you wanna reorient it to RAS, the second the third axes should be flipped
    # ornt[P, 1] is flip of axis N, where 1 means no flip and -1 means flip.
    ornt = np.array([[0, 1],
                    [1, -1],
                    [2, -1]])
    
    img_orient = img.as_reoriented(ornt)
    nib.save(img_orient, img_path)
    

    【讨论】:

      【解决方案2】:

      这很简单,对来自 nibabel 的 rawdata 使用 numpy.moveaxis() 和 numpy.flip() 操作。如下。

          # Getting raw data back to process for better orienation and label mapping.
          orig_img_data = nib.MGHImage(numpy_arr, affine)
          nifti_img = nib.MGHImage(segmented_arr_output, affine)  
      
          # Getting original and predicted data to preprocess to original shape and view for visualisation.
          orig_img = orig_img_data.get_fdata()
          seg_img = nifti_img.get_fdata()
      
          # Placing proper views in proper place and flipping it for a better visualisation as required.
          # moveaxis to get original order.
          orig_img_ = np.moveaxis(orig_img, -1, 0)
          seg_img = np.moveaxis(seg_img, -1, 0)
      
          # Flip axis to overcome mirror image/ flipped view.        
          orig_img_ = np.flip(orig_img_, 2)
          seg_img = np.flip(seg_img, 2)
      
          orig_img_data_ = nib.MGHImage(orig_img_.astype(np.uint8), np.eye(4), header)
          nifti_img_ = nib.MGHImage(seg_img.astype(np.uint8), np.eye(4), header)
      

      注意: 使用相同的仿射矩阵来包装这两个数组非常重要。 4*4 单位矩阵比使用原始仿射矩阵更好,因为这给我带来了问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多