Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
fslpy
Commits
e4acefd6
Commit
e4acefd6
authored
5 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: New Nifti.adjust method for adjusting a header shape/pixdim
parent
127e2dfd
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/data/image.py
+63
-0
63 additions, 0 deletions
fsl/data/image.py
with
63 additions
and
0 deletions
fsl/data/image.py
+
63
−
0
View file @
e4acefd6
...
@@ -839,6 +839,69 @@ class Nifti(notifier.Notifier, meta.Meta):
...
@@ -839,6 +839,69 @@ class Nifti(notifier.Notifier, meta.Meta):
return
code
return
code
def
adjust
(
self
,
pixdim
=
None
,
shape
=
None
,
origin
=
None
):
"""
Return a new ``Nifti`` object with the specified ``pixdim``
or ``shape``. The affine is of the new ``Nifti`` is adjusted
accordingly.
Only one of ``pixdim`` or ``shape`` can be specified.
The default behaviour of the ``origin`` argument (``
'
centre
'
``) causes
the corner voxel of the output to have the same centre as the corner
voxel of the input. If the origin is ``
'
corner
'
``, an offset is
applied which effectively causes the origin corners of the voxel grids
of the input and output to be aligned.
:arg pixdim: New voxel dimensions
:arg shape: New image shape
:arg origin: Voxel grid alignment - either ``
'
centre
'
`` (the default)
or ``
'
corner
'
``
:returns: A new ``Nifti`` object based on this one, with adjusted
pixdims, shape and affine.
"""
if
((
pixdim
is
not
None
)
and
(
shape
is
not
None
))
or
\
((
pixdim
is
None
)
and
(
shape
is
None
)):
raise
ValueError
(
'
Exactly one of pixdim or
'
'
shape must be specified
'
)
newShape
=
shape
newPixdim
=
pixdim
# if pixdims were specified,
# convert them into a shape
if
newPixdim
is
not
None
:
npixdim
=
len
(
newPixdim
)
newPixdim
=
np
.
array
(
newPixdim
)
oldShape
=
np
.
array
(
self
.
shape
[
:
npixdim
])
oldPixdim
=
np
.
array
(
self
.
pixdim
[:
npixdim
])
newShape
=
oldShape
*
(
oldPixdim
/
newPixdim
)
# pad shape to full dimensions
if
len
(
newShape
)
!=
3
:
raise
ValueError
(
'
Three dimensions must be specified
'
)
# Rescale the voxel-to-world affine
xform
=
affine
.
rescale
(
oldShape
,
newShape
,
origin
)
xform
=
affine
.
concat
(
self
.
getAffine
(
'
voxel
'
,
'
world
'
),
xform
)
# Now that we've got our spatial
# scaling/offset matrix, pad the
# new shape/pixdims with those
# from any non-spatial dimensions
newShape
=
list
(
newShape
)
+
list
(
self
.
shape
[
3
:])
newPixdim
=
list
(
newPixdim
)
+
list
(
self
.
pixdim
[
3
:])
# And create the new header
# and we're away
header
=
self
.
header
.
copy
()
header
.
set_data_shape
(
newShape
)
header
.
set_zooms
(
newPixdim
)
header
.
set_sform
(
xform
)
header
.
set_qform
(
xform
)
return
Nifti
(
header
)
class
Image
(
Nifti
):
class
Image
(
Nifti
):
"""
Class which represents a NIFTI image. Internally, the image is
"""
Class which represents a NIFTI image. Internally, the image is
loaded/stored using a :mod:`nibabel.nifti1.Nifti1Image` or
loaded/stored using a :mod:`nibabel.nifti1.Nifti1Image` or
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment