Skip to content

Instantly share code, notes, and snippets.

@grzanka
Created November 16, 2016 07:54
Show Gist options
  • Save grzanka/2d1d3911acb2886606be1f2a84a17a56 to your computer and use it in GitHub Desktop.
Save grzanka/2d1d3911acb2886606be1f2a84a17a56 to your computer and use it in GitHub Desktop.
import pytrip as pt
# first define some paths and other important parameters
ctx_path = "tests/sphere000.ctx"
vdx_path = "tests/sphere000.vdx"
my_target_voi = "bsPTVClassic0"
# load CT cube
my_ctx = pt.CtxCube()
my_ctx.read(ctx_path)
print(my_ctx.cube.min(), my_ctx.cube.max() )
# load VOIs
my_vdx = pt.VdxCube("", my_ctx) # my_vdx is the object which will hold all volumes of interest and the meta information
my_vdx.read(vdx_path) # load the .vdx file
print(my_vdx.get_voi_names()) # show us all VOIs found in the .vdx file
# Select the requested VOI from the VdxCube object
target_voi = my_vdx.get_voi_by_name(my_target_voi)
print("X max", my_ctx.pixel_size * my_ctx.dimx)
print("Y max", my_ctx.pixel_size * my_ctx.dimy)
print("Z max", my_ctx.slice_distance * my_ctx.dimz)
# get_voi_cube() returns a DosCube() object, where all voxels inside the VOI holds the value 1000, and 0 elsewhere.
voi_cube = target_voi.get_voi_cube()
for k,slice in target_voi.slices.items():
print("slice no", k, slice)
for c in slice.contour:
print("c", c)
for c2 in c.contour:
print("c2", c2)
print("vc", voi_cube.cube.min(), voi_cube.cube.max())
# Based on the retrieved DosCube() we calculate a three dimensional mask object,
# which assigns True to all voxels inside the Voi, and False elsewhere.
mask = (voi_cube.cube == 1000)
# "The mask object and the CTX cube have same dimensions (they are infact inherited from the same top level class).
# Therefore we can apply the mask cube to the ctx cube and work with the values.
# For instance we can set all HUs to zero within the Voi:
my_ctx.cube[mask] = -1000
# or add 100 to all HUs of the voxels inside the mask:
# my_ctx.cube[mask] += 100
# save masked CT to the file in current directory
masked_ctx = "masked.ctx"
my_ctx.write(masked_ctx)
print(my_ctx.cube.max(), my_ctx.cube.min())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment