简体   繁体   English

npy 到 nii.gz 转换的问题

[英]Problems with npy to nii.gz conversion

I'm trying to convert a .npy image to a nii.gz image, I'm having various problems even though I'm following instructions correctly.我正在尝试将.npy图像转换为nii.gz图像,即使我正确遵循了说明,我也遇到了各种问题。

This is the code I'm using:这是我正在使用的代码:

import numpy as np
import nibabel as nib

file_dir = "D:/teste volumes slicer/"
fileNPY1 = "teste01.npy"
img_array1 = np.load(file_dir + fileNPY1)
print(img_array1.shape)
print(img_array1.dtype)

normal_array = "D:/teste volumes slicer/teste01.npy"
print ("done")
nifti_file = nib.Nifti1Image(normal_array, np.eye(4))

About the image: (332, 360, 360) float64 (this is what we got when we print the shape and dtype) https://ibb.co/mRyTrw7 - image that shows the error and the image's information关于图像:(332、360、360)float64(这是我们打印形状和 dtype 时得到的) https://ibb.co/mRyTrw7 - 显示错误和图像信息的图像

The error message: 
Traceback (most recent call last):
  File "d:\teste volumes slicer\conversor.py", line 16, in <module>
    nifti_file = nib.Nifti1Image(normal_array, np.eye(4))
  File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 1756, in __init__
    super(Nifti1Pair, self).__init__(dataobj,
  File "C:\Python39\lib\site-packages\nibabel\analyze.py", line 918, in __init__
    super(AnalyzeImage, self).__init__(
  File "C:\Python39\lib\site-packages\nibabel\spatialimages.py", line 469, in __init__
    self.update_header()
  File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 2032, in update_header
    super(Nifti1Image, self).update_header()
  File "C:\Python39\lib\site-packages\nibabel\nifti1.py", line 1795, in update_header
    super(Nifti1Pair, self).update_header()
  File "C:\Python39\lib\site-packages\nibabel\spatialimages.py", line 491, in update_header
    shape = self._dataobj.shape
AttributeError: 'str' object has no attribute 'shape'

The problem with your code is instead of passing your Numpy-array (your image), you are passing the path of the image to the Nifti1Image function.您的代码的问题不是传递您的 Numpy 数组(您的图像),而是将图像的路径传递给Nifti1Image function。

This is the correct way to convert it:这是转换它的正确方法:

import numpy as np
import nibabel as nib

file_dir = "D:/teste volumes slicer/"
fileNPY1 = "teste01.npy"
img_array1 = np.load(file_dir + fileNPY1) 
nifti_file = nib.Nifti1Image(img_array1 , np.eye(4))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM