-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiconeimagesJDK_windowsQAmodeALLcones.py
157 lines (131 loc) · 5.96 KB
/
multiconeimagesJDK_windowsQAmodeALLcones.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
156
157
"""
Created on Oct 2017
@author: justin
"""
import glob
import numpy as np
import matplotlib.pyplot as plt
import os.path
import dicom
import pylab
from scipy.interpolate import UnivariateSpline
from matplotlib.backends.backend_pdf import PdfPages
d = "\\\\10.20.67.201\\D$\\_Justin\\__JustinPythonCode__\\20181017_all\\" #Enter Directory where dicom image files are saved
pp = PdfPages(d + "ConeAnalysisResults.pdf")
ConeDiamm=[4,5,7.5,10,12.5,15,17.5] #Enter Cone Diameter in mm
BeamCenters=[]
kk=0
for k in glob.glob(d + '*.dcm'): #only tested when file names are sequential, ie 04mm.dcm, 05mm.dcm, ...10mm.dcm
dcmFiles = []
dcmFiles=dicom.read_file(k)
dcmFilespix=dcmFiles.pixel_array
#plt.imshow(dcmFilespix)
#invert due to acquistion in QA mode
orig_array=dcmFilespix
#dcmFilespix=65535 - orig_array #apparently does the same function as invert
dcmFilespix=np.invert(orig_array)
# Normalize the image to the max value in the image.
maxpix=np.max(dcmFilespix)
dcmFilespixNorm=dcmFilespix/maxpix
#plt.imshow(dcmFilespixNorm)
Width=dcmFilespix.shape[1]
Height=dcmFilespix.shape[0]
halfWidth=int(Width/2)
halfHeight=int(Height/2)
pixelWidth = dcmFiles.ImagePlanePixelSpacing[1]
pixelHeight = dcmFiles.ImagePlanePixelSpacing[0]
SID=dcmFiles.RTImageSID
ConeDiapix=ConeDiamm[kk]/pixelWidth*(SID/1000) #for now assuming pixelWidth=PixelHeight
CollAng = str(int(round(dcmFiles.BeamLimitingDeviceAngle)))
left=int(halfWidth-round(ConeDiapix))
right=int(halfWidth+round(ConeDiapix))
top=int(halfHeight-round(ConeDiapix))
bottom=int(halfHeight+round(ConeDiapix))
dcmFilespixNormROI=dcmFilespixNorm[top:bottom, left:right]
maxpixloc=np.where(dcmFilespixNormROI==1)
maxpixloc=np.array(maxpixloc)
maxpixloc=maxpixloc[:,0]
WidthROI=dcmFilespixNormROI.shape[1]
HeightROI=dcmFilespixNormROI.shape[0]
y=np.linspace(left,left+WidthROI-1,WidthROI)
x=np.linspace(top,top+HeightROI-1,HeightROI)
maxfwhmy=0 #loop through columns
for i in range(np.asscalar(maxpixloc[1])-round(0.4*ConeDiapix),np.asscalar(maxpixloc[1])+round(0.4*ConeDiapix)):
splinefit=UnivariateSpline(y,dcmFilespixNormROI[:,i]-np.max(dcmFilespixNormROI[:,i])/2,s=0) #see sbcrowe.net/python-exercise-field-size-calculator/
if len(splinefit.roots())==2: ## have had code crash sometimes due to root not being found correctly
r1,r2=splinefit.roots()
fwhm=abs(r1-r2)
if fwhm > maxfwhmy:
maxfwhmy=fwhm
columnfwhm=i
fwhmr1y=r1
fwhmr2y=r2
fwhmyloc=(r1+r2)/2 #location of cone determined as center of peak
plt.figure()
pylab.plot(y,dcmFilespixNormROI[:,columnfwhm], label="inplane")
plt.axvspan(fwhmr1y,fwhmr2y,facecolor='b',alpha=0.4)
maxfwhmx=0 #loop through rows
for j in range(np.asscalar(maxpixloc[0])-round(0.4*ConeDiapix),np.asscalar(maxpixloc[0])+round(0.4*ConeDiapix)):
splinefit=UnivariateSpline(x,dcmFilespixNormROI[j,:]-np.max(dcmFilespixNormROI[j,:])/2,s=0)
if len(splinefit.roots())==2:
r1,r2=splinefit.roots()
fwhm=abs(r1-r2)
if fwhm > maxfwhmx:
maxfwhmx=fwhm
rowfwhm=j
fwhmr1x=r1
fwhmr2x=r2
fwhmxloc=(r1+r2)/2
pylab.plot(x,dcmFilespixNormROI[rowfwhm,:], label="crossplane")
plt.axvspan(fwhmr1x,fwhmr2x,facecolor='r',alpha=0.4)
plt.title(str(ConeDiamm[kk]) + "mm Cone, COLL " + CollAng + "Deg Profiles")
plt.xlabel(" pixels")
plt.legend()
plt.grid()
FWHMcross=(abs(fwhmr1x-fwhmr2x))*pixelWidth*1000/SID
#print("FWHM crossplane projected to iso = " + str(FWHMcross) +"mm")
FWHMin=(abs(fwhmr1y-fwhmr2y))*pixelHeight*1000/SID
#print("FWHM inplane projected to iso = " + str(FWHMin) +"mm")
captiontext=("FWHM crossplane projected to iso = " + str(FWHMcross) +"mm" + "\n" + "FWHM inplane projected to iso = " + str(FWHMin) +"mm")
plt.figtext(0,0,captiontext)
#plt.show()
pp.savefig()
plt.close()
plt.figure()
plt.imshow(dcmFilespixNormROI, cmap='Greys', extent=(left,right,bottom,top))
plt.scatter(fwhmxloc,fwhmyloc, color='r', marker='+', s=500)
plt.title(str(ConeDiamm[kk]) + "mm Cone, COLL " + CollAng + "Deg")
#plt.show()
pp.savefig()
plt.close()
BeamCenter=[fwhmxloc,fwhmyloc,int(round(dcmFiles.BeamLimitingDeviceAngle))]
BeamCenters.append(BeamCenter)
kk=kk+1
npBeamCenters=np.array(BeamCenters)
#plt.figure()
#plt.scatter(npBeamCenters[:,0],npBeamCenters[:,1], color='r', marker='+', s=500)
#plt.xlabel("pixels")
#pp.savefig()
#plt.close()
averagex=np.mean(npBeamCenters[:,0])
averagey=np.mean(npBeamCenters[:,1])
xdist=np.vstack(npBeamCenters[:,0]-averagex)*pixelWidth*(1000/SID)
ydist=np.vstack(npBeamCenters[:,1]-averagey)*pixelWidth*(1000/SID)
CollAngles=np.vstack(npBeamCenters[:,2])
CenterDistfromAvgmm=np.hstack((xdist,ydist,CollAngles)) #still assuming pixel width=height
DistancefromAvg=np.sqrt(np.square(CenterDistfromAvgmm[:,0])+np.square(CenterDistfromAvgmm[:,1]))
DistancefromAvg=np.vstack(DistancefromAvg)
AverageDistancefromAvg=np.mean(DistancefromAvg)
MaxDistancefromAvg=np.max(DistancefromAvg)
plt.figure()
plt.scatter(CenterDistfromAvgmm[:,0],CenterDistfromAvgmm[:,1], color='r', marker='+', s=500)
plt.xlabel(' mm')
plt.ylabel('mm')
plt.title('Cone center coordinates relative to average, projected to isocenter')
captiontext=("Average Distance from Center = " + str(AverageDistancefromAvg) +"mm" + "\n" + "Max Distance from Center = " + str(MaxDistancefromAvg) +"mm")
plt.figtext(0,0,captiontext)
pp.savefig()
plt.close()
pp.close()
OutputTable=np.hstack((CollAngles,DistancefromAvg))
print("Collimator Angle & Distance from Center" + "\n" + str(OutputTable))