I have two nii images, which I load into SimpleITK on Python:
- 3D CT scan
- 3D Segmentation Mask (0s and 1s)
The segmentation mask has the size of the minimum bounding box around the segmentation and therefore is much smaller than the CT Scan, has a different origin and also a different spacing than the CT scan. What I want to accomplish is to transform the segmentation image into an image which has:
- Same size as the CT image
- Same spacing as the CT image
- Same origin as the CT image
- All the extra voxels must be filled with zeros.
Essentially, turn the current segmentation mask into an image where each voxel corresponds to the same same voxel in the CT scan.
How should I go about doing this?
This can be readily done via resampling (details in section titled resampling):
sitk.Resample(mask, image, sitk.Transform(), sitk.NearestNeighbor, 0, mask.GetPixelID())
The one liner assumes that the CT scan is called
image the segmentation
mask, they are aligned in physical space (i.e. transformation is the identity), and we use the nearest-neighbor interpolator so that we do not introduce new labels.
Thanks a lot @zivy, this worked perfectly and I now understand the Resample functionality much more.
@zivy I have a similar problem. Also having 2 nifti images, ct scan and segmentation mask containing (0-3s) label.
The CT scan and segmentation mask has the following differences:
- the origin does not match between the images:
(-126.67142486572266, -100.2229995727539, -35.8679313659668) <-CT scan
(-107.0, -105.69999694824219, -169.0) <-mask
- the spacing does not match between the images
(0.47460898756980896, 0.47460898756980896, 4.999993324279785) <-CT scan
(0.4179689884185791, 0.4179689884185791, 5.0) <-mask
- the direction does not match between the images
(0.9999979873288019, 2.2101333702174357e-05, -0.0020062028280818365, 2.210133363 4976865e-05, 0.9997573031831891, 0.022030304063859862, 0.0020062028502736066, -0 .022030302844218676, 0.99975529048516) <-CT scan
(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0) <-mask
I tried the following:
original_image = sitk.ReadImage(’./188_0000.nii.gz’)
original_segmentation = sitk.ReadImage(’./188.nii.gz’)
new_segmentation = sitk.Resample(original_segmentation, original_image,sitk.Transform(),sitk.sitkNearestNeighbor,0,original_segmentation.GetPixelID())
But the resulting image end up with an empty (all black) image after resampling.
Alternatively, I tried the following and still get all black
new_segmentation = sitk.Resample(original_segmentation, original_image.GetSize(),
Do you have any solution for this?
The first approach should work if the segmentation and original image are spatially aligned.
Have you checked if the segmentation and image overlap in space? They may not overlap due to some error in previous processing. This requires knowing the image size for each of them so that we can see what region in space they occupy.
If you can share the two files would be easier for us to help you along.
Do you achieve to solve this problem?
I’m trying to do something similar.
Thank you very much in advance