def create_ct_foam_phantom(shape=501, seed=0, gen_new=False): """""" if gen_new: foam_ct_phantom.FoamPhantom.generate('large_foam_phantom.h5', seed, nspheres_per_unit=10000) # (nx, ny, nx) for phatom volume geometry shape = shape if isinstance(shape, tuple) else (shape, ) * 3 phantom = foam_ct_phantom.FoamPhantom('large_foam_phantom.h5') geom = foam_ct_phantom.VolumeGeometry(*shape, voxsize=3 / shape[0]) phantom.generate_volume('test_phantom.h5', geom) # [z,y,x] axis order phantom_volume = foam_ct_phantom.load_volume('test_phantom.h5') return phantom_volume
startz=1.5, cutoff=1e-2) infiltration_phantom = foam_ct_phantom.InfiltrationFoamPhantom( 'test_infiltration.h5') vol_geom = foam_ct_phantom.VolumeGeometry(256, 256, 1, 3 / 256) infiltration_phantom.generate_volume('test_infiltration_vol_start.h5', vol_geom, time=0) infiltration_phantom.generate_volume('test_infiltration_vol_end.h5', vol_geom, time=1) vol0 = foam_ct_phantom.load_volume('test_infiltration_vol_start.h5') vol1 = foam_ct_phantom.load_volume('test_infiltration_vol_end.h5') pl.imshow(vol0[0]) pl.figure() pl.imshow(vol1[0]) pl.show() proj_geom = foam_ct_phantom.ParallelGeometry( 256, 256, np.linspace(0, 4 * np.pi, 128, False), 3 / 256) infiltration_phantom.generate_projections('test_infiltration_projs.h5', proj_geom) projs = foam_ct_phantom.load_projections('test_infiltration_projs.h5') pl.imshow(projs[0]) pl.figure()
def build_foam_phantom_dataset(folder_path, n_phantoms, spheres_per_unit=10000, GPU_ID=0): # Choose to reset dataset path or not if os.path.exists(folder_path): print("Path already exists, delete?") choice = input() if choice.lower()[0] == 'y': shutil.rmtree(folder_path) os.makedirs(folder_path) else: print("Will overwrite files in folder") shape = (501, ) * 3 n_rad_slices = 360 temp_dir = '_temp_phatoms/' if not os.path.exists(temp_dir): os.mkdir(temp_dir) scanner_params = FleX_ray_scanner() orbit_elevation = [-15, 0, 15] theta_range = np.linspace(0, np.pi, n_rad_slices, endpoint=False) for i in range(n_phantoms): foam_ct_phantom.FoamPhantom.generate( os.path.join(temp_dir, 'foam_phantom.h5'), seed=i, nspheres_per_unit=spheres_per_unit) phantom = foam_ct_phantom.FoamPhantom( os.path.join(temp_dir, 'foam_phantom.h5')) geom = foam_ct_phantom.VolumeGeometry(*shape, voxsize=3 / shape[0]) phantom.generate_volume(os.path.join(temp_dir, 'gen_phantom.h5'), geom) phantom_volume = foam_ct_phantom.load_volume( os.path.join(temp_dir, 'gen_phantom.h5')) if not os.path.exists(os.path.join(folder_path, f'phantom{i+1}/')): os.makedirs(os.path.join(folder_path, f'phantom{i+1}/')) rad_slices = astra_sim.radial_slice_sampling(phantom_volume, theta_range) for j in range(len(rad_slices)): print(f"\rSaving phantom_n{i+1:0>3d}_s{j+1:0>4d}.tif", end=' ' * 5) imsave( os.path.join(folder_path, f'phantom{i+1}/', f"phantom_true_n{i+1:0>3d}_s{j+1:0>4d}.tif"), rad_slices[j].astype('float32')) print('') # Simulates FDK reconstruction for all 3 orbits for orbit in [1, 2, 3]: vecs = astra_sim.create_scan_geometry( scanner_params, 1200, elevation=orbit_elevation[orbit - 1]) projections = astra_sim.create_CB_projection(phantom_volume, scanner_params, proj_vecs=vecs, gpu_id=GPU_ID) fdk_phantom_volume = astra_sim.FDK_reconstruction(projections, scanner_params, proj_vecs=vecs, gpu_id=GPU_ID, rec_shape=(501, 501, 501)) fdk_rad_slices = astra_sim.radial_slice_sampling( fdk_phantom_volume, theta_range) for j in range(len(rad_slices)): print(f"\rSaving phantom_n{i+1:0>3d}_s{j+1:0>4d}.tif", end=' ' * 5) imsave( os.path.join( folder_path, f'phantom{i+1}/', f"phantom_fdk_n{i+1:0>3d}_o{orbit:d}_s{j+1:0>4d}.tif"), fdk_rad_slices[j].astype('float32')) print('') # Removes temp phatom files shutil.rmtree(temp_dir)
""" import foam_ct_phantom import numpy as np import pylab as pl pl.gray() random_seed = 12345 def maxsize_func(x, y, z): return 0.2 - 0.1 * np.abs(z) # Note that nspheres_per_unit is set to a low value to reduce the computation time here. # The default value is 100000. foam_ct_phantom.FoamPhantom.generate('test_uniform.h5', random_seed, nspheres_per_unit=1000, maxsize=maxsize_func) geom = foam_ct_phantom.VolumeGeometry(1, 256, 256, 3 / 256) phantom = foam_ct_phantom.FoamPhantom('test_uniform.h5') phantom.generate_volume('test_uniform_midslice.h5', geom) vol = foam_ct_phantom.load_volume('test_uniform_midslice.h5') pl.imshow(vol[..., 0]) pl.show()
import pylab as pl pl.gray() random_seed = 9876 foam_ct_phantom.ExpandingFoamPhantom.generate('test_expand.h5', 'test_phantom.h5', random_seed) expanding_phantom = foam_ct_phantom.ExpandingFoamPhantom('test_expand.h5') vol_geom = foam_ct_phantom.VolumeGeometry(256, 256, 1, 3 / 256) expanding_phantom.generate_volume('test_expand_vol_start.h5', vol_geom, time=0) expanding_phantom.generate_volume('test_expand_vol_end.h5', vol_geom, time=1) vol0 = foam_ct_phantom.load_volume('test_expand_vol_start.h5') vol1 = foam_ct_phantom.load_volume('test_expand_vol_end.h5') pl.imshow(vol0[0]) pl.figure() pl.imshow(vol1[0]) pl.show() proj_geom = foam_ct_phantom.ParallelGeometry( 256, 128, np.linspace(0, 4 * np.pi, 128, False), 3 / 256) expanding_phantom.generate_projections('test_expand_projs.h5', proj_geom) projs = foam_ct_phantom.load_projections('test_expand_projs.h5') pl.imshow(projs[0]) pl.figure() pl.imshow(projs[-1])
import pylab as pl pl.gray() random_seed = 9876 foam_ct_phantom.MovingFoamPhantom.generate('test_moving.h5', 'test_phantom.h5', random_seed, -0.75, 0.75) moving_phantom = foam_ct_phantom.MovingFoamPhantom('test_moving.h5') vol_geom = foam_ct_phantom.VolumeGeometry(256, 256, 1, 3 / 256) moving_phantom.generate_volume('test_moving_vol_start.h5', vol_geom, time=0) moving_phantom.generate_volume('test_moving_vol_end.h5', vol_geom, time=1) vol0 = foam_ct_phantom.load_volume('test_moving_vol_start.h5') vol1 = foam_ct_phantom.load_volume('test_moving_vol_end.h5') pl.imshow(vol0[0]) pl.figure() pl.imshow(vol1[0]) pl.show() proj_geom = foam_ct_phantom.ParallelGeometry( 256, 128, np.linspace(0, 4 * np.pi, 128, False), 3 / 256) moving_phantom.generate_projections('test_moving_projs.h5', proj_geom) projs = foam_ct_phantom.load_projections('test_moving_projs.h5') pl.imshow(projs[0]) pl.figure() pl.imshow(projs[-1])