python 批量修改nii.gz格式图像分辨率
时间: 2023-05-17 10:07:59 浏览: 281
可以使用 SimpleITK 库来批量修改 nii.gz 格式图像的分辨率。以下是一个示例代码:
```python
import SimpleITK as sitk
import os
input_dir = "/path/to/input/dir"
output_dir = "/path/to/output/dir"
new_spacing = (1.0, 1.0, 1.0) # 新的分辨率
for filename in os.listdir(input_dir):
if filename.endswith(".nii.gz"):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename)
image = sitk.ReadImage(input_path)
old_spacing = image.GetSpacing()
new_size = [int(round(old_size * old_spacing / new_spacing)) for old_size in image.GetSize()]
resample = sitk.ResampleImageFilter()
resample.SetOutputSpacing(new_spacing)
resample.SetSize(new_size)
resample.SetOutputDirection(image.GetDirection())
resample.SetOutputOrigin(image.GetOrigin())
resample.SetTransform(sitk.Transform())
resample.SetInterpolator(sitk.sitkLinear)
resampled_image = resample.Execute(image)
sitk.WriteImage(resampled_image, output_path)
```
这个代码会将 `input_dir` 目录下所有的 `.nii.gz` 文件的分辨率修改为 `(1.0, 1.0, 1.0)`,并保存到 `output_dir` 目录下。
阅读全文