Skip to content

Commit

Permalink
Created the apply transfomr workflow.
Browse files Browse the repository at this point in the history
1) Added a function to load the saved affine matrix from the text file.
2) Added the import of ndimage from scipy in the test_parzenhist.py file.
3) Created a new workflow file apply_transform.
  • Loading branch information
Parichit Sharma committed Jun 18, 2018
1 parent c3b4786 commit b765826
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions dipy/align/tests/test_parzenhist.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import scipy as sp
from scipy import ndimage
from functools import reduce
from operator import mul
from dipy.core.ndindex import ndindex
Expand Down
13 changes: 11 additions & 2 deletions dipy/io/image.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import division, print_function, absolute_import

import nibabel as nib
import numpy as np
import nibabel as nib


def load_nifti(fname, return_img=False, return_voxsize=False,
Expand Down Expand Up @@ -37,3 +36,13 @@ def save_affine_matrix(fname, affine):
The object containing the affine matrix.
"""
np.savetxt(fname, affine)


def load_affine_matrix(fname):
"""
Parameters
---------
fname : str
The file containing the saved affine matrix.
"""
return np.loadtxt(fname)
47 changes: 47 additions & 0 deletions dipy/workflows/apply_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from dipy.workflows.workflow import Workflow

from dipy.align.imaffine import AffineMap
import nibabel as nib
import numpy as np
from os.path import join as pjoin
from os import path
from dipy.io.image import save_nifti, load_affine_matrix


class ApplyTransformFlow(Workflow):

def run(self, reference_image_file, moving_image_files, affine_matrix_file,
out_file='transformed.nii.gz'):

"""
Parameters
----------
reference_image_file : string
Path to the static image file.
moving_image_files : string
Location of the file or folder containing the images to be transformed.
affine_matrix_file : string
The text file containing the affine matrix for transformation.
out_file : string, optional
Name of the transformed file. (default 'transformed.nii.gz')
"""

io = self.get_io_iterator()

for ref_image_file, mov_images, affine_matrix_file, out_file in io:

ref_image = nib.load(ref_image_file)
static_grid2world = ref_image.affine

image = nib.load(mov_images)
image_data = np.array(image.get_data())

affine_matrix = load_affine_matrix(affine_matrix_file)

img_transformation = AffineMap(affine=affine_matrix, domain_grid_shape=image_data.shape)
transformed = img_transformation.transform(image_data)

save_nifti(pjoin(path.split(mov_images)[1]+out_file), transformed, affine=static_grid2world)

0 comments on commit b765826

Please sign in to comment.