SaveImage doesn't work well #8287
Unanswered
paproaskwar
asked this question in
Q&A
Replies: 1 comment
-
I got the reason. When I read the input images, using EnsureChannelFirstd, which changes the affine from 2d to 3d |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to use Autoencoder reference to reconstruct the input CT image. I want to change the autoencoder's output's space, so I use the resample and set the meta equals to the input's meta
the code is:
reconstruction, _, _ = dynamic_infer(val_inferer, autoencoder, images)
reconstruction.meta = images.meta
img_saver = SaveImage(
output_dir="output",
output_postfix=output_postfix + "_image",
output_ext=".nii.gz",
separate_folder=False,
resample=True,
mode="nearest",
# dtype=np.float32
)
img_saver(reconstruction[0]) # image is the input CT
However it can't work:
Only 2d and 3d affine are supported, got 0d input.
Traceback (most recent call last):
File ".../maisi/test/xu_ttest.py", line 133, in
img_saver(reconstruction[0])
File "../enter/envs/new/lib/python3.9/site-packages/monai/transforms/io/array.py", line 520, in call
raise RuntimeError(
RuntimeError: SaveImage cannot find a suitable writer for output/sample_20250108_210439_751563_image.nii.gz.
Please install the writer libraries, see also the installation instructions:
https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies.
The current registered writers for .nii.gz: (<class 'monai.data.image_writer.NibabelWriter'>, <class 'monai.data.image_writer.ITKWriter'>).
Traceback (most recent call last):
File "../enter/envs/new/lib/python3.9/site-packages/monai/transforms/io/array.py", line 505, in call
writer_obj.set_metadata(meta_dict=meta_data, **self.meta_kwargs)
File "../enter/envs/new/lib/python3.9/site-packages/monai/data/image_writer.py", line 604, in set_metadata
self.data_obj, self.affine = self.resample_if_needed(
File "../enter/envs/new/lib/python3.9/site-packages/monai/data/image_writer.py", line 273, in resample_if_needed
output_array = resampler(
File "../enter/envs/new/lib/python3.9/site-packages/monai/transforms/spatial/array.py", line 226, in call
return spatial_resample(
File "../enter/envs/new/lib/python3.9/site-packages/monai/transforms/spatial/functional.py", line 105, in spatial_resample
src_affine = to_affine_nd(spatial_rank, src_affine).to(torch.float64)
File "../enter/envs/new/lib/python3.9/site-packages/monai/data/utils.py", line 977, in to_affine_nd
raise ValueError(f"affine must have 2 dimensions, got {affine_np.ndim}.")
ValueError: affine must have 2 dimensions, got 3.
Traceback (most recent call last):
File ".../enter/envs/new/lib/python3.9/site-packages/monai/transforms/io/array.py", line 505, in call
writer_obj.set_metadata(meta_dict=meta_data, **self.meta_kwargs)
File "../enter/envs/new/lib/python3.9/site-packages/monai/data/image_writer.py", line 440, in set_metadata
self.data_obj, self.affine = self.resample_if_needed(
File "../enter/envs/new/lib/python3.9/site-packages/monai/data/image_writer.py", line 273, in resample_if_needed
output_array = resampler(
File "../enter/envs/new/lib/python3.9/site-packages/monai/transforms/spatial/array.py", line 226, in call
return spatial_resample(
File "../enter/envs/new/lib/python3.9/site-packages/monai/transforms/spatial/functional.py", line 105, in spatial_resample
src_affine = to_affine_nd(spatial_rank, src_affine).to(torch.float64)
File "../enter/envs/new/lib/python3.9/site-packages/monai/data/utils.py", line 977, in to_affine_nd
raise ValueError(f"affine must have 2 dimensions, got {affine_np.ndim}.")
ValueError: affine must have 2 dimensions, got 3.
Beta Was this translation helpful? Give feedback.
All reactions