Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Expose ITK Image to MONAI MetaTensor conversion #5708

Closed
ntatsisk opened this issue Dec 12, 2022 · 6 comments · Fixed by #5897
Closed

[Feature request] Expose ITK Image to MONAI MetaTensor conversion #5708

ntatsisk opened this issue Dec 12, 2022 · 6 comments · Fixed by #5897

Comments

@ntatsisk
Copy link

Is your feature request related to a problem? Please describe.
I am trying to convert easily between ITK Image and MONAI MetaTensor. Currently, I am using my own simple function that works for my partcular use case:

def itk_image_to_metatensor(img):
    reader = ITKReader(affine_lps_to_ras=False)
    img_array, meta_data = reader.get_data(img)
    img_array = convert_to_dst_type(img_array, dst=img_array, dtype=itk.D)[0]
    metatensor = MetaTensor.ensure_torch_and_prune_meta(img_array, meta_data)
    metatensor = EnsureChannelFirst()(metatensor)

    return metatensor

which more-or-less the code that lives insde LoadImage: https://github.com/Project-MONAI/MONAI/blob/dev/monai/transforms/io/array.py#L273-L289

Describe the solution you'd like
I would like that MONAI provides two functions e.g. itk_image_to_metatensor() and metatensor_to_itk_image(). The implementation of the first already exists inside LoadImage transform (https://github.com/Project-MONAI/MONAI/blob/dev/monai/transforms/io/array.py#L273-L289) and of the latter inside ITKWriter: https://github.com/Project-MONAI/MONAI/blob/dev/monai/data/image_writer.py#L470-L517. So, ideally they just need to be exposed in order for them to be more easily usable.

Thanks in advance ;)

xref for us: InsightSoftwareConsortium/ITKElastix#126

@Shadow-Devil
Copy link
Contributor

Hi, I would like to work on this issue.
Where should these two functions be placed (maybe inside the monai.data.utils.py file)?

@wyli
Copy link
Contributor

wyli commented Jan 22, 2023

thanks @Shadow-Devil, there's an ongoing PR and possibly it's getting redirected to monai InsightSoftwareConsortium/itk-torch-transform-bridge#6.

@ntatsisk Please let us know if you need any help or prefer us to take over the PR redirection. cc @thewtex @HastingsGreer

@ntatsisk
Copy link
Author

Hi @Shadow-Devil, @wyli, and thanks for the interest! Feel free to take over the PR redirection, and I can help as well. I am looking forward to seeing this integrated to MONAI. :)

I will try to do the final changes in the PR as soon as possible so that it is ready for the transfer.

@dzenanz
Copy link
Contributor

dzenanz commented Jan 23, 2023

Moving these functions into MONAI proper would help with #5711.

@Spenhouet
Copy link
Contributor

Spenhouet commented Jan 27, 2023

The proposed functions might not properly work, see: InsightSoftwareConsortium/itk-torch-transform-bridge#6 (comment)

I suggest we should solve this first.

wyli pushed a commit that referenced this issue Feb 20, 2023
Fixes #5708
Fixes #4117

### Description

This is a migration of the PR (by @ntatsisk https://github.com/ntatsisk)
InsightSoftwareConsortium/itk-torch-transform-bridge#6
into MONAI.

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [ ] Breaking change (fix or new feature that would cause existing
functionality to change).
- [x] New tests added to cover the changes.
- [ ] Integration tests passed locally by running `./runtests.sh -f -u
--net --coverage`.
- [x] Quick tests passed locally by running `./runtests.sh --quick
--unittests --disttests`.
- [ ] In-line docstrings updated.
- [ ] Documentation updated, tested `make html` command in the `docs/`
folder.

---------

Signed-off-by: Felix Schnabel <f.schnabel@tum.de>
@ibro45
Copy link
Contributor

ibro45 commented Sep 14, 2023

In case someone knows what's causing this #6985

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants