Getting affine matrix error using loadimaged to read nii files. #5098
-
I want to use Monai to read the nii mask image from Pmod output. The original image can be read normally, but there is an error when reading the mask image. How should I solve it? monai version: 0.8.1
with open
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
the meta info doesn't match, so it doesn't make sense to stack them into a single array. from monai.transforms import Compose, EnsureTyped, LoadImaged, Orientationd, EnsureChannelFirstd, ConcatItemsd
def toLoadNiiFile(path):
trans = Compose([ LoadImaged(keys=['img']), EnsureChannelFirstd(keys="img"), Orientationd('img', "RAS"), EnsureTyped(keys=['img'], track_meta=False)])
return trans({'img':path})['img']
t1 = toLoadNiiFile('NPC_AI_Detection_01 _SUVconversion.nii') # Is normally.
t2 = toLoadNiiFile('NPC_AI_Detection_01_2.5_TN_merge.nii') # Is error
print(t1.shape)
print(t2.shape)
output = ConcatItemsd(keys=['img1', 'img2'], name='img3')({'img1': t1, 'img2': t2})
print(output['img3'].shape) ( this script requires monai 0.9.1) |
Beta Was this translation helpful? Give feedback.
-
Hi @wyli , from monai.transforms import Compose, EnsureTyped, LoadImaged, Orientationd, EnsureChannelFirstd, SaveImage
import numpy as np
def toLoadNiiFile(path):
trans = Compose([ LoadImaged(keys=['img'], meta_keys='meta'), EnsureChannelFirstd(keys="img"), Orientationd('img', "RAS"), EnsureTyped(keys=['img'], track_meta=False)])
return trans({'img':path})
data = toLoadNiiFile('NPC_AI_Detection_01 _SUVconversion.nii')
img, meta = data['img'], data['meta']
img = img[0,:,:,30:80]
meta['spatial_shape'] = np.array(img.shape, dtype=np.int16)
saver = SaveImage(output_dir='new', mode='bilinear', output_postfix='')
saver(img, meta_data=meta) |
Beta Was this translation helpful? Give feedback.
-
it's possible in v0.9.1 and newer versions with the metatensor, and from monai.transforms import Compose, EnsureTyped, LoadImaged, Orientationd, EnsureChannelFirstd, SaveImaged, SpatialCropd
import numpy as np
def toLoadNiiFile(path):
trans = Compose([ LoadImaged(keys=['img'], meta_keys='meta'), EnsureChannelFirstd(keys="img"), Orientationd('img', "RAS"), EnsureTyped(keys=['img'])])
return trans({'img':path})
data = toLoadNiiFile('NPC_AI_Detection_01 _SUVconversion.nii')
data = SpatialCropd(keys="img", roi_start=30, roi_end=80)(data)
saver = SaveImaged(keys="img", output_dir='new', output_postfix='', resample=False)
saver(data) |
Beta Was this translation helpful? Give feedback.
it's possible in v0.9.1 and newer versions with the metatensor, and
SpatialCropd
does the slicing: