【问题标题】:Decompress nifti medical image in gz format using python使用python解压缩gz格式的nifti医学图像
【发布时间】:2020-06-05 14:17:42
【问题描述】:

我想在 python 中解压缩一批 nii.gz 文件,以便稍后在sitk 中处理它们。当我通过右键单击文件并选择“Extract..”手动解压缩单个文件时,该文件随后会被 sitk 正确解释(我使用的是 sitk.ReadImage(unzipped))。但是当我尝试使用以下代码在python中解压缩它时:

with gzip.open(segmentation_zipped, "rb") as f:
    bindata = f.read()
segmentation_unzipped = os.path.join(segmentation_zipped.replace(".gz", ""))
with gzip.open(segmentation_unzipped, "wb") as f:
    f.write(bindata)

sitk 尝试读取文件时出现错误: RuntimeError:SimpleITK ReadImage 中抛出异常:C:\d\VS14-Win64-pkg\SimpleITK\Code\IO\src\sitkImageReaderBase.cxx:82: sitk::ERROR: 无法确定“E:\BraTS19_2013_10_1_seg.nii”的 ImageIO 阅读器

当尝试做一些不同的事情时:

input = gzip.GzipFile(segmentation_zipped, 'rb')
s = input.read()
input.close()

segmentation_unzipped = os.path.join(segmentation_zipped.replace(".gz", ""))
output = open(segmentation_unzipped, 'wb')
output.write(s)
output.close()

我得到: RuntimeError:SimpleITK ReadImage 中抛出异常:C:\d\VS14-Win64-pkg\SimpleITK-build\ITK\Modules\IO\PNG\src\itkPNGImageIO.cxx:101: itk::ERROR: PNGImageIO(0000022E3AF2C0C0): PNGImageIO 未能读取文件头: 原因:fread 只读 0 而不是 8

谁能帮忙?

【问题讨论】:

  • SimpleITK 出现错误很奇怪,因为您没有使用 SimpleITK 读取图像(至少在您显示的代码中)。请注意,SimpleITK.ReadImage 无需解压缩即可读取“.nii.gz”文件。

标签: python-3.x gzip simpleitk nifti


【解决方案1】:

Nifti 镜像无需解压,Nibabel 等库无需解压即可处理。

#==================================
import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt
#==================================
# load image (4D) [X,Y,Z_slice,time]
nii_img  = nib.load('path_to_file.nii.gz')
nii_data = nii_img.get_fdata()

fig, ax = plt.subplots(number_of_frames, number_of_slices,constrained_layout=True)
fig.canvas.set_window_title('4D Nifti Image')
fig.suptitle('4D_Nifti 10 slices 30 time Frames', fontsize=16)
#-------------------------------------------------------------------------------
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

for slice in range(number_of_slices):
    # if your data in 4D, otherwise remove this loop
    for frame in range(number_of_frames):
        ax[frame, slice].imshow(nii_data[:,:,slice,frame],cmap='gray', interpolation=None)
        ax[frame, slice].set_title("layer {} / frame {}".format(slice, frame))
        ax[frame, slice].axis('off')

plt.show() 

或者您可以使用 SimpleITK,如下所示:

import SimpleITK as sitk
import numpy as np

# A path to a T1-weighted brain .nii image:
t1_fn = 'path_to_file.nii'

# Read the .nii image containing the volume with SimpleITK:
sitk_t1 = sitk.ReadImage(t1_fn)

# and access the numpy array:
t1 = sitk.GetArrayFromImage(sitk_t1)

【讨论】:

  • 感谢您的回答!这正是我想要做的:我真的很想使用简单的 itk 来读取图像,但我有很多 nii.gz 文件。这就是为什么我希望能够在 python 中解压它们以便能够使用简单的 itk。我尝试使用简单的 itk 读取 nii.gz 格式的图像,但效果是: RuntimeError: Exception throw in SimpleITK ReadImage: C:\d\VS14-Win64-pkg\SimpleITK\Code\IO\src\sitkImageReaderBase.cxx:82 : sitk::ERROR: 无法确定“\BraTS19_2013_10_1_seg.nii.gz”的 ImageIO 阅读器
  • @Biba,但是SimpleITK可以读取压缩格式! sitk_3D = sitk.ReadImage( os.path.join(DIR_DATA, "3D_Brain_Source.nii.gz"))
  • 当我尝试使用 itk 读取压缩的 nifti 时,我得到: RuntimeError: Exception throw in SimpleITK ReadImage: C:\d\VS14-Win64-pkg\SimpleITK\Code\IO\src\sitkImageReaderBase.cxx :82: sitk::ERROR: 无法确定“BraTS19_2013_10_1_seg.nii.gz”的 ImageIO 阅读器。当我手动解压缩文件(右键单击 -> 提取文件)然后通过 itk 读取文件时,文件被正确读取
  • 对不起!您的解决方案确实有效,结果证明路径存在问题:我更改为简单的 D:\BraTS19_2013_10_1_seg.nii.gz 并且一切正常!感谢您的帮助!
猜你喜欢
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 2017-10-09
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多