def backproj(self, sino_data, filt=False): if filt is True: bid, rec = astra.create_backprojection(self.filter_projections(sino_data), self.proj_id)#,useCUDA=True) #last keyword for astra 1.1 else: bid, rec = astra.create_backprojection(sino_data, self.proj_id)#,useCUDA=True) #last keyword for astra 1.1 astra.data2d.delete(bid) return rec
def backprojOS(self, sinogram, no_os): """Applying backprojection for a specific subset""" rec_id, image = astra.create_backprojection(sinogram, self.proj_id_OS[no_os]) astra.data2d.delete(rec_id) astra.data2d.delete(self.proj_id_OS[no_os]) return image
def bp0(sino, det_col=111, num_angles=222, voxel_size_mm=1): """Wrapper for astra forward projector :param sino: :param projector_id: :return: backprojected sinogram """ vol_geom = astra.create_vol_geom(sino.shape) proj_id = astra.create_projector( 'cuda', astra.create_proj_geom('parallel', 1.0, det_col, np.linspace(0, np.pi, num_angles, False)), vol_geom) rec_id, backprojection = astra.create_backprojection(sino * voxel_size_mm, proj_id) # rec_id, backprojection = astra.create_backprojection(sino, projector_id) # backprojection /= sino.shape[0] # backprojection *= np.pi astra.data2d.delete(rec_id) astra.projector.delete(proj_id) return backprojection
def bp0(sino, det_col=111, num_angles=222, voxel_size_mm=1): """Wrapper for astra forward projector :param sino: :param projector_id: :return: backprojected sinogram """ vol_geom = astra.create_vol_geom(sino.shape) proj_id = astra.create_projector( 'cuda', astra.create_proj_geom('parallel', 1.0, det_col, np.linspace(0, np.pi, num_angles, False)), vol_geom) rec_id, backprojection = astra.create_backprojection( sino * voxel_size_mm, proj_id) # rec_id, backprojection = astra.create_backprojection(sino, projector_id) # backprojection /= sino.shape[0] # backprojection *= np.pi astra.data2d.delete(rec_id) astra.projector.delete(proj_id) return backprojection
def rmatvec(self, v): bid, b = astra.create_backprojection( np.reshape(v, ( len(proj_geom['ProjectionAngles']), proj_geom['DetectorCount'], )), self.proj_id) astra.data2d.delete(bid) return b.ravel()
def backward(self, x): Y = np.empty((x.shape[0], x.shape[1], x.shape[1]), dtype=np.float32) for i in range(x.shape[0]): pid, Y[i] = astra.create_backprojection(x[i], self.proj_id) self.data2d.append(pid) return Y
def backward(self, x): device = x.device x = x.cpu().numpy() Y = np.empty((x.shape[0], x.shape[1], x.shape[1]), dtype=np.float32) for i in range(x.shape[0]): _, Y[i] = astra.create_backprojection(x[i], self.proj_id) return torch.from_numpy(Y).to(device)
def test_fanbeam_error(device, batch_size, image_size, angles, spacing, distances, det_count, clip_to_circle): # generate random images # generate random images det_count = int(det_count * image_size) mask_radius = det_count / 2.0 if clip_to_circle else -1 x = generate_random_images(1, image_size, mask_radius)[0] s_dist, d_dist = distances s_dist *= image_size d_dist *= image_size # astra vol_geom = astra.create_vol_geom(x.shape[0], x.shape[1]) proj_geom = astra.create_proj_geom('fanflat', spacing, det_count, angles, s_dist, d_dist) proj_id = astra.create_projector('cuda', proj_geom, vol_geom) id, astra_y = astra.create_sino(x, proj_id) _, astra_bp = astra.create_backprojection(astra_y, proj_id) if clip_to_circle: astra_bp *= circle_mask(image_size, mask_radius) # TODO clean astra structures # our implementation radon = RadonFanbeam(image_size, angles, s_dist, d_dist, det_count=det_count, det_spacing=spacing, clip_to_circle=clip_to_circle) x = torch.FloatTensor(x).to(device).view(1, x.shape[0], x.shape[1]) # repeat data to fill batch size x = torch.cat([x] * batch_size, dim=0) our_fp = radon.forward(x) our_bp = radon.backprojection(our_fp) forward_error = relative_error(astra_y, our_fp[0].cpu().numpy()) back_error = relative_error(astra_bp, our_bp[0].cpu().numpy()) # if back_error > 5e-3: # plt.imshow(astra_bp) # plt.figure() # plt.imshow(our_bp[0].cpu().numpy()) # plt.show() print(np.max(our_fp.cpu().numpy()), np.max(our_bp.cpu().numpy())) print( f"batch: {batch_size}, size: {image_size}, angles: {len(angles)}, spacing: {spacing}, distances: {distances} circle: {clip_to_circle}, forward: {forward_error}, back: {back_error}" ) # TODO better checks assert_less(forward_error, 1e-2) assert_less(back_error, 5e-3)
def process(self): DATA = self.get_input() IM = ImageData(geometry=self.volume_geometry) rec_id, IM.array = astra.create_backprojection(DATA.as_array(), self.proj_id) astra.data2d.delete(rec_id) if self.device == 'cpu': return IM else: scaling = self.volume_geometry.voxel_size_x**3 return scaling * IM
def bp(sino, projector_id): """Wrapper for astra forward projector :param sino: :param projector_id: :return: backprojected sinogram """ rec_id, backprojection = astra.create_backprojection(sino * voxel_size_mm, projector_id) backprojection /= sino.shape[0] # backprojection *= np.pi astra.data2d.delete(rec_id) return backprojection
def process(self, out=None): DATA = self.get_input() if out is None: IM = self.volume_geometry.allocate(None) else: IM = out for k in range(IM.geometry.channels): data_temp = DATA.as_array()[k] rec_id, IM.as_array()[k] = astra.create_backprojection( data_temp, self.proj_id) astra.data2d.delete(rec_id) if out is None: return IM
def process(self, out=None): DATA = self.get_input() data_temp = DATA.as_array() rec_id, arr_out = astra.create_backprojection(data_temp, self.proj_id) astra.data2d.delete(rec_id) if out is None: out = ImageData(arr_out, deep_copy=False, geometry=self.volume_geometry.copy(), suppress_warning=True) return out else: out.fill(arr_out)
def bp(sino, projector_id): """Wrapper for astra forward projector :param sino: :param projector_id: :return: backprojected sinogram """ rec_id, backprojection = astra.create_backprojection( sino * voxel_size_mm, projector_id) backprojection /= sino.shape[0] # backprojection *= np.pi astra.data2d.delete(rec_id) return backprojection
def iradon(g, theta, n): ''' Takes a numpy.ndarray g that was created with theta angles and applies the inverse radon transform with no filters to it. Returns the backprojected data. ''' vol_geom = astra.create_vol_geom(n) proj_geom = astra.create_proj_geom('parallel', 1.0, 367, theta) proj_id = astra.create_projector('line', proj_geom, vol_geom) f_id = astra.data2d.create('-vol', vol_geom) g_id = astra.data2d.create('-sino', proj_geom, g) rec_id, rec_data = astra.create_backprojection(g, proj_id) astra.data2d.clear() astra.projector.clear() return rec_data
def process(self, out=None): DATA = self.get_input() IM = ImageData(geometry=self.volume_geometry) for k in range(IM.geometry.channels): rec_id, IM.as_array()[k] = astra.create_backprojection( DATA.as_array()[k], self.proj_id) astra.data2d.delete(rec_id) if self.device == 'cpu': ret = IM else: scaling = self.volume_geometry.voxel_size_x**3 ret = scaling*IM if out is None: return ret else: out.fill(ret)
def backproj(self, sinogram): """Applying backprojection""" rec_id, image = astra.create_backprojection(sinogram, self.proj_id) astra.data2d.delete(self.proj_id) astra.data2d.delete(rec_id) return image
def rmatvec(self, v): bid, b = astra.create_backprojection( np.reshape(v, (len(proj_geom['ProjectionAngles']), proj_geom[ 'DetectorCount'],)), self.proj_id) # ,useCUDA=True) astra.data2d.delete(bid) return b.flatten()
det_width = 2.0 # astra # astra = AstraWrapper(angles) # # astra_fp_id, astra_y = astra.forward(x, det_width) # astra_bp = astra.backproject(astra_fp_id, res, 1) vol_geom = astra.create_vol_geom(x.shape[0], x.shape[1]) proj_geom = astra.create_proj_geom('fanflat', det_width, x.shape[0], angles, source_distance, det_distance) #proj_geom = astra.create_proj_geom('parallel', det_width, x.shape[0], angles) proj_id = astra.create_projector('cuda', proj_geom, vol_geom) id, astra_y = astra.create_sino(x, proj_id) _, astra_bp = astra.create_backprojection(astra_y, proj_id) plt.imshow(astra_bp) # # # # plt.figure() # # # projection = FanBeamProjection(x.shape[0], source_distance, det_distance, det_width) # # # projection = ParallelBeamProjection(x.shape[0]) radon = RadonFanbeam(x.shape[-1], angles, source_distance, det_distance, det_spacing=det_width) print(source_distance + det_distance) # radon.rays = fan_beam_rays(x.shape[0], source_distance, det_distance, det_width, clip_to_circle=True) # print(radon.rays.size())