ccipd
/
LuMiRa-An-Integrated-Lung-Deformation-Atlas-and-3D-CNN-model-of-Infiltrates-for-COVID-19-Prognosis
Public
forked from amogh3892/LuMiRa-An-Integrated-Lung-Deformation-Atlas-and-3D-CNN-model-of-Infiltrates-for-COVID-19-Prognosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
medImageProcessingUtil.py
96 lines (66 loc) · 3.65 KB
/
medImageProcessingUtil.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
83
84
85
86
87
88
89
90
91
import SimpleITK as sitk
import numpy as np
import pandas as pd
import os
import cv2
from pathlib import Path
import subprocess
class MedImageProcessingUtil(object):
def __init__(self):
pass
@staticmethod
def copyImageHeaders(toImage,fromImage):
toImage.SetSpacing(fromImage.GetSpacing())
toImage.SetDirection(fromImage.GetDirection())
toImage.SetOrigin(fromImage.GetOrigin())
return toImage
@staticmethod
def cv2inpaint(ipatch,mpatch = None):
if ipatch.ndim == 2:
ipatch = ipatch.astype(np.float32)
elif ipatch.ndim == 3:
ipatch = ipatch.astype(np.uint8)
if ipatch[ipatch == 0].size > 0 :
ipatch = np.pad(ipatch, 1, mode='constant', constant_values=0)
if mpatch is None:
mpatch = np.zeros(ipatch.shape)
mpatch[ipatch == 0] = 1
else:
mpatch = np.pad(mpatch, 1, mode='constant', constant_values=0)
mpatch = mpatch.astype(np.uint8)
dst = cv2.inpaint(ipatch,mpatch,3,cv2.INPAINT_NS)
dst = dst[1:-1,1:-1]
else:
dst = ipatch
return dst
@staticmethod
def registerImages(fixedpath,movingpath,parameterpath,outputfolder,fixedmaskpath=None,movingmaskpath=None):
cmd = "export PATH=/Users/amogh3892/Documents/Softwares/elastix/elastix_macosx64_v4/bin:$PATH\nexport DYLD_LIBRARY_PATH=/Users/amogh3892/Documents/Softwares/elastix/elastix_macosx64_v4/lib:$DYLD_LIBRARY_PATH\n"
if (fixedmaskpath and movingmaskpath) is not None:
cmd = cmd + 'elastix -f {} -fMask {} -m {} -mMask {} -p {} -out {}'.format(fixedpath,fixedmaskpath,movingpath,movingmaskpath,parameterpath,outputfolder)
elif fixedmaskpath is not None :
cmd = cmd + 'elastix -f {} -fMask {} -m {} -p {} -out {}'.format(fixedpath,fixedmaskpath,movingpath,parameterpath,outputfolder)
else:
cmd = cmd + 'elastix -f {} -m {} -p {} -out {}'.format(fixedpath,movingpath,parameterpath,outputfolder)
if not cmd is None:
subprocess.call([cmd],shell=True)
else:
print("Masks not provided properly")
@staticmethod
def transformImages(movingpath,transformationpath,outputfolder,mask=None):
cmd = "export PATH=/Users/amogh3892/Documents/Softwares/elastix/elastix_macosx64_v4/bin:$PATH\nexport DYLD_LIBRARY_PATH=/Users/amogh3892/Documents/Softwares/elastix/elastix_macosx64_v4/lib:$DYLD_LIBRARY_PATH\n"
if mask is not None:
with open(fr"{transformationpath}/TransformParameters.0.txt","r") as infile:
trans = infile.read()
infile.close()
trans = trans.replace('(ResampleInterpolator "FinalLinearInterpolator")','(ResampleInterpolator "FinalNearestNeighborInterpolator")')
trans = trans.replace('(ResampleInterpolator "FinalBSplineInterpolator")','(ResampleInterpolator "FinalNearestNeighborInterpolator")')
trans = trans.replace("(FinalBSplineInterpolationOrder 3)","(FinalBSplineInterpolationOrder 0)")
with open(fr"{transformationpath}/TransformParametersMASK.0.txt","w") as infile:
infile.writelines(trans)
infile.close()
cmd = cmd + "transformix -in {} -tp {}/TransformParametersMASK.0.txt -out {}".format(movingpath,transformationpath,outputfolder)
subprocess.call([cmd],shell=True)
else:
cmd = cmd + "transformix -in {} -tp {}/TransformParameters.0.txt -out {}".format(movingpath,transformationpath,outputfolder)
subprocess.call([cmd],shell=True)