Example #1
0
def example_cone_3d():
    # ------------------ Declare Parameters ------------------

    # Volume Parameters:
    volume_size = 256
    volume_shape = [volume_size, volume_size, volume_size]
    v_spacing = 0.25
    volume_spacing = [v_spacing, v_spacing, v_spacing]

    # Detector Parameters:
    detector_shape = [450, 450]
    d_spacing = 0.33
    detector_spacing = [d_spacing, d_spacing]

    # Trajectory Parameters:
    number_of_projections = 248
    angular_range = np.pi + 2 * np.arctan(detector_shape[0] / 2 / 1200)

    source_detector_distance = 1200
    source_isocenter_distance = 750

    # create Geometry class
    geometry = GeometryCone3D(volume_shape, volume_spacing, detector_shape,
                              detector_spacing, number_of_projections,
                              angular_range, source_detector_distance,
                              source_isocenter_distance)
    geometry.angular_range = np.radians(200)
    projection_geometry = circular_trajectory.circular_trajectory_3d(geometry)

    geometry.set_projection_matrices(projection_geometry)

    # Get Phantom 3d
    phantom = shepp_logan.shepp_logan_3d(volume_shape)

    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.5
    config.gpu_options.allow_growth = True
    # ------------------ Call Layers ------------------
    with tf.Session(config=config) as sess:
        sinogram = generate_sinogram.generate_sinogram(phantom,
                                                       cone_projection3d,
                                                       geometry)

        model = nn_model(geometry)
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        reco_tf, redundancy_weighted_sino_tf = model.model(sinogram)
        reco, redundancy_weighted_sino = sess.run(
            [reco_tf, redundancy_weighted_sino_tf])

    plt.figure()
    plt.imshow(reco[(int)(volume_shape[0] / 2), :, :],
               cmap=plt.get_cmap('gist_gray'),
               vmin=0,
               vmax=0.4)
    plt.axis('off')
    plt.savefig('fdk_reco.png',
                dpi=150,
                transparent=False,
                bbox_inches='tight')
Example #2
0
def example_cone_3d():
    # ------------------ Declare Parameters ------------------

    # Volume Parameters:
    volume_size = 256
    volume_shape = [volume_size, volume_size, volume_size]
    v_spacing = 0.25
    volume_spacing = [v_spacing, v_spacing, v_spacing]

    # Detector Parameters:
    detector_shape = [450, 450]
    d_spacing = 0.33
    detector_spacing = [d_spacing, d_spacing]

    # Trajectory Parameters:
    number_of_projections = 248
    angular_range = np.pi + 2 * np.arctan(detector_shape[0] / 2 / 1200)

    source_detector_distance = 1200
    source_isocenter_distance = 750

    # create Geometry class
    geometry = GeometryCone3D(volume_shape, volume_spacing, detector_shape,
                              detector_spacing, number_of_projections,
                              angular_range, source_detector_distance,
                              source_isocenter_distance)
    geometry.angular_range = np.radians(200)
    projection_geometry = circular_trajectory.circular_trajectory_3d(geometry)

    geometry.set_trajectory(projection_geometry)

    # Get Phantom 3d
    phantom = shepp_logan.shepp_logan_3d(volume_shape)
    phantom = np.expand_dims(phantom, axis=0)

    # gpus = tf.config.experimental.list_physical_devices('GPU')
    # if gpus:
    #     try:
    #         for gpu in gpus:
    #             tf.config.experimental.set_memory_growth(gpu, True)
    #     except RunetimeError as e:
    #         print(e)
    # ------------------ Call Layers ------------------

    sinogram = generate_sinogram.generate_sinogram(phantom, cone_projection3d,
                                                   geometry)

    model = nn_model(geometry)
    reco, redundancy_weighted_sino = model.model(sinogram)

    plt.figure()
    plt.imshow(np.squeeze(reco)[volume_shape[0] // 2],
               cmap=plt.get_cmap('gist_gray'),
               vmin=0,
               vmax=0.4)
    plt.axis('off')
    plt.savefig('fdk_reco.png',
                dpi=150,
                transparent=False,
                bbox_inches='tight')
Example #3
0
def example_cone_3d():
    # ------------------ Declare Parameters ------------------

    # Volume Parameters:
    volume_size = 256
    volume_shape = [volume_size, volume_size, volume_size]
    volume_spacing = [0.5, 0.5, 0.5]

    # Detector Parameters:
    detector_shape = [2*volume_size, 2*volume_size]
    detector_spacing = [1, 1]

    # Trajectory Parameters:
    number_of_projections = 360
    angular_range = 2 * np.pi

    source_detector_distance = 1200
    source_isocenter_distance = 750

    # create Geometry class
    geometry = GeometryCone3D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range, source_detector_distance, source_isocenter_distance)
    geometry.set_trajectory(circular_trajectory.circular_trajectory_3d(geometry))

    # Get Phantom 3d
    phantom = shepp_logan.shepp_logan_3d(volume_shape)
    # Add required batch dimension
    phantom = np.expand_dims(phantom, axis=0)

    # ------------------ Call Layers ------------------
    # The following code is the new TF2.0 experimental way to tell
    # Tensorflow only to allocate GPU memory needed rather then allocate every GPU memory available.
    # This is important for the use of the hardware interpolation projector, otherwise there might be not enough memory left
    # to allocate the texture memory on the GPU

    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
        try:
            for gpu in gpus:
                tf.config.experimental.set_memory_growth(gpu, True)
        except RunetimeError as e:
            print(e)

    sinogram = cone_projection3d(phantom, geometry)

    reco_filter = ram_lak_3D(geometry)
    sino_freq = tf.signal.fft(tf.cast(sinogram,dtype=tf.complex64))
    sino_filtered_freq = tf.multiply(sino_freq,tf.cast(reco_filter,dtype=tf.complex64))
    sinogram_filtered = tf.math.real(tf.signal.ifft(sino_filtered_freq))

    reco = cone_backprojection3d(sinogram_filtered, geometry)

    plt.figure()
    plt.imshow(np.squeeze(reco)[volume_shape[0]//2], cmap=plt.get_cmap('gist_gray'))
    plt.axis('off')
    plt.savefig('3d_cone_reco.png', dpi=150, transparent=False, bbox_inches='tight')
Example #4
0
def example_cone_3d():
    # ------------------ Declare Parameters ------------------

    # Volume Parameters:
    volume_size = 256
    volume_shape = [volume_size, volume_size, volume_size]
    volume_spacing = [0.5, 0.5, 0.5]

    # Detector Parameters:
    detector_shape = [2 * volume_size, 2 * volume_size]
    detector_spacing = [1, 1]

    # Trajectory Parameters:
    number_of_projections = 360
    angular_range = 2 * np.pi

    source_detector_distance = 1200
    source_isocenter_distance = 750

    # create Geometry class
    geometry = GeometryCone3D(volume_shape, volume_spacing, detector_shape,
                              detector_spacing, number_of_projections,
                              angular_range, source_detector_distance,
                              source_isocenter_distance)
    geometry.set_projection_matrices(
        circular_trajectory.circular_trajectory_3d(geometry))

    # Get Phantom 3d
    phantom = shepp_logan.shepp_logan_3d(volume_shape)
    phantom = np.expand_dims(phantom, axis=0)

    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.5
    config.gpu_options.allow_growth = True
    # ------------------ Call Layers ------------------
    with tf.Session(config=config) as sess:
        result = cone_projection3d(phantom, geometry)
        sinogram = result.eval()

        filter = ram_lak_3D(geometry)
        sino_freq = np.fft.fft(sinogram, axis=-1)
        filtered_sino_freq = sino_freq * filter
        filtered_sino = np.fft.ifft(filtered_sino_freq, axis=-1)

        result_back_proj = cone_backprojection3d(filtered_sino, geometry)
        reco = result_back_proj.eval()
        plt.figure()
        plt.imshow(np.squeeze(reco)[volume_shape[0] // 2],
                   cmap=plt.get_cmap('gist_gray'))
        plt.axis('off')
        plt.savefig('3d_cone_reco.png',
                    dpi=150,
                    transparent=False,
                    bbox_inches='tight')
Example #5
0
def example_parallel_3d():
    # ------------------ Declare Parameters ------------------

    # Volume Parameters:
    volume_size = 256
    volume_shape = [volume_size, volume_size, volume_size]
    volume_spacing = [1, 1, 1]

    # Detector Parameters:
    detector_size = 350
    detector_shape = [detector_size, detector_size]
    detector_spacing = [1, 1]

    # Trajectory Parameters:
    number_of_projections = 180
    angular_range = 1 * np.pi

    # create Geometry class
    geometry = GeometryParallel3D(volume_shape, volume_spacing, detector_shape,
                                  detector_spacing, number_of_projections,
                                  angular_range)
    geometry.set_trajectory(circular_trajectory_3d(geometry))

    # Get Phantom
    phantom = shepp_logan.shepp_logan_3d(volume_shape)

    # ------------------ Call Layers ------------------
    with tf.Session() as sess:
        #result = parallel_projection3d(phantom, geometry)

        result = par_projection3d(phantom, geometry)

        sinogram = result.eval()

        sinogram = sinogram + np.random.normal(loc=np.mean(np.abs(sinogram)),
                                               scale=np.std(sinogram),
                                               size=sinogram.shape) * 0.02

        reco_filter = filters.ram_lak_3D(geometry)

        sino_freq = np.fft.fft(sinogram, axis=2)
        sino_filtered_freq = np.multiply(sino_freq, reco_filter)
        sinogram_filtered = np.fft.ifft(sino_filtered_freq, axis=2)

        result_back_proj = par_backprojection3d(sinogram_filtered, geometry)
        reco = result_back_proj.eval()
        import pyconrad as pyc
        pyc.setup_pyconrad()
        pyc.imshow(phantom)
        pyc.imshow(sinogram)
        pyc.imshow(reco)
        a = 5
Example #6
0
def get_test_data(number_of_samples=1):

    data   = np.empty((number_of_samples,) + tuple(GEOMETRY.sinogram_shape))
    labels = np.empty((number_of_samples,) + tuple(GEOMETRY.volume_shape))

    with tf.Session() as sess:

        # get shepp logan 2d
        if number_of_samples == 1:
             labels[0] = shepp_logan.shepp_logan_enhanced(GEOMETRY.volume_shape)
             data[0] = generate_sinogram(np.expand_dims(labels[0],axis=0), parallel_projection2d, GEOMETRY)

        # every slice of shepp logan 3d with number_of_samples as Z-dimension as own image
        else:
            labels = shepp_logan.shepp_logan_3d((number_of_samples,) + tuple(GEOMETRY.sinogram_shape))
            for i in range(number_of_samples):
                data[i] = generate_sinogram_parallel_2d(np.expand_dims(labels[i], axis=0), GEOMETRY)

    return data, labels
Example #7
0
def example_cone_3d():
    # ------------------ Declare Parameters ------------------

    # Volume Parameters:
    volume_size = 8
    volume_shape = [volume_size, volume_size, volume_size]
    volume_spacing = [1, 1, 1]

    # Detector Parameters:
    detector_shape = [12, 12]
    detector_spacing = [1,1]

    # Trajectory Parameters:
    number_of_projections = 12
    angular_range = np.pi

    source_detector_distance = 1200
    source_isocenter_distance = 750

    # create Geometry class
    geometry = GeometryCone3D(volume_shape, volume_spacing, detector_shape, detector_spacing, number_of_projections, angular_range, source_detector_distance, source_isocenter_distance)
    geometry.set_trajectory(circular_trajectory.circular_trajectory_3d(geometry))

    # Get Phantom
    phantom = shepp_logan.shepp_logan_3d(volume_shape).astype(np.float32)
    # Add required batch dimension
    phantom = np.expand_dims(phantom, axis=0)
    sino = cone_projection3d(phantom,geometry)
    @tf.function
    def test_func_proj(x):
        return cone_projection3d(x,geometry)

    @tf.function
    def test_func_reco(x):
        return cone_backprojection3d(x,geometry)

    proj_theoretical, proj_numerical = tf.test.compute_gradient(test_func_proj, [sino])
    reco_theoretical, reco_numerical = tf.test.compute_gradient(test_func_reco, [sino])
Example #8
0
def example_cone_3d():
    # ------------------ Declare Parameters ------------------

    # Volume Parameters:
    volume_size = 256
    volume_shape = [volume_size, volume_size, volume_size]
    volume_spacing = [0.5, 0.5, 0.5]

    # Detector Parameters:
    detector_shape = [2 * volume_size, 2 * volume_size]
    detector_spacing = [1, 1]

    # Trajectory Parameters:
    number_of_projections = 360
    angular_range = 2 * np.pi

    source_detector_distance = 1200
    source_isocenter_distance = 750

    # create Geometry class
    geometry = GeometryCone3D(volume_shape, volume_spacing, detector_shape,
                              detector_spacing, number_of_projections,
                              angular_range, source_detector_distance,
                              source_isocenter_distance)
    geometry.set_projection_matrices(
        circular_trajectory.circular_trajectory_3d(geometry))

    # Get Phantom 3d
    phantom = shepp_logan.shepp_logan_3d(volume_shape)

    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.5
    config.gpu_options.allow_growth = True
    # ------------------ Call Layers ------------------
    with tf.Session(config=config) as sess:
        result = cone_projection3d(phantom, geometry)
        sinogram = result.eval()

        #TODO: Use 3D ramp / ram_lak not 1D
        # filtering
        filter = ramp(int(geometry.detector_shape[1]))
        sino_freq = np.fft.fft(sinogram, axis=2)
        filtered_sino_freq = np.zeros_like(sino_freq)
        for row in range(int(geometry.detector_shape[0])):
            for projection in range(geometry.number_of_projections):
                filtered_sino_freq[projection,
                                   row, :] = sino_freq[projection,
                                                       row, :] * filter[:]

        filtered_sino = np.fft.ifft(filtered_sino_freq, axis=2)

        result_back_proj = cone_backprojection3d(filtered_sino, geometry)
        reco = result_back_proj.eval()
        plt.figure()
        plt.imshow(reco[(int)(volume_shape[0] / 2), :, :],
                   cmap=plt.get_cmap('gist_gray'))
        plt.axis('off')
        plt.savefig('3d_cone_reco.png',
                    dpi=150,
                    transparent=False,
                    bbox_inches='tight')