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

NRRDLoader handling of coronal and sagittal oriented scans. #21962

Merged

Conversation

developers-mirrorme3d
Copy link
Contributor

@developers-mirrorme3d developers-mirrorme3d commented Jun 8, 2021

Related issue: Fixed #21898

Description

The IJK -> RAS transformation matrix is being loaded with hard-coded assumptions. The scans are presumed to fall under an (X, Y, Z) order. The transformation matrix is assumed to be the following.

[1,0,0]
[0,1,0]
[0,0,1]

This is true for any NRRD with an axial scan order, however, as a result of this assumption any coronal or sagittal oriented scan will be incorrectly transformed and look incredibly stretched on one axis and squished on another. The transformation matrix could be in the forms of:

Coronal:                Sagittal:
[1, 0, 0]               [0, 1, 0]
[0, 0, 1]               [0, 0, 1]
[0, 1, 0]               [1, 0, 0]

To illustrate this point, the header of an axial oriented NRRD is as follows, you will notice that the space directions field is in the form of the identity matrix and thus will be processed correctly by the NRRDLoader as it is now.

type: int
dimension: 3
space: left-posterior-superior
sizes: 512 512 149
space directions: (1.1972659999999999,0,0) (0,1.1972659999999999,0) (0,0,2.5168918918918921)
kinds: domain domain domain
endian: little
encoding: gzip
space origin: (-306.5,-306.5,-176.75)

An NRRD with a coronal scan order can have a header with space directions in a different orientation, which is where the NRRDLoader will struggle to transform properly:

type: int
dimension: 3
space: left-posterior-superior
sizes: 512 512 106
space directions: (0.48828100000000002,0,0) (0,0,-0.48828100000000002) (0,2,0)
kinds: domain domain domain
endian: little
encoding: gzip
space origin: (-129.63867200000004,-101.337891,147.570007)

The transformation matrix can also hold negative values which indicate a flip, previously ignored and instead operated on by the space field within an NRRD. The proposed changes within this PR take this into account by the space directions matrix instead.

To summarize the proposed solution included in this PR:

  • First identify the indices of each axis from reading the space directions matrix
  • Re-order the reading of the matrix based on these axis.
  • Use the space directions matrix as the source of truth for axis flipping instead of the space header entry

developers-mirrorme3d and others added 2 commits June 8, 2021 11:56
…ections" field within an NRRD file to orient and transform the volume structures properly in 3D space, fixing the previously broken sagittal and coronal oriented scans.
@developers-mirrorme3d developers-mirrorme3d changed the title #21898 NRRDLoader #21898 NRRDLoader handling of coronal and sagittal oriented scans. Jun 8, 2021
@mrdoob mrdoob added this to the r130 milestone Jun 8, 2021
…d only how the spacing (and thus, the size) is calculated by changing its read target. This is done by storing axis order as a new member of the Volume objects
@developers-mirrorme3d
Copy link
Contributor Author

developers-mirrorme3d commented Jun 14, 2021

The newest commit improves upon the first iteration of the solution by allowing for future extensions to support different coordinate systems. For now, the solution fixes the NRRDLoader's initial ability to load both left-posterior-superior and left-anterior-superior coordinate systems by properly supporting axial, coronal, and sagittal oriented scans, whereas previously only axial scans properly loaded.

Before the suggested additions to the code, a sagittal scan would look similar to this:
brokenNRRDLoader

After the fix, they now scale properly.
fixedNRRDLoader

For the above examples, an NRRD from the following DICOM directory is used.

Some additional notes gathered from implementing this fix, for future thought:

  • The NRRDLoader currently assumes that the output coordinate system is RAS, which is why there are transformations being done on the NRRDs to convert them from LPS/LAS -> RAS. This should be configurable in the future, as most medical systems use LPS as a default.
  • The NRRDLoader only supports LPS/LAS coordinate systems, which can be extended to the other remaining ones in the future as needed.

This PR is intended to resolve the immediate issue of improper scaling of slices. Additional improvements or additions will be in a separate PR.

@mrdoob mrdoob changed the title #21898 NRRDLoader handling of coronal and sagittal oriented scans. NRRDLoader handling of coronal and sagittal oriented scans. Jun 17, 2021
@mrdoob mrdoob merged commit 4e0669a into mrdoob:dev Jun 17, 2021
@mrdoob
Copy link
Owner

mrdoob commented Jun 17, 2021

Thanks!

@mrdoob mrdoob removed their request for review June 17, 2021 10:43
@mrdoob
Copy link
Owner

mrdoob commented Jun 17, 2021

@developers-mirrorme3d Made some clean up. Hopefully I didn't break anything. 2f6358d

@joshuaellis joshuaellis mentioned this pull request Jul 5, 2021
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NRRD volumes do not size slices properly when using NRRDs with a scan order different from axial.
3 participants