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
# #This file is part of foam_ct_phantom, a Python package for generating #foam-like phantoms for CT. #----------------------------------------------------------------------- """ Example 04: Generate cone-beam projections ========================================== """ import foam_ct_phantom import numpy as np import h5py import pylab as pl pl.gray() phantom = foam_ct_phantom.FoamPhantom('test_phantom.h5') geom = foam_ct_phantom.ConeGeometry(256, 256, np.linspace(0, 2 * np.pi, 256), 3 / 256, sod=2, odd=0, usecuda=False) phantom.generate_projections('test_projs_cone.h5', geom) projs = foam_ct_phantom.load_projections('test_projs_cone.h5') pl.imshow(projs[0]) pl.show()
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)
else: random_seed = int(args.seed) prefix = "data/" + str(random_seed) + "_" suffix = "_" + str(args.spheres) + "spheres.h5" # 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(prefix + 'phantom' + suffix, random_seed, nspheres_per_unit=args.spheres) ### 03 create parallel projection print("Projecting...") phantom = foam_ct_phantom.FoamPhantom(prefix + 'phantom' + suffix) geom = foam_ct_phantom.ParallelGeometry(256, 256, np.linspace(0, np.pi, 128, False), 3 / 256) phantom.generate_projections(prefix + 'proj_par' + suffix, geom) projs = foam_ct_phantom.load_projections(prefix + 'proj_par' + suffix) print(len(projs), "projections, shape", projs.shape) pl.imshow(projs[0]) pl.savefig("projected.png") pl.imshow(projs[3]) pl.savefig("projected3.png")