From 25145d182dca91b77f2cf9a12381462f3106c8c0 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauld.mccarthy@gmail.com>
Date: Wed, 25 May 2016 12:43:19 +0100
Subject: [PATCH] FNIRT-specific hack - using qform matrix for FNIRT output
 images.

---
 fsl/data/image.py | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/fsl/data/image.py b/fsl/data/image.py
index 8ca0d1ed2..fbebad533 100644
--- a/fsl/data/image.py
+++ b/fsl/data/image.py
@@ -150,9 +150,25 @@ class Nifti1(object):
 
         shape, pixdim = self.__determineShape(self.nibImage)
         
-        self.shape         = shape
-        self.pixdim        = pixdim
-        self.voxToWorldMat = np.array(self.nibImage.get_affine())
+        self.shape  = shape
+        self.pixdim = pixdim
+
+        # We have to treat FSL/FNIRT images
+        # specially, as FNIRT clobbers the
+        # sform section of the NIFTI header
+        # to store other data. The hard coded
+        # numbers here are the intent codes
+        # output by FNIRT.
+        intent = self.nibImage.get_header().get('intent_code', -1)
+        if intent in (2006, 2007, 2008, 2009):
+            log.debug('FNIRT output image detected - using qform matrix')
+            self.voxToWorldMat = self.nibImage.get_qform()
+
+        # Otherwise we let nibabel decide
+        # which transform to use.
+        else:
+            self.voxToWorldMat = np.array(self.nibImage.get_affine())
+
         self.worldToVoxMat = transform.invert(self.voxToWorldMat)
 
         if loadData:
-- 
GitLab