-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleRegistrationClean.py
82 lines (60 loc) · 2.6 KB
/
SingleRegistrationClean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 11:15:36 2020
@author: Wayan A. Fontaine-Seiler
Simple Program for dual image registration using the SimpleElastix module integrated in SimpleITX with minimal customization option
Dependencies: PILLOW, numpy and custom SimpleITK compiled from source with SimpleElastix module
"""
## Single image Simple registration program using the SITK with SimpleElastix extension
## IMPORT PRE-COMPILED EXTENSIONS & LIBRARIES##
import SimpleITK as sitk # Loads the SimpleITK module compiled from source with the addition of the SimpleElastix addition
print(sitk.__file__) # Tests the version of SimpleITK (Should be SimpleITK-2.0.0rc2.dev908+g8244e-py[your_version_of_python])
import numpy as np
import matplotlib as plt
from PIL import Image # Image manipulation in Python using PILLOW (For of unmaintained PIL)
##IMPORT CUSTOM EXTENSIONS##
from ImageCleanup import ImageCleanup
# IMPORT IMAGES #
fixedImage = sitk.ReadImage('MRIm15Target.png',sitk.sitkInt32)
movingImage = sitk.ReadImage('MRIm15Moving.png',sitk.sitkInt32)
#SET THE PARAMETER MAP (Transformation to apply)#
parameterMap = sitk.GetDefaultParameterMap('translation')
#parameterMap["numberOfResolutions"].append(16)
#Or(To apply successive Registration protocols -- Useful for bspline registration)#
parameterMapVector = sitk.VectorOfParameterMap()
parameterMapVector.append(sitk.GetDefaultParameterMap("affine"))
test = sitk.GetDefaultParameterMap("bspline")
#test["Metric"].append("CorrespondingPointsEuclideanDistanceMetric")
parameterMapVector.append(test)
# APPLY THE PARAMETERS #
elastixImageFilter = sitk.ElastixImageFilter()
elastixImageFilter.SetFixedImage(fixedImage)
elastixImageFilter.SetMovingImage(movingImage)
elastixImageFilter.SetParameterMap(parameterMap) # OR parameterMap for only one step registration
#EXECUTE & GET THE RESULTS #
elastixImageFilter.Execute()
resultImage = elastixImageFilter.GetResultImage()
transformParameterMap = elastixImageFilter.GetTransformParameterMap()
#EXPORT in array #
ndares = sitk.GetArrayFromImage(resultImage)
nda1 = sitk.GetArrayFromImage(fixedImage)
nda2 = sitk.GetArrayFromImage(movingImage)
#
nda1 = nda1.astype(np.uint8)
nda2 = nda2.astype(np.uint8)
ndares = ndares.astype(np.uint8)
ndares=ImageCleanup(ndares,254,128) # Rought Cleanup using the ImageCleanup.py extension module
im1 = Image.fromarray(nda1)
im2 = Image.fromarray(nda2)
imres = Image.fromarray(ndares)
print (imres)
im1.convert('L')
im2.convert('L')
imres.convert('L')
immerge = Image.blend(im1, imres, 0.5)
im1.show()
im2.show()
imres.show()
immerge.show()
immerge.save("MERGESIMPLE","TIFF")