def stir_projector_from_file(volume_file, projection_file): """Create a STIR projector from given template files. Parameters ---------- volume_file : `str` Full file path to the STIR input file containing information on the volume. This is usually a '.hv' file. For STIR reasons, a '.v' file is also needed. projection_file : `str` Full file path to the STIR input file with information on the projection data. This is usually a '.hs' file. For STIR reasons, a '.s' file is also needed. Returns ------- projector : `ForwardProjectorByBinWrapper` A STIR forward projector. """ volume = stir.FloatVoxelsOnCartesianGrid.read_from_file(volume_file) proj_data_in = stir.ProjData.read_from_file(projection_file) proj_data = stir.ProjDataInMemory(proj_data_in.get_exam_info(), proj_data_in.get_proj_data_info()) origin = volume.get_origin() grid_spacing = volume.get_grid_spacing() grid_shape = [ volume.get_z_size(), volume.get_y_size(), volume.get_x_size() ] min_corner = [origin[1], origin[2], origin[3]] max_corner = [ origin[1] + grid_spacing[1] * grid_shape[0], origin[2] + grid_spacing[2] * grid_shape[1], origin[3] + grid_spacing[3] * grid_shape[2] ] # reverse to handle STIR bug? See: # https://github.com/UCL/STIR/issues/7 recon_sp = uniform_discr(min_corner, max_corner, grid_shape, dtype='float32') # TODO: set correct projection space. Currently, a default grid with # stride (1, 1, 1) is used. proj_shape = proj_data.to_array().shape() data_sp = uniform_discr([0, 0, 0], proj_shape, proj_shape, dtype='float32') return ForwardProjectorByBinWrapper(recon_sp, data_sp, volume, proj_data)
def _projector_from_file(volume_file, projection_file): """Create a STIR projector from given template files. Old implementation, for test purposes only. """ volume = stir.FloatVoxelsOnCartesianGrid.read_from_file(volume_file) recon_sp = space_from_stir_domain(volume) proj_data_in = stir.ProjData.read_from_file(projection_file) proj_data = stir.ProjDataInMemory(proj_data_in.get_exam_info(), proj_data_in.get_proj_data_info()) data_sp = get_range_from_proj_data(proj_data) return ForwardProjectorByBinWrapper(recon_sp, data_sp, volume, proj_data)
`STIR <http://stir.sourceforge.net/>`_ and its Python bindings. """ from os import path import stir import odl # Load STIR input files with data base = path.join(path.dirname(path.abspath(__file__)), 'data', 'stir') volume_file = str(path.join(base, 'initial.hv')) volume = stir.FloatVoxelsOnCartesianGrid.read_from_file(volume_file) projection_file = str(path.join(base, 'small.hs')) proj_data_in = stir.ProjData.read_from_file(projection_file) proj_data = stir.ProjDataInMemory(proj_data_in.get_exam_info(), proj_data_in.get_proj_data_info()) # Create ODL spaces matching the discretization as specified in the # interfiles. recon_sp = odl.uniform_discr([0, 0, 0], [1, 1, 1], (15, 64, 64)) data_sp = odl.uniform_discr([0, 0, 0], [1, 1, 1], (37, 28, 56)) # Make STIR projector proj = odl.tomo.backends.stir_bindings.ForwardProjectorByBinWrapper( recon_sp, data_sp, volume, proj_data) # Create Shepp-Logan phantom vol = odl.phantom.shepp_logan(proj.domain, modified=True) # Project and show result = proj(vol)
fillStirSpace(originalImageS, originalImageP) # Initialize the projection matrix (using ray-tracing) # Het motion model doet nu niets, maar is nodig omdat Stir anders flipt MotionModel = stir.MotionModel() MotionModel.setOffset(0.0) projmatrix = stir.ProjMatrixByBinUsingRayTracing(MotionModel) projmatrix.set_num_tangential_LORs(nLOR) projmatrix.set_up(projdata_info, originalImageS) # Create projectors forwardprojector = stir.ForwardProjectorByBinUsingProjMatrixByBin(projmatrix) backprojector = stir.BackProjectorByBinUsingProjMatrixByBin(projmatrix) # Creating an instance for the sinogram (measurement), it is not yet filled measurement = stir.ProjDataInMemory(stir.ExamInfo(), projdata_info) # Forward project originalImageS and store in measurement forwardprojector.forward_project(measurement, originalImageS) # Converting the stir sinogram to a numpy sinogram measurementS = measurement.get_segment_by_sinogram(0) measurementP = stirextra.to_numpy(measurementS) #plt.figure(2), plt.title('Sinogram original image'), plt.imshow(measurementP[0,:,:]), plt.show() # Backprojecting the sinogram to get an image finalImageS = stir.FloatVoxelsOnCartesianGrid( projdata_info, 1, stir.FloatCartesianCoordinate3D(stir.make_FloatCoordinate(0, 0, 0)), stir.IntCartesianCoordinate3D(
image_data = stir.FloatVoxelsOnCartesianGrid(proj_data_info, zoom) #%% initialise a projection matrix # Using ray-tracing here # Note that the default is to restrict the projection to a cylindrical FOV projmatrix = stir.ProjMatrixByBinUsingRayTracing() projmatrix.set_up(proj_data_info, image_data) #%% construct projectors forwardprojector = stir.ForwardProjectorByBinUsingProjMatrixByBin(projmatrix) forwardprojector.set_up(proj_data_info, image_data) backprojector = stir.BackProjectorByBinUsingProjMatrixByBin(projmatrix) backprojector.set_up(proj_data_info, image_data) #%% create projection data for output of forward projection # We'll create the data in memory here exam_info = stir.ExamInfo() projdataout = stir.ProjDataInMemory(exam_info, proj_data_info) # Note: we could write to file, but it is right now a bit complicated to open a # projection data file for read/write: # inout=stir.ios.trunc|stir.ios.ios_base_in|stir.ios.out; # projdataout=stir.ProjDataInterfile(exam_info, proj_data_info, 'my_test_python_projection.hs',inout); #%% Done creating data and projectors! # Let's now create an interesting image with 2 cylinders #%% create a first cylinder (note: units are in mm) # we'll put it in the middle of the scanner # This is currently a bit difficult in STIR due to its # choice of origin (the middle of the first ring). length = 60 radius = 40 middle_slice = (image_data.get_max_z() + image_data.get_min_z()) / 2
#%% Create an empty image with suitable voxel sizes # use smaller voxels than the default zoom = 2. target = stir.FloatVoxelsOnCartesianGrid(projdata.get_proj_data_info(), zoom) #%% initialise the projection matrix # Using ray-tracing here # Note that the default is to restrict the projection to a cylindrical FOV projmatrix = stir.ProjMatrixByBinUsingRayTracing() projmatrix.set_up(projdata.get_proj_data_info(), target) #%% construct projectors forwardprojector = stir.ForwardProjectorByBinUsingProjMatrixByBin(projmatrix) backprojector = stir.BackProjectorByBinUsingProjMatrixByBin(projmatrix) #%% create projection data for output of forward projection # We'll just create the data in memory here projdataout = stir.ProjDataInMemory(projdata.get_exam_info(), projdata.get_proj_data_info()) # Note: we could write to file, but it is right now a bit complicated to open a # file for read/write: # inout=stir.ios.trunc|stir.ios.ios_base_in|stir.ios.out; # projdataout=stir.ProjDataInterfile(projdata.get_exam_info(), projdata.get_proj_data_info(), 'my_test_python_projection.hs',inout); #%% forward project an image. Here just some uniform data target.fill(2) forwardprojector.set_up(projdataout.get_proj_data_info(), target) forwardprojector.forward_project(projdataout, target) #%% display seg = projdataout.get_segment_by_sinogram(0) seg_array = stirextra.to_numpy(seg) pylab.figure() pylab.subplot(1, 2, 1) pylab.imshow(seg_array[10, :, :]) pylab.title('Forward projection')
#_______________________PROJ MATRIX AND PROJECTORS______________________ slope = 0.0 offSet = 0.0 MotionModel = stir.MotionModel(nFrames, slope, offSet) # A motion model is compulsory projmatrix = stir.ProjMatrixByBinUsingRayTracing(MotionModel) projmatrix.set_num_tangential_LORs(nLOR) projmatrix.set_up(projdata_info, originalImageS) # Create projectors forwardprojector = stir.ForwardProjectorByBinUsingProjMatrixByBin(projmatrix) backprojector = stir.BackProjectorByBinUsingProjMatrixByBin(projmatrix) #_________________________MEASUREMENT_______________________________ measurement = stir.ProjDataInMemory(stir.ExamInfo(), projdata_info) measurementListP = [] ## First time frame MotionModel.setOffset(0.0) forwardprojector.forward_project(measurement, phantomS[0]) measurement.write_to_file('sinoMeas_1.hs') measurementS = measurement.get_segment_by_sinogram(0) measurementP = stirextra.to_numpy(measurementS) measurementListP.append(measurementP) ## Second time frame MotionModel.setOffset(0.0) # Beweging zit al in het plaatje forwardprojector.forward_project(measurement, phantomS[1]) measurement.write_to_file('sinoMeas_2.hs') measurementS = measurement.get_segment_by_sinogram(0)
#allocate imagespace guestimates guestimatespace = stir.FloatVoxelsOnCartesianGrid( projdata_info, 1, stir.FloatCartesianCoordinate3D(stir.make_FloatCoordinate(0, 0, 0)), stir.IntCartesianCoordinate3D( stir.make_IntCoordinate( np.shape(p)[0], np.shape(p)[1], np.shape(p)[2]))) guestimatespace.fill(1) reconList.append(guestimatespace) # We'll just create the data in memory here measurementList.append( stir.ProjDataInMemory(stir.ExamInfo(), projdata_info)) #forward project last measurement in list forwardprojector.forward_project(measurementList[-1], phantomList[-1]) tmp = measurementList[-1].get_segment_by_sinogram(0) #pyvpx.numpy2vpx(stirextra.to_numpy(tmp), ('MeasuredSinoFrame' + str(iFrame) + '.vpx')) #append empty sinogram for estimations sinogramList.append(stir.ProjDataInMemory(stir.ExamInfo(), projdata_info)) SurrogateSignal = np.array(range(nFrames)) #Normalize NormSpace = stir.FloatVoxelsOnCartesianGrid( projdata_info, 1, stir.FloatCartesianCoordinate3D(stir.make_FloatCoordinate(0, 0, 0)),
#projdata_info = stir.ProjDataInfo.ProjDataInfoCTI(scanner, span, max_ring_diff, 168, scanner.get_max_num_non_arccorrected_bins(), False) projdata_info2D = stir.ProjDataInfo.ProjDataInfoCTI(scanner, span, max_ring_diff, 168, scanner.get_max_num_non_arccorrected_bins(), False) guessVolume = stir.FloatVoxelsOnCartesianGrid(projdata_info2D, 1, stir.FloatCartesianCoordinate3D(stir.make_FloatCoordinate(0,0,0)), stir.IntCartesianCoordinate3D(stir.make_IntCoordinate(sizeOfVolumeInVoxels[0], sizeOfVolumeInVoxels[1], sizeOfVolumeInVoxels[2]))) guessVolume.fill(1) ErrorVolume = stir.FloatVoxelsOnCartesianGrid(projdata_info2D, 1, stir.FloatCartesianCoordinate3D(stir.make_FloatCoordinate(0,0,0)), stir.IntCartesianCoordinate3D(stir.make_IntCoordinate(sizeOfVolumeInVoxels[0], sizeOfVolumeInVoxels[1], sizeOfVolumeInVoxels[2]))) ErrorVolume.fill(0) forwardSino2D = stir.ProjDataInMemory(stir.ExamInfo(), projdata_info2D) forwardSino2D.fill(0) #Initialize the projection matrix (using ray-tracing) projmatrix2D = stir.ProjMatrixByBinUsingRayTracing() nLOR = 10 projmatrix2D.set_num_tangential_LORs(nLOR) projmatrix2D.set_up(projdata_info2D, guessVolume) #Create projectors forwardprojector2D = stir.ForwardProjectorByBinUsingProjMatrixByBin(projmatrix2D) backprojector2D = stir.BackProjectorByBinUsingProjMatrixByBin(projmatrix2D) def forwardProject(npSource): guessVolume.fill(npSource.flat) forwardSino2D.fill(0)
def test_shape(): s = stir.Scanner.get_scanner_from_name("ECAT 962") projdatainfo = stir.ProjDataInfo.ProjDataInfoCTI(s, 3, 9, 8, 6) projdata = stir.ProjDataInMemory(stir.ExamInfo(), projdatainfo) shape = get_shape_from_proj_data(projdata) assert shape == projdata.to_array().shape()