def calc_volume(roi):
# oar and ptv are lists using str(z) as keys
# each item is an ordered list of points representing a polygon
# polygon n is inside polygon n-1, then the current accumulated polygon is
# polygon n subtracted from the accumulated polygon up to and including polygon n-1
# Same method DICOM uses to handle rings and islands
volume = 0.
all_z_values = [round(float(z), 2) for z in list(roi)]
all_z_values = np.sort(all_z_values)
thicknesses = np.abs(np.diff(all_z_values))
thicknesses = np.append(thicknesses, np.min(thicknesses))
all_z_values = all_z_values.tolist()
for z in list(roi):
# z in coord will not necessarily go in order of z, convert z to float to lookup thickness
# also used to check for top and bottom slices, to add area of those contours
thickness = thicknesses[all_z_values.index(round(float(z), 2))]
shapely_roi = points_to_shapely_polygon(roi[z])
if shapely_roi:
volume += shapely_roi.area * thickness
return round(volume / 1000., 2)
评论列表
文章目录