How to convert multiple Nifti1Image of different sizes to a nii.gz #1223
-
I have an equally-sized DICOM series whose images I crop using Otsu thresholding and contouring. This cropping leads to smaller images with various sizes (width, height). These cropped images are represented as 2D grayscale numpy arrays in my code. Currently, I am able to convert each image to a # Inspired by https://stackoverflow.com/a/28331076
import nibabel as nib
import numpy as np
data = np.arange(4*4).reshape(4,4) # This is the cropped image data
new_image = nib.Nifti1Image(data, affine=np.eye(4)) I am curious if there's a way to write these EDIT: I would like to preserve the image sizes of these |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @emmanuel-nwogu, I hope it's OK I converted your issue to a discussion, since it is more a support question, rather than an issue with nibabel. Unfortunately, I don't think it is possible for a single NIfTI file to hold differently sized 2D arrays to represent a single 3D array. However, I imagine this is practically equivalent to zero-padding the arrays to match the largest dimension sizes. If you do that after cropping, it might be difficult to realign the slices correctly. Therefore, I would suggest finding out how the slice with the largest dimensions is cropped, and then applying the same cropping to all the other slices. Does that make sense? |
Beta Was this translation helpful? Give feedback.
-
Could you share your cropping code? It may be that you could mask the 2D arrays instead and keep the original dimensions. |
Beta Was this translation helpful? Give feedback.
Hi @emmanuel-nwogu,
I hope it's OK I converted your issue to a discussion, since it is more a support question, rather than an issue with nibabel.
Unfortunately, I don't think it is possible for a single NIfTI file to hold differently sized 2D arrays to represent a single 3D array. However, I imagine this is practically equivalent to zero-padding the arrays to match the largest dimension sizes. If you do that after cropping, it might be difficult to realign the slices correctly. Therefore, I would suggest finding out how the slice with the largest dimensions is cropped, and then applying the same cropping to all the other slices. Does that make sense?