File tree Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -209,6 +209,17 @@ def from_image(cls, imgobj):
209209
210210 return imgobj .__class__ (field , imgobj .affine , hdr )
211211
212+ @classmethod
213+ def to_image (cls , imgobj ):
214+ """Export a displacements field from a nibabel object."""
215+
216+ hdr = imgobj .header .copy ()
217+
218+ warp_data = imgobj .get_fdata ().reshape (imgobj .shape [:3 ] + (1 , imgobj .shape [- 1 ]))
219+ warp_data [..., (0 , 1 )] *= - 1
220+
221+ return imgobj .__class__ (warp_data , imgobj .affine , hdr )
222+
212223
213224def _is_oblique (affine , thres = OBLIQUITY_THRESHOLD_DEG ):
214225 """
Original file line number Diff line number Diff line change @@ -146,6 +146,17 @@ def from_image(cls, imgobj):
146146 """Import a displacements field from a nibabel image object."""
147147 raise NotImplementedError
148148
149+ @classmethod
150+ def to_filename (cls , img , filename ):
151+ """Export a displacements field to a NIfTI file."""
152+ imgobj = cls .to_image (img )
153+ imgobj .to_filename (filename )
154+
155+ @classmethod
156+ def to_image (cls , imgobj ):
157+ """Export a displacements field image from a nitransforms image object."""
158+ raise NotImplementedError
159+
149160
150161def _ensure_image (img ):
151162 if isinstance (img , (str , Path )):
Original file line number Diff line number Diff line change @@ -190,6 +190,17 @@ def from_image(cls, imgobj):
190190
191191 return imgobj .__class__ (field , imgobj .affine , hdr )
192192
193+ @classmethod
194+ def to_image (cls , imgobj ):
195+ """Export a displacements field from a nibabel object."""
196+
197+ hdr = imgobj .header .copy ()
198+
199+ warp_data = imgobj .get_fdata ()
200+ warp_data [..., 0 ] *= - 1
201+
202+ return imgobj .__class__ (warp_data , imgobj .affine , hdr )
203+
193204
194205def _fsl_aff_adapt (space ):
195206 """
Original file line number Diff line number Diff line change @@ -352,6 +352,18 @@ def from_image(cls, imgobj):
352352
353353 return imgobj .__class__ (field , imgobj .affine , hdr )
354354
355+ @classmethod
356+ def to_image (cls , imgobj ):
357+ """Export a displacements field from a nibabel object."""
358+
359+ hdr = imgobj .header .copy ()
360+ hdr .set_intent ("vector" )
361+
362+ warp_data = imgobj .get_fdata ().reshape (imgobj .shape [:3 ] + (1 , imgobj .shape [- 1 ]))
363+ warp_data [..., (0 , 1 )] *= - 1
364+
365+ return imgobj .__class__ (warp_data , imgobj .affine , hdr )
366+
355367
356368class ITKCompositeH5 :
357369 """A data structure for ITK's HDF5 files."""
You can’t perform that action at this time.
0 commit comments