Resizing a PNG image, working only with dictionary-based transforms #5347
-
|
Hello guys. By following some of the examples online, I'm trying to load and resize a PNG image using Monai's transforms. =========================================First example code -- IT WORKS!=========================================from monai.transforms import (LoadImaged, EnsureChannelFirstd, Resized, EnsureTyped, Compose) transform = Compose([LoadImaged(keys="image"), test_data = {'image': 'sample_image.png'} result = transform(test_data) print(f"image data shape:{result['image'].shape}") =========================================Similar example code -- IT DOESN'T WORK!=========================================from monai.transforms import (LoadImage, EnsureChannelFirst, Resize, EnsureType, Compose) transform = Compose([LoadImage(), result = transform('sample_image.png') print(f'image data shape:{result[0].shape}') =========================================Below a brief text of the console output============================================ Transform input info -- EnsureChannelFirst === ... ValueError: meta_dict not available, EnsureChannelFirst is not in use. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
|
Hi @hblanco2009, may I know your MONAI version? I guess in your case, you can simply use Hope it can help you, thanks! |
Beta Was this translation helpful? Give feedback.
-
|
Dear @KumoLiu, I successfully reinstalled Monai as you suggested. Afterwards, I just substituted the transform "AddChannel" (which is deprecated in this 1.0.0 version) by the suggested transform: EnsureChannelFirst(channel_dim="no_channel"). It is happening exactly the same as with the previous version, despite of the new Monai version. That is, the LoadImage() transform returns a tuple when reading a PNG image. The first component is now a MetaTensor (which contains the pixel matrix), whereas the second component is a dictionary. The EnsureChannelFirst transform is applied to both components, but fails on the dictionary. In the image below, you can see that when calling the EnsureChannelFirst over the object "img" (i.e., img = MetaTensor(img)), which is actually a dictionary, as you can see in the screenshot below, it fails and returns this messge: *** RuntimeError: Could not infer dtype of dict |
Beta Was this translation helpful? Give feedback.

Dear @KumoLiu, I successfully reinstalled Monai as you suggested. Afterwards, I just substituted the transform "AddChannel" (which is deprecated in this 1.0.0 version) by the suggested transform: EnsureChannelFirst(channel_dim="no_channel"). It is happening exactly the same as with the previous version, despite of the new Monai version. That is, the LoadImage() transform returns a tuple when reading a PNG image. The first component is now a MetaTensor (which contains the pixel matrix), whereas the second component is a dictionary. The EnsureChannelFirst transform is applied to both components, but fails on the dictionary. In the image below, you can see that when calling the EnsureChannel…