def run(fixed0, fixed1, moving0, moving1, output): """ Registers the two files and outputs the transformed moving file """ # Read images using the SimpleITK IO functions fixed = [pydeform.read_volume(fixed0), pydeform.read_volume(fixed1)] moving = [pydeform.read_volume(moving0), pydeform.read_volume(moving1)] fixed_mask = build_mask(fixed[0]) moving_mask = build_mask(moving[0]) regularization_map = build_regularization_map(fixed[0], 100, 0.15, 0.25) # Perform the registration with open('registration_log.txt', 'w') as f: df = pydeform.register( fixed, moving, settings=settings, log=f, # Log output log_level=pydeform.LogLevel.Verbose, # Log everything fixed_mask=fixed_mask, moving_mask=moving_mask, regularization_map=regularization_map) # Transform with linear interpolation transformed = pydeform.transform(moving[0], df, pydeform.Interpolator.Linear) # Write transformed image to file pydeform.write_volume(output, transformed)
def test_affine(self): # Test affine initialization affine_transform = pydeform.AffineTransform( np.array(( (2, 0, 0), (0, 3, 0), (0, 0, 4) )), np.array((10, 10, 10)) ) # Do a registration pass without actual iterations to see if affine transform is # applied to the resulting displacement field settings = { 'max_iteration_count': 0 } fixed = pydeform.Volume(np.zeros((10,10,10), dtype=np.float32)) moving = pydeform.Volume(np.zeros((10,10,10), dtype=np.float32)) df = pydeform.register( fixed, moving, settings=settings, affine_transform=affine_transform ) df = np.array(df, copy=False) # Ax + b -> A(1, 1, 1) + b -> (2, 3, 4) + (10, 10, 10) -> (12, 13, 14) # u(x) = Ax + b - x self.assertEqual(df[1,1,1,0], 11) self.assertEqual(df[1,1,1,1], 12) self.assertEqual(df[1,1,1,2], 13)
def run(fixed_file, moving_file, output, affine_file=None): """ Registers the two files and outputs the transformed moving file """ # Read images using the IO functions fixed = pydeform.read_volume(fixed_file) moving = pydeform.read_volume(moving_file) # To use multiple image pairs you simply replace fixed and moving with lists # of images. Remember to add an additional image_slot in the settings. # fixed = [pydeform.read_volume((fixed_file0), pydeform.read_volume((fixed_file1)] # moving = [pydeform.read_volume((moving_file0), pydeform.read_volume((moving_file1)] # Optionally, read an affine transform kwargs = {} if affine_file: kwargs['affine_transform'] = pydeform.read_affine_transform( affine_file) # Perform the registration with open('registration_log.txt', 'w') as f: df = pydeform.register( fixed, moving, settings=settings, log=f, # Log output log_level=pydeform.LogLevel.Verbose, # Log everything use_gpu=use_gpu, **kwargs) # Transform with linear interpolation transformed = pydeform.transform(moving, df, pydeform.Interpolator.Linear) # Write transformed image to file pydeform.write_volume(output, transformed)
def _registration_worker(q, args, kwargs): R""" To be ran in a subprocess. Parameters ---------- q: multiprocessing.Queue Queue to return the result. kwargs: dict Keyword arguments for the registration. """ try: result = pydeform.register(*args, **kwargs) except BaseException as e: result = e q.put(result)
def test_register(self): with self.assertRaises(RuntimeError): pydeform.register(None, None) fixed = rand(10, 10, 10) with self.assertRaises(RuntimeError): pydeform.register(fixed, None) moving = rand(10, 10, 10) with self.assertRaises(RuntimeError): pydeform.register(None, moving) fixed = rand(10, 10, 10) moving = [fixed, fixed] with self.assertRaises(ValueError): pydeform.register(fixed, moving) moving = rand(10, 10, 10) fixed = [moving, moving] with self.assertRaises(ValueError): pydeform.register(fixed, moving) fixed = (_gauss3(size=(40, 50, 60), mu=(20, 25, 30), sigma=8) > 0.3).astype(np.float32) moving = (_gauss3(size=(40, 50, 60), mu=(30, 20, 25), sigma=8) > 0.3).astype(np.float32) settings = { 'regularization_weight': 0.05, } d = pydeform.register(fixed, moving, settings=settings) res = _transform(moving, np.array(d), interpolator=sitk.sitkNearestNeighbor) self.assertGreater(_jaccard(res > 0.1, fixed > 0.1), 0.97)