if __name__ == '__main__': # -------------Adjustable global parameters---------- n=512 # pixel number m=10 # number of time phases angle = 5 # #sample points = 360/angle on the boundary numOfAngles = int(180/angle) numOfContourPts = int(360/angle) labelID = 1 # 勾画的RS文件中第几个轮廓为GTV # path of the input data folder = 'E:\\MedData\\4DCT-202305\\' #patient = '0007921948' # 缺少时间信息 patient = '0000726380' # 病人的编号 # 呼吸曲线数据文件 vxpPath = folder+patient+'\\0000726380\\0000726380_20230420_143723.vxp' # Save the generated figures to the latex file path figPath = "D:\\HUNNU\\Research\\DMD\\4D-CT\\latex-DMD插值\\modify202305\\figure\\" # -------------Auto generated global parameters---------- # 每个dicom文件包含多少横截面 name = os.listdir(folder+patient+'\\0') cuts = [] for i in range(len(name)): if 'CT' in name[i][0:2]: cuts.append(i+1) cuts = np.array(cuts) # phase name times = np.linspace(0,90,10) # image pixel coordinate nums = np.linspace(0,n-1,n) x,y = np.meshgrid(nums,nums) # 输出dicom头文件信息 filename = folder+patient+'\\0\\CT.{}'.format(patient)+'.Image 1.dcm' print('CT dicom file information:') info = loadFileInformation(filename) # 像素之间的间距,包括列间距和行间距,单位mm SliceThickness = info['SliceThickness'] # Z轴的扫描分辨率,单位mm pixelSpace = info['pixelSpace'] # 一个像素所占的实际体积 pixelVol = float(pixelSpace[0])*float(pixelSpace[0])*float(SliceThickness) print('sliceThickness=',SliceThickness,' pixelSpace=',pixelSpace)
时间: 2024-02-29 11:55:52 浏览: 146
这段代码是一个 Python 脚本中的主函数部分。代码中定义了一些可调整的全局参数,例如像素数目、时间相位数目、采样点角度等。然后根据给定的病人信息和文件路径,读取 DICOM 文件并获取一些头文件信息,例如像素间距、扫描分辨率等。最后打印输出这些信息。
需要注意的是,这段代码中存在一些依赖的库和函数,例如 `os`、`np`、`loadFileInformation` 等。在运行代码之前需要确保这些依赖已经被正确安装和导入。
相关问题
for Itr=1:Max_Itr for i=1:nop % Determin RSs and Search by LTs %-------------------------------------------------------- Rf=((i-1)/(nop-1))(RM-Rm)+Rm; Rd=norm(GOP-GTs(:,RKs(i))); Rs=Rf(Rf>=Rd)+Rd*(Rd>Rf); LTs_C=Create_LTs(No_LTs,Rs,Dim); LTs=repmat(GTs(:,RKs(i)),1,No_LTs)+LTs_C; LTs=SS(LTs,Par_Interval); %---------------- if Graphic_on==1 subplot(2,2,1) hold off pause(0.000001); plot(LTs(1,:),LTs(2,:),'x'); hold on ezplot(['(x-' num2str(GTs(1,RKs(i))) ')^2 + (y-' num2str(GTs(2,RKs(i))) ')^2 -' num2str(Rs^2)],[0 10],[0 10]); hold off xlim([Par_Interval(1,1) Par_Interval(1,2)]); ylim([Par_Interval(2,1) Par_Interval(2,2)]); pbaspect([1 1 1]) title('Local Search') xlabel('x_1') ylabel('x_2') end %---------------- LTs_Cost=Ev_Fcn(LTs,Fcn_Name); [L_min,L_inx]= min(LTs_Cost); if L_min<=LP_Cost(RKs(i)) LP(:,RKs(i))=LTs(:,L_inx); LP_Cost(RKs(i))=L_min; end if L_min<=GOP_Cost GOP_Cost=L_min; GOP=LTs(:,L_inx); end end % Search by GTs %-------------------------------------------------------- for i=1:nop GTs(:,i)=New_GT(GTs(:,i),LP(:,i),GOP,Lambda,Theta,Beta); GTs(:,i)=SS(GTs(:,i),Par_Interval); GTs_Cost(i)=Ev_Fcn(GTs(:,i),Fcn_Name); end % Ranking %-------------------------------------------------------- [Gts_Sorted,RKs]=sort(GTs_Cost); GOP_B=GTs(:,RKs(1)); GOP_Cost_B=Gts_Sorted(1); if GOP_Cost_B<=GOP_Cost GOP_Cost=GOP_Cost_B; GOP=GOP_B; end OP_Cost(Itr+1)=GOP_Cost; %---------------- if Graphic_on==1 subplot(2,2,2) hold off pause(.000001) plot(GTs(1,:),GTs(2,:),'*') hold on plot(GOP(1,:),GOP(2,:),'X','color','red') xlim([Par_Interval(1,1) Par_Interval(1,2)]); ylim([Par_Interval(2,1) Par_Interval(2,2)]); hold off pbaspect([1 1 1]*3) title('Global Search') xlabel('x_1') ylabel('x_2') end %---------------- %---------------- if Graphic_on==1 subplot(2,2,3) hold off pause(.000001) plot(OP_Cost(1:Itr+1)) pbaspect([2 1 1]) xlim([1 Max_Itr+1]) title(['Cost=' num2str(GOP_Cost,'%4.10f')]) xlabel('Iteration') ylabel('Cost') else hold off pause(.000001) plot(0:Itr,OP_Cost(1:Itr+1),'.','MarkerSize',15,'LineStyle','-','Color',[214 30 0]/255,'MarkerEdgeColor',[3 93 118]/255) pbaspect([2 1 1]) title(['Itr=' num2str(Itr) ', Cost=' num2str(GOP_Cost,'%4.10f')]) xlim([0 Max_Itr]) xlabel('Iteration') ylabel('Cost') end %---------------- end 把这段MATLAB代码转换为python代码
以下是将给定的MATLAB代码转换为Python代码的结果:
```python
for Itr in range(1, Max_Itr + 1):
for i in range(1, nop + 1):
# Determine RSs and search by LTs
# --------------------------------------------------------
Rf = ((i - 1) / (nop - 1)) * (RM - Rm) + Rm
Rd = np.linalg.norm(GOP - GTs[:, RKs[i - 1]])
Rs = Rf[Rf >= Rd] + Rd * (Rd > Rf)
LTs_C = Create_LTs(No_LTs, Rs, Dim)
LTs = np.tile(GTs[:, RKs[i - 1]], (1, No_LTs)) + LTs_C
LTs = SS(LTs, Par_Interval)
# ----------------
if Graphic_on == 1:
plt.subplot(2, 2, 1)
plt.cla()
plt.plot(LTs[0, :], LTs[1, :], 'x')
plt.hold(True)
ezplot('(x - %f)^2 + (y - %f)^2 - %f' % (GTs[0, RKs[i - 1]],
GTs[1, RKs[i - 1]], Rs ** 2),
[0, 10], [0, 10])
plt.hold(False)
plt.xlim([Par_Interval[0, 0], Par_Interval[0, 1]])
plt.ylim([Par_Interval[1, 0], Par_Interval[1, 1]])
plt.gca().set_aspect('equal', adjustable='box')
plt.title('Local Search')
plt.xlabel('x_1')
plt.ylabel('x_2')
# ----------------
LTs_Cost = Ev_Fcn(LTs, Fcn_Name)
L_min, L_inx = np.min(LTs_Cost), np.argmin(LTs_Cost)
if L_min <= LP_Cost[RKs[i - 1]]:
LP[:, RKs[i - 1]] = LTs[:, L_inx]
LP_Cost[RKs[i - 1]] = L_min
if L_min <= GOP_Cost:
GOP_Cost = L_min
GOP = LTs[:, L_inx]
# Search by GTs
# --------------------------------------------------------
for j in range(1, nop + 1):
GTs[:, j - 1] = New_GT(GTs[:, j - 1], LP[:, j - 1], GOP, Lambda, Theta, Beta)
GTs[:, j - 1] = SS(GTs[:, j - 1], Par_Interval)
GTs_Cost[j - 1] = Ev_Fcn(GTs[:, j - 1], Fcn_Name)
# Ranking
# --------------------------------------------------------
Gts_Sorted = np.sort(GTs_Cost)
RKs = np.argsort(GTs_Cost)
GOP_B = GTs[:, RKs[0]]
GOP_Cost_B = Gts_Sorted[0]
if GOP_Cost_B <= GOP_Cost:
GOP_Cost = GOP_Cost_B
GOP = GOP_B
OP_Cost[Itr] = GOP_Cost
# ----------------
if Graphic_on == 1:
plt.subplot(2, 2, 2)
plt.cla()
plt.plot(GTs[0, :], GTs[1, :], '*')
plt.hold(True)
plt.plot(GOP[0, :], GOP[1, :], 'X', color='red')
plt.xlim([Par_Interval[0, 0], Par_Interval[0, 1]])
plt.ylim([Par_Interval[1, 0], Par_Interval[1, 1]])
plt.hold(False)
plt.gca().set_aspect('equal', adjustable='box')
plt.title('Global Search')
plt.xlabel('x_1')
plt.ylabel('x_2')
# ----------------
if Graphic_on == 1:
plt.subplot(2, 2, 3)
plt.cla()
plt.plot(OP_Cost[0:Itr + 1])
plt.gca().set_aspect('equal', adjustable='box')
plt.xlim([1, Max_Itr + 1])
plt.title('Cost = %f' % GOP_Cost)
plt.xlabel('Iteration')
plt.ylabel('Cost')
else:
plt.cla()
plt.plot(range(Itr + 1), OP_Cost[0:Itr + 1], '.', MarkerSize=15, LineStyle='-',
Color=[214, 30, 0] / 255, MarkerEdgeColor=[3, 93, 118] / 255)
plt.gca().set_aspect('equal', adjustable='box')
plt.xlim([0, Max_Itr])
plt.title('Itr = %d, Cost = %f' % (Itr, GOP_Cost))
plt.xlabel('Iteration')
plt.ylabel('Cost')
# ----------------
plt.pause(0.0001)
# --------------
# ----------------
```
需要注意的是,这里的代码仅为转换结果,因此无法保证代码正确性。您需要根据原始MATLAB代码和转换结果进行逐行比较,以确保转换结果的正确性。
阅读全文