def initialize(self): """Initialize the ASTRA projectors.""" if not self.is_initialized: if self.is_3d: projector_type = "cuda3d" else: if has_cuda: projector_type = "cuda" else: projector_type = "linear" opts = { "VoxelSuperSampling": self.super_sampling, "DetectorSuperSampling": self.super_sampling } if self.has_individual_projs: self.proj_id = [ astra.create_projector(projector_type, pg, self.vol_geom, opts) for pg in self.proj_geom_ind ] self.W_ind = [astra.OpTomo(p_id) for p_id in self.proj_id] self.proj_id.append( astra.create_projector(projector_type, self.proj_geom_all, self.vol_geom, opts)) self.W_all = astra.OpTomo(self.proj_id[-1]) super().initialize()
def initialize(self, cfg, Relaxation=1, MinConstraint=None, MaxConstraint=None): self.W = astra.OpTomo(cfg['ProjectorId']) self.vid = cfg['ReconstructionDataId'] self.sid = cfg['ProjectionDataId'] self.min_constraint = MinConstraint self.max_constraint = MaxConstraint try: v = astra.data2d.get_shared(self.vid) s = astra.data2d.get_shared(self.sid) self.data_mod = astra.data2d except Exception: v = astra.data3d.get_shared(self.vid) s = astra.data3d.get_shared(self.sid) self.data_mod = astra.data3d self.R = self.W * np.ones(v.shape, dtype=np.float32).ravel() self.R[self.R < 0.000001] = np.Inf self.R = 1 / self.R self.R = self.R.reshape(s.shape) self.mrC = self.W.T * np.ones(s.shape, dtype=np.float32).ravel() self.mrC[self.mrC < 0.000001] = np.Inf self.mrC = -Relaxation / self.mrC self.mrC = self.mrC.reshape(v.shape)
def initialize(self, cfg, liptschitz=1, MinConstraint=None, MaxConstraint=None): self.W = astra.OpTomo(cfg['ProjectorId']) self.vid = cfg['ReconstructionDataId'] self.sid = cfg['ProjectionDataId'] self.min_constraint = MinConstraint self.max_constraint = MaxConstraint try: v = astra.data2d.get_shared(self.vid) s = astra.data2d.get_shared(self.sid) self.data_mod = astra.data2d except Exception: v = astra.data3d.get_shared(self.vid) s = astra.data3d.get_shared(self.sid) self.data_mod = astra.data3d self.liptschitz = self.power_iteration(self.W, 10) self.nu = 1 / self.liptschitz self.ATy = self.W.BP(s) self.obj_func = None print('plugin initialized.', flush=True)
def recon_sirt_fbp(im, angles, iter, temppath): """Reconstruct a sinogram with the SIRT-FBP algorithm (Pelt, 2015). Parameters ---------- im : array_like Sinogram image data as numpy array. iter : int Number of iterations to be used for the computation of SIRT filter. angles : double Value in radians representing the number of angles of the input sinogram. """ # Create ASTRA geometries: vol_geom = astra.create_vol_geom(im.shape[1], im.shape[1]) proj_geom = astra.create_proj_geom('parallel', 1.0, im.shape[1], linspace(0, angles, im.shape[0], False)) proj = astra.create_projector('cuda', proj_geom, vol_geom) p = astra.OpTomo(proj) # Register plugin with ASTRA astra.plugin.register(sirtfbp.plugin) # Create the ASTRA projector im_rec = p.reconstruct('SIRT-FBP', im, iter, extraOptions={'filter_dir': temppath}) return im_rec.astype(float32)
def __init__(self, DetectorsDim, AnglesVec, CenterRotOffset, ObjSize, device): self.DetectorsDim = DetectorsDim self.AnglesVec = AnglesVec self.ObjSize = ObjSize if CenterRotOffset is None: 'scalar geometry since parallel_vec is not implemented for CPU ASTRA modules yet?' self.proj_geom = astra.create_proj_geom('parallel', 1.0, DetectorsDim, AnglesVec) else: # define astra vector geometry (default) vectors = vec_geom_init2D(AnglesVec, 1.0, CenterRotOffset) self.proj_geom = astra.create_proj_geom('parallel_vec', DetectorsDim, vectors) self.vol_geom = astra.create_vol_geom(ObjSize, ObjSize) if device == 'cpu': self.proj_id = astra.create_projector('line', self.proj_geom, self.vol_geom) # for CPU self.device = 1 elif device == 'gpu': self.proj_id = astra.create_projector('cuda', self.proj_geom, self.vol_geom) # for GPU self.device = 0 else: print("Select between 'cpu' or 'gpu' for device") # add optomo operator self.A_optomo = astra.OpTomo(self.proj_id)
def calculate_error(proj_id, img, data): W = astra.OpTomo(proj_id) sino = W * img sino = sino.reshape(data.shape) - data tv = norm1(gradient(sino)) l2 = norm2sq(sino) return tv, l2
def DT(W,p, vol_size, proj_size, angles, Ngv, lamb=10, PU = 'cuda', K = 4, Niter = 50, epsilon=1e-4): [Nx,Nz] = vol_size [Ndetx, Ndety] = proj_size Nan = len(angles) if Ndety==1: sinogram = p.reshape([Nan, Ndetx]) else: sinogram = p.reshape([Nan, Ndetx, Ndety]) # create projection geometry and operator print('TVR-DART ...') print('Create projection geometry and operator...') proj_geom = astra.create_proj_geom('parallel', 1.0, Ndetx, angles) vol_geom = astra.create_vol_geom(Nz,Nx) proj_id = astra.create_projector(PU,proj_geom,vol_geom) W = astra.OpTomo(proj_id) # initial reconstruction print('Initial reconstruction...') recsirt = SIRT.recon(sinogram, 200, proj_geom, vol_geom, PU) sf = np.max(recsirt) p = p/sf if Ndety==1: sinogram = p.reshape([Nan, Ndetx]) else: sinogram = p.reshape([Nan, Ndetx, Ndety]) recsirt = recsirt/sf # set initial TVR-DART parameters K = K*np.ones(Ngv-1) gv = np.linspace(0, 1, Ngv,True) param0 = gv2param(gv,K) # Esimating parameter using a small section of the dataset print('Estimation of optimal parameters...') if Ndety==1: Segrec,param_esti = joint(W,p, recsirt, param0 ,lamb) else: Elist = np.sum(recsirt,axis=(0,2)) index = np.argmax(Elist) index1 = np.max(np.hstack((0,index-1))) index2 = np.min(np.hstack((index+1,Ndety-1))) x0_esti = recsirt[:,index1:index2+1,:] sinogram_esti = sinogram[:,:,index1:index2+1] p_esti = sinogram_esti.reshape(Nan*Ndetx*(index2-index1+1)) Segrec,param_esti = joint(W,p_esti, x0_esti, param0 ,lamb) # Reconstruction with estimated parameter print('Reconstruction with estimated parameters...') Segrec,rec = recon(W,p, recsirt, param_esti, lamb, Niter, epsilon) [gv,K] = param2gv(param_esti) param_esti = gv2param(gv*sf,K) Segrec = Segrec*sf; return Segrec, param_esti;
def initialize(self, cfg, hqrecfiles, z_id, traindir, nlinear=2, npick=100, extra_ids=None, angle_dependent=False): if extra_ids == None: extra_ids = [] self.extra_s = [astra.data2d.get_shared(i) for i in extra_ids] self.W = astra.OpTomo(cfg['ProjectorId']) self.npick = npick self.td = traindir mkdir_p(traindir) self.rec = tifffile.imread(sorted(glob.glob(hqrecfiles))[z_id]) self.bck = (self.W.T * np.ones_like(self.s) < self.s.shape[0] - 0.5).reshape(self.v.shape) self.outfn = traindir + os.sep + "train_{:05d}.mat".format(z_id) self.z_id = z_id fs = self.s.shape[1] if fs % 2 == 0: fs += 1 mf = int(fs / 2) w = 1 c = mf bas = np.zeros(fs, dtype=np.float32) self.basis = [] count = 0 while c < fs: bas[:] = 0 l = c r = c + w if r > fs: r = fs bas[l:r] = 1 if l != 0: l = fs - c - w r = l + w if l < 0: l = 0 bas[l:r] = 1 self.basis.append(bas.copy()) c += w count += 1 if count > nlinear: w = 2 * w if angle_dependent == "True": basis_ang = [] bas_ang = np.zeros((self.s.shape[0], fs), dtype=np.float32) for bas in self.basis: for i in range(self.s.shape[0]): bas_ang[i] = bas basis_ang.append(bas_ang.copy()) bas_ang[i][:] = 0 self.basis = basis_ang self.nf = len(self.basis)
def Algorithm_TV_regularized(self, beam_geometry, lam, lower_bound, upper_bound, projector_type='cuda', num_inner_iter=100, num_main_iter=100, recon_dimension=None, source_origin_cm=None, detector_origin_cm=None, print_progress=True): ''' Inner iterations: --- Main iterations: --- ''' sinogram = self.sino_pad_roi source_origin_cm = source_origin_cm detector_origin_cm = detector_origin_cm proj_id = self.create_projector_id(sinogram, beam_geometry, projector_type, recon_dimension, source_origin_cm, detector_origin_cm) p = astra.OpTomo(proj_id) f = tvtomo.FISTA(p, lam, num_inner_iter, bmin=lower_bound, bmax=upper_bound) start = time.time() rec_tv = f.reconstruct(self.sino_pad_roi, num_main_iter, progress=print_progress) end = time.time() print('TV-FISTA: ' + beam_geometry + ', ' + projector_type + ', ' + str(num_main_iter) + ' iterations, ' + 'elapsed time: ' + str(end - start) + ' seconds') # Extract the horizontal pixel profile from centerline region TV_cl_h_profile = rec_tv[int(rec_tv.shape[0] / 2), :] # Plot reconstructed image plt.figure() plt.imshow(rec_tv) plt.title('Reconstruction: TV FISTA ' + beam_geometry) plt.colorbar() plt.gray() return rec_tv, TV_cl_h_profile
def initialize(self, cfg, nlinear=2, reg_wav=None, wav_bas='haar', reg_grad=None, save_filter=None, use_saved_filter=None, reg_path=None, reg_range=(1, 100, 10)): self.W = astra.OpTomo(cfg['ProjectorId']) self.reg_gr = reg_grad self.reg_wav = reg_wav self.save_filter = save_filter self.use_saved = use_saved_filter self.reg_path = reg_path self.reg_range = reg_range self.wav_bas = wav_bas if not self.use_saved: self.bck = (self.W.T * np.ones_like(self.s) < self.s.shape[0] - 0.5).reshape(self.v.shape) if self.reg_path: self.reg_gr = 1. fs = self.s.shape[1] if fs % 2 == 0: fs += 1 mf = int(fs / 2) w = 1 c = mf bas = np.zeros(fs, dtype=np.float32) self.basis = [] count = 0 while c < fs: bas[:] = 0 l = c r = c + w if r > fs: r = fs bas[l:r] = 1 if l != 0: l = fs - c - w r = l + w if l < 0: l = 0 bas[l:r] = 1 self.basis.append(bas.copy()) c += w count += 1 if count > nlinear: w = 2 * w self.nf = len(self.basis)
def initialize(self, cfg): self.W = astra.OpTomo(cfg['ProjectorId']) self.vid = cfg['ReconstructionDataId'] self.sid = cfg['ProjectionDataId'] try: v = astra.data2d.get_shared(self.vid) s = astra.data2d.get_shared(self.sid) self.data_mod = astra.data2d except Exception: v = astra.data3d.get_shared(self.vid) s = astra.data3d.get_shared(self.sid) self.data_mod = astra.data3d
def initialize(self, cfg, tv_reg, fgp_iters=100, bmin=-np.inf, bmax=np.inf, print_progress=False, fgp_nthreads=None): self.w = astra.OpTomo(self.pid) self.tv = tv_reg self.fgp_iters = fgp_iters self.pr = print_progress self.bmin = bmin self.bmax = bmax self.nthreads = fgp_nthreads
def __init__(self, DetectorsDim, AnglesVec, ObjSize, device): self.DetectorsDim = DetectorsDim self.AnglesVec = AnglesVec self.ObjSize = ObjSize self.proj_geom = astra.create_proj_geom('parallel', 1.0, DetectorsDim, AnglesVec) self.vol_geom = astra.create_vol_geom(ObjSize, ObjSize) if device == 'cpu': self.proj_id = astra.create_projector('line', self.proj_geom, self.vol_geom) # for CPU self.device = 1 elif device == 'gpu': self.proj_id = astra.create_projector('cuda', self.proj_geom, self.vol_geom) # for GPU self.device = 0 else: print ("Select between 'cpu' or 'gpu' for device") # add optomo operator self.A_optomo = astra.OpTomo(self.proj_id)
def __init__(self, DetColumnCount, DetRowCount, AnglesVec, CenterRotOffset, ObjSize): self.ObjSize = ObjSize self.DetectorsDimV = DetRowCount # define astra type geometry (scalar) # self.proj_geom = astra.create_proj_geom('parallel3d', 1.0, 1.0, DetRowCount, DetColumnCount, AnglesVec) # define astra type geometry (vector) vectors = vec_geom_init(AnglesVec, 1.0, 1.0, CenterRotOffset) self.proj_geom = astra.create_proj_geom('parallel3d_vec', DetRowCount, DetColumnCount, vectors) if type(ObjSize) == tuple: Y,X,Z = [int(i) for i in ObjSize] else: Y=X=ObjSize Z=DetRowCount self.vol_geom = astra.create_vol_geom(Y,X,Z) self.proj_id = astra.create_projector('cuda3d', self.proj_geom, self.vol_geom) # for GPU self.A_optomo = astra.OpTomo(self.proj_id)
def generateAmatrix(proj_params, miscalib, vol_params, gpu_idx): """ Generate the A matrix using spot operators Inputs: proj_params: Dictionary of the projection related parameters miscalib: Dictionary of miscalibration paramaters such as center of offset rotation and/or tilt vol_params: Dictionary of reconstruction volume parameters gpu_idx: List of gpus to use """ if (proj_params['type'] == 'par'): proj_geom, vol_geom = createGeomPar(proj_params, miscalib, vol_params, gpu_idx) elif (proj_params['type'] == 'cone'): proj_geom, vol_geom = createGeomCone(proj_params, miscalib, vol_params, gpu_idx) elif (proj_params['type'] == 'par_euler'): proj_geom, vol_geom = createGeomParEuler(proj_params, miscalib, vol_params, gpu_idx) else: print('Unrecognized type for projector') proj_id = astra.create_projector('cuda3d', proj_geom, vol_geom) #opTomo based Projection function A = astra.OpTomo(proj_id) return A
# ----------------------------------------------------------------------- import astra import numpy as np import scipy.sparse.linalg vol_geom = astra.create_vol_geom(256, 256) proj_geom = astra.create_proj_geom('parallel', 1.0, 384, np.linspace(0,np.pi,180,False)) # As before, create a sinogram from a phantom import scipy.io P = scipy.io.loadmat('phantom.mat')['phantom256'] proj_id = astra.create_projector('cuda',proj_geom,vol_geom) # construct the OpTomo object W = astra.OpTomo(proj_id) sinogram = W * P sinogram = sinogram.reshape([180, 384]) import pylab pylab.gray() pylab.figure(1) pylab.imshow(P) pylab.figure(2) pylab.imshow(sinogram) # Run the lsqr linear solver output = scipy.sparse.linalg.lsqr(W, sinogram.ravel(), iter_lim=150) rec = output[0].reshape([256, 256])
import pylab as pl pl.gray() # Run 03_generate_projections_parallel.py and 04_generate_projections_cone first projs = foam_ct_phantom.load_projections('test_projs_par.h5') vol_geom = foam_ct_phantom.VolumeGeometry(256, 256, 256, 3 / 256) proj_geom = foam_ct_phantom.ParallelGeometry.from_file('test_projs_par.h5') pg = proj_geom.to_astra(single_slice=True) vg = vol_geom.to_astra(single_slice=True) pid = astra.create_projector('cuda', pg, vg) w = astra.OpTomo(pid) mid_slice = w.reconstruct('FBP_CUDA', projs[:, projs.shape[1] // 2]) pl.imshow(mid_slice) pl.show() astra.projector.delete(pid) projs = foam_ct_phantom.load_projections('test_projs_cone.h5') proj_geom = foam_ct_phantom.ConeGeometry.from_file('test_projs_cone.h5', usecuda=False) pg3d = proj_geom.to_astra() vg3d = vol_geom.to_astra()
def initialize(self,cfg, Relaxation = 1): self.W = astra.OpTomo(cfg['ProjectorId']) self.vid = cfg['ReconstructionDataId'] self.sid = cfg['ProjectionDataId'] self.rel = Relaxation
# 0. parameter settings of fanflat geometry detector_num = img.shape[0] * 2 views = 540 detector_size = 1 source_origion = 800 origion_det = 200 # 1. creat geometries proj_geom = astra.create_proj_geom('fanflat', detector_size, detector_num, np.linspace(0, 2 * np.pi, views, False), source_origion, origion_det) vol_geom = astra.create_vol_geom(img.shape[0], img.shape[0]) # 2. use OpTomo to get W and then sinogram and image projector_id = astra.create_projector('cuda', proj_geom, vol_geom) W = astra.OpTomo(projector_id) print W.shape sinogram = W * img haha = W.T * sinogram haha = np.reshape(haha, (img.shape[0], img.shape[0])) sinogram = np.reshape(sinogram, (views, detector_num)) # sino = sinogram # plt.figure('phamtom'), plt.imshow(img, cmap=plt.cm.gray) # plt.figure('sinogram'), plt.imshow(sino, cmap=plt.cm.gray) # plt.figure('slice') # plt.plot(sino[50, :], label='50') # plt.plot(sino[100, :], label='100') # plt.plot(sino[150, :], label='150') # plt.plot(sino[200, :], label='200')
def initialize(self, cfg, its_PM, Lambda): self.W = astra.OpTomo(cfg['ProjectorId']) self.vid = cfg['ReconstructionDataId'] self.sid = cfg['ProjectionDataId'] self.its_PM = its_PM self.Lambda = Lambda
def initialize_projector(self): self.proj_id = astra.create_projector("linear", self.proj_geom, self.vol_geom) self.W = astra.OpTomo(self.proj_id)
def fp(im, ang): proj_geom = astra.create_proj_geom('parallel', 1.0, im.shape[0], np.array([ang,0])) vol_geom = astra.create_vol_geom(im.shape) w = astra.OpTomo('cuda',proj_geom,vol_geom) fpim = w*im return w[0:im.shape[0]]
def initialize(self, cfg, filter_file, nlinear=2, extra_ids=None, angle_dependent=False): if extra_ids == None: extra_ids = [] self.extra_s = [astra.data2d.get_shared(i) for i in extra_ids] self.W = astra.OpTomo(cfg['ProjectorId']) fs = self.s.shape[1] if fs % 2 == 0: fs += 1 mf = int(fs / 2) w = 1 c = mf bas = np.zeros(fs, dtype=np.float32) self.basis = [] count = 0 while c < fs: bas[:] = 0 l = c r = c + w if r > fs: r = fs bas[l:r] = 1 if l != 0: l = fs - c - w r = l + w if l < 0: l = 0 bas[l:r] = 1 self.basis.append(bas.copy()) c += w count += 1 if count > nlinear: w = 2 * w if angle_dependent == "True": basis_ang = [] bas_ang = np.zeros((self.s.shape[0], fs), dtype=np.float32) for bas in self.basis: for i in range(self.s.shape[0]): bas_ang[i] = bas basis_ang.append(bas_ang.copy()) bas_ang[i][:] = 0 self.basis = basis_ang self.nf = len(self.basis) fl = sio.loadmat(filter_file) self.l1 = fl['l1'] self.l2 = fl['l2'].transpose() minmax = fl['minmax'][0] minL = minmax[0] maxL = minmax[1] self.minIn = minmax[2] self.maxIn = minmax[3] mindivmax = minL / (maxL - minL) mindivmax[np.isnan(mindivmax)] = 0 mindivmax[np.isinf(mindivmax)] = 0 divmaxmin = 1. / (maxL - minL) divmaxmin[np.isnan(divmaxmin)] = 0 divmaxmin[np.isinf(divmaxmin)] = 0 nHid = self.l1.shape[1] nsl = len(extra_ids) + 1 dims = [ nHid, nsl, ] dims.extend(self.basis[0].shape) self.filters = np.empty(dims) self.offsets = np.empty(nHid) for i in range(nHid): wv = (2 * self.l1[0:self.l1.shape[0] - 1, i] * divmaxmin).transpose() self.filters[i] = np.zeros(dims[1:]) for t, bas in enumerate(self.basis): for l in range(nsl): self.filters[i, l] += wv[t + l * len(self.basis)] * bas self.offsets[i] = 2 * np.dot(self.l1[0:self.l1.shape[0] - 1, i], mindivmax.transpose()) + np.sum( self.l1[:, i])