NIFTI格式(.Nii)数据version 1格式分析

NIFTI格式(.Nii)数据格式分析

NIFTI出现原因

  1. NIFTI出现的原因是原来一种图像格式是ANALYZE 7.5 format,但是这个图像格式缺少一些信息,比如没有方向信息,病人的左右方位等,如果需要包括额外的信息,就需要一个额外的文件,比如ANALYZE7.5就需要一对<.hdr, .img>文件来保存图像的完整信息。因此,解决这个问题Data Format Working Group (DFWG) 将图像格式完整的定义为NIFTI(Neuroimaging Informatics Technology Initiative)格式。
  2. 放射学和神经学医生使用软件和使用习惯会有一些不同,所以为了统一格式,DFWG提出NIFTI格式图像。
  3. NIFTI格式的读取软件,也应该可以读取<.hdr, .img>文件对。

NIFTI-1文件头信息格式

  • :NIFTI格式中保存的信息是,一共可以保存7维数据。第1,2,3维度(0维统计使用维度个数)保留给空间维度x,y,z,而第四维度留给时间维度t。第5,6,7维度可供用户其他使用,但是第五维度也有一些预定义的用途,例如存储特定体素分布的参数或存储矢量数据。
// NIFTI version1 头文件存储信息(顺序存放)
/*
NIFTI 第一版的数据格式的头文件大小必须为348字节,这是为了兼容新,和旧的analyze格式保持一致。就是sizeof_hdr保存的数据大小。
*/
//                      //OFFSET    SIZE    Description
int     sizeof_hdr;     //0B        4B      Size of the header.Must be 348 (bytes).
char    data_type[10];  //4B        10B     Not used; compatibility with analyze.
char    db_name[18];    //14B       18B     Not used; compatibility with analyze.
int     extents;        //32B       4B      Not used; compatibility with analyze.
short   session_error;  //36B       2B      Not used; compatibility with analyze.
char    regular;        //38B       1B      Not used; compatibility with analyze.
char    dim_info;       //39B       1B      Encoding directions(phase, frequency, slice).
short   dim[8];         //40B       16B     Data array dimensions.
float   intent_p1;      //56B       4B      1st intent parameter.
float   intent_p2;      //60B       4B      2nd intent parameter.
float   intent_p3;      //64B       4B      3rd intent parameter.
short   intent_code;    //68B       2B      nifti intent.
short   datatype;       //70B       2B      Data type.
short   bitpix;         //72B       2B      Number of bits per voxel.
short   slice_start;    //74B       2B      First slice index.
float   pixdim[8];      //76B       32B     Grid spacings(unit per dimension).
float   vox_offset;     /*108B      4B      Offset into a.nii file. !Data true start point.*/
float   scl_slope;      //112B      4B      Data scaling, slope.
float   scl_inter;      //116B      4B      Data scaling, offset.
short   slice_end;      //120B      2B      Last slice index.
char    slice_code;     //122B      1B      Slice timing order.
char    xyzt_units;     //123B      1B      Units of pixdim[1..4].
float   cal_max;        //124B      4B      Maximum display intensity.
float   cal_min;        //128B      4B      Minimum display intensity.
float   slice_duration; //132B      4B      Time for one slice.
float   toffset;        //136B      4B      Time axis shift.
int     glmax;          //140B      4B      Not used; compatibility with analyze.
int     glmin;          //144B      4B      Not used; compatibility with analyze.
char    descrip[80];    //148B      80B     Any text.
char    aux_file[24];   //228B      24B     Auxiliary filename.
short   qform_code;     //252B      2B      Use the quaternion fields.
short   sform_code;     //254B      2B      Use of the affine fields.
float   quatern_b;      
### 将 NIfTI 文件从 `.nii` 转换为 `.nii.gz` 的方法 可以使用 `gzip` 命令或者 Python 中的 `nibabel` 库实现将 `.nii` 文件转换为 `.nii.gz` 格式的操作。 #### 使用命令行工具 gzip 如果是在 Linux 或 macOS 系统上,可以直接通过终端运行以下命令完成压缩: ```bash gzip file.nii ``` 这会生成一个名为 `file.nii.gz` 的文件[^1]。需要注意的是,原始的 `.nii` 文件会被删除,仅保留压缩后的 `.nii.gz` 文件。如果不希望覆盖原文件,则可以通过复制的方式创建副本再执行上述命令。 #### 利用 nibabel 实现程序化转换 对于更灵活的需求(比如批量处理),推荐采用编程方式完成此任务。以下是基于 Python 和 `nibabel` 库的一个简单脚本: ```python import os import nibabel as nib from pathlib import Path def convert_nii_to_niigz(input_path, output_path=None): """ Converts a single .nii file to .nii.gz format. Parameters: input_path (str): The path of the original .nii file. output_path (str, optional): Destination path for saving compressed version. Defaults to None which appends '.gz' suffix automatically. Returns: str: Output filepath after conversion process completes successfully. Raises: FileNotFoundError: If provided source does not exist or is invalid type. """ if not os.path.exists(input_path): raise FileNotFoundError(f"The specified {input_path} doesn't seem valid.") img = nib.load(input_path) # Determine default save location when unspecified by user if not output_path: pth_obj = Path(input_path) new_stem = f"{pth_obj.stem}.gz" output_path = pth_obj.with_name(new_stem).as_posix() nib.save(img, output_path) return output_path if __name__ == "__main__": src_file = "/path/to/your/image/file.nii" # Replace with actual paths accordingly dest_file = "/desired/output/directory/file.nii.gz" result = convert_nii_to_niigz(src_file, dest_file) print(result) ``` 该函数接受两个参数:一个是输入路径 (`input_path`);另一个是可选的目标存储位置(`output_path`)。如果没有指定目标地址,默认会在原有基础上追加`.gz`扩展名保存新版本[^2]。 以上两种方案均能有效解决您的需求,请依据具体场景选择合适的方法实施!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值