-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTExport.py
155 lines (137 loc) · 6.44 KB
/
RTExport.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import dicom
import copy
from os import listdir
from os.path import isfile, isdir, join, splitext
from scipy import ndimage
from skimage import measure
import numpy as np
class RTExport(object):
dicomPath = None
sourceStructurePath = None
destStructurePath = None
sourceDs = None
destDs = None
structureSetList = None
roiObservationList = None
roiContourList = None
def __init__(self, dicomPath, sourceStructurePath, destStructurePath):
self.dicomPath = dicomPath
self.destStructurePath = destStructurePath
self.sourceStructurePath = sourceStructurePath
#self.structurePath = [dicomPath + "/" + f for f in listdir(dicomPath) if isfile(join(dicomPath, f)) and f.startswith('RS')]
self.sourceDs = dicom.read_file(self.sourceStructurePath)
self.destDs = copy.deepcopy(self.sourceDs)
def get_by_structure(self, structure_name):
structureSetList = self.sourceDs.StructureSetROIs
roiObservationList = self.sourceDs.RTROIObservations
roiContourList = self.sourceDs.ROIContours
for i in range(len(structureSetList)):
if structureSetList[i].ROIName == structure_name:
structureSet = copy.deepcopy(structureSetList[i])
roiObservation = copy.deepcopy(roiObservationList[i])
roiContour = copy.deepcopy(roiContourList[i])
return structureSet, roiObservation, roiContour
return None, None, None
def modifyStructureSet(self, structureSet,roiNumber):
structureSet.ROIName += " AUTO"
structureSet.ROINumber = roiNumber
return structureSet
def modifyRoiObservation(self, roiObservation, roiNumber):
roiObservation.ObservationNumber = roiNumber
roiObservation.RefdROINumber = roiNumber
roiObservation.ReferencedROINumber = roiNumber
return roiObservation
#label_list=[(sopInstanceUID,[(x,y,z),(x,y,z)]),(sopInstanceUID,[(x,y,z),(x,y,z)])]
def modifyRoiContour(self, roiContour, roiNumber, label_list):
roiContour.RefdROINumber = roiNumber
roiContour.ReferencedROINumber = roiNumber
contour = roiContour.Contours[0]
contour_list = []
for label in label_list:
(sopInstanceUID, points_list) = label
contour_temp = copy.deepcopy(contour)
contour_temp.ContourImages[0].ReferenedSOPInstanceUID = sopInstanceUID
contour_temp.NumberofContourPoints = len(points_list)
contour_temp.ContourData = []
contour_data = []
for item in points_list:
contour_data.append(item[0])
contour_data.append(item[1])
contour_data.append(item[2])
contour_temp.ContourData = contour_data
contour_list.append(contour_temp)
roiContour.Contours = contour_list
return roiContour
def addNewLabel(self, name, label_list):
roiNumber = 0
for structureSet in self.destDs.StructureSetROIs:
if structureSet.ROINumber > roiNumber:
roiNumber = structureSet.ROINumber
roiNumber += 1
structureSet, roiObservation, roiContour = self.get_by_structure(name)
structureSet = self.modifyStructureSet(structureSet,roiNumber)
structureSetList = self.destDs.StructureSetROIs
structureSetList.append(structureSet)
roiObservation = self.modifyRoiObservation(roiObservation,roiNumber)
roiObservationList = self.destDs.RTROIObservations
roiObservationList.append(roiObservation)
roiContour = self.modifyRoiContour(roiContour,roiNumber,label_list)
roiContourList = self.destDs.ROIContours
roiContourList.append(roiContour)
#print roiContour
#image_list=[(sliceLocation,sopInstanceUID,startPosition),(sliceLocation,sopInstanceUID,startPosition)]
def getImageList(self):
path = self.dicomPath
path_list = [path+"/"+f for f in listdir(path) if isfile(join(path, f)) and f.startswith('CT')]
image_list = []
for path in path_list:
image_data = dicom.read_file(path)
sliceLocation = image_data.SliceLocation
sopInstanceUID = image_data.SOPInstanceUID
startPosition = image_data.ImagePositionPatient
image_list.append((sliceLocation,sopInstanceUID,startPosition))
image_list.sort()
sorted(image_list, key=lambda data : data[0])
return image_list
'''
def createLabelList(self, image_list, points_list):
label_list = []
for (index,points) in points_list:
(sliceLocation, sopInstanceUID, startPosition) = image_list[index]
points = [(point[0]+startPosition[0],point[1]+startPosition[1],startPosition[2]) for point in points]
label_list.append((sopInstanceUID,points))
return label_list
'''
def createLabelList(self, image_list, points_list):
label_list = []
for (index,list) in points_list:
for points in list:
(sliceLocation, sopInstanceUID, startPosition) = image_list[index]
points = [(point[0]+startPosition[0],point[1]+startPosition[1],startPosition[2]) for point in points]
label_list.append((sopInstanceUID,points))
return label_list
def addStructure(self,structureName, points_list):
image_list = self.getImageList()
label_list = self.createLabelList(image_list, points_list)
self.addNewLabel(structureName, label_list)
def save(self):
self.destDs.save_as(self.destStructurePath)
'''
if __name__ == "__main__":
source = "Dataset/data1/V13195/RS.1.2.246.352.71.4.126422491061.189601.20151221094515.dcm"
dest = "Dataset/rstest.dcm"
rtExport = RTExport(source, dest)
#print rtExport.structureSetList
name = "Urinary Bladder"
rtExport.get_by_structure(name)
'''
if __name__ == "__main__":
source = "Dataset/data1/RS.1.2.246.352.71.4.126422491061.189601.20151221094515.dcm"
dest = "Dataset/data1/test.dcm"
rtExport = RTExport(source, dest)
#print rtExport.structureSetList
name = "Urinary Bladder"
label_list = [("1.3.12.2.1107.5.1.4.49611.30000014052605590490600002095",[(-45,-234,-381.0),(20,-234,-381.0),(20,-200,-381.0),(-45,-200,-381.0)]),
("1.3.12.2.1107.5.1.4.49611.30000014052605590490600002096",[(-45,-234,-386.0),(20,-234,-386.0),(20,-200,-386.0),(-45,-200,-386.0)])]
rtExport.addNewLabel(name,label_list)
rtExport.save()