|
| 1 | +__all__ = [ |
| 2 | + "CT2D", |
| 3 | +] |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from pylops import LinearOperator |
| 11 | +from pylops.utils import deps |
| 12 | +from pylops.utils.decorators import reshaped |
| 13 | +from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray |
| 14 | + |
| 15 | +logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) |
| 16 | + |
| 17 | + |
| 18 | +astra_message = deps.astra_import("the astra module") |
| 19 | + |
| 20 | +if astra_message is None: |
| 21 | + import astra |
| 22 | + |
| 23 | + |
| 24 | +class CT2D(LinearOperator): |
| 25 | + r"""2D Computerized Tomography |
| 26 | +
|
| 27 | + Apply 2D computerized tomography operator to model to obtain a |
| 28 | + 2D sinogram. |
| 29 | +
|
| 30 | + Note that the CT2D operator is an overload of the ``astra`` |
| 31 | + implementation of the tomographic operator. Refer to |
| 32 | + https://www.astra-toolbox.com/ for a detailed description of the |
| 33 | + input parameters. |
| 34 | +
|
| 35 | + Parameters |
| 36 | + ---------- |
| 37 | + dims : :obj:`list` or :obj:`int` |
| 38 | + Number of samples for each dimension. Must be 2-dimensional and of size :math:`n_y \times n_x` |
| 39 | + det_width : :obj:`float` |
| 40 | + Detector width |
| 41 | + det_count : :obj:`int` |
| 42 | + Number of detectors |
| 43 | + thetas : :obj:`numpy.ndarray` |
| 44 | + Vector of angles in degrees |
| 45 | + proj_geom_type : :obj:`str`, optional |
| 46 | + Type of projection geometry (``parallel`` or ``fanflat``) |
| 47 | + source_origin_dist : :obj:`float`, optional |
| 48 | + Distance between source and origin (only for ``proj_geom_type=fanflat``) |
| 49 | + origin_detector_dist : :obj:`float`, optional |
| 50 | + Distance between origin and detector along the source-origin line |
| 51 | + (only for "proj_geom_type=fanflat") |
| 52 | + projector_type : :obj:`int`, optional |
| 53 | + Type of projection geometry (``strip``, or ``line``, or ``linear``) |
| 54 | + dtype : :obj:`str`, optional |
| 55 | + Type of elements in input array. |
| 56 | + name : :obj:`str`, optional |
| 57 | + Name of operator (to be used by :func:`pylops.utils.describe.describe`) |
| 58 | +
|
| 59 | + Attributes |
| 60 | + ---------- |
| 61 | + shape : :obj:`tuple` |
| 62 | + Operator shape |
| 63 | + explicit : :obj:`bool` |
| 64 | + Operator contains a matrix that can be solved |
| 65 | + explicitly (``True``) or not (``False``) |
| 66 | +
|
| 67 | + Notes |
| 68 | + ----- |
| 69 | + The CT2D operator applies parallel or fan beam computerized tomography operators |
| 70 | + to 2-dimensional objects and produces their corresponding sinograms. |
| 71 | +
|
| 72 | + Mathematically the forward operator can be described as [1]_: |
| 73 | +
|
| 74 | + .. math:: |
| 75 | + s(r,\theta; i) = \int_l i(l(r,\theta)) dl |
| 76 | +
|
| 77 | + where :math:`l(r,\theta)` is the summation line and :math:`i(x, y)` |
| 78 | + is the intensity map of the model. Here, :math:`\theta` refers to the angle |
| 79 | + between the y-axis (:math:`y`) and the summation line and :math:`r` is |
| 80 | + the distance from the origin of the summation line. |
| 81 | +
|
| 82 | + .. [1] http://people.compute.dtu.dk/pcha/HDtomo/astra-introduction.pdf |
| 83 | +
|
| 84 | + """ |
| 85 | + |
| 86 | + def __init__( |
| 87 | + self, |
| 88 | + dims: InputDimsLike, |
| 89 | + det_width: int, |
| 90 | + det_count: float, |
| 91 | + thetas: NDArray, |
| 92 | + proj_geom_type: Optional[str] = "parallel", |
| 93 | + source_origin_dist: float = None, |
| 94 | + origin_detector_dist: float = None, |
| 95 | + projector_type: Optional[str] = "strip", |
| 96 | + dtype: DTypeLike = "float64", |
| 97 | + name: str = "C", |
| 98 | + ) -> None: |
| 99 | + if astra_message is not None: |
| 100 | + raise NotImplementedError(astra_message) |
| 101 | + |
| 102 | + # create volume and projection geometries |
| 103 | + self.vol_geom = astra.create_vol_geom(dims) |
| 104 | + if proj_geom_type == "parallel": |
| 105 | + self.proj_geom = astra.create_proj_geom( |
| 106 | + proj_geom_type, det_width, det_count, thetas |
| 107 | + ) |
| 108 | + else: |
| 109 | + self.proj_geom = astra.create_proj_geom( |
| 110 | + proj_geom_type, |
| 111 | + det_width, |
| 112 | + det_count, |
| 113 | + thetas, |
| 114 | + source_origin_dist, |
| 115 | + origin_detector_dist, |
| 116 | + ) |
| 117 | + |
| 118 | + # create projector |
| 119 | + self.proj_id = astra.create_projector( |
| 120 | + projector_type, self.proj_geom, self.vol_geom |
| 121 | + ) |
| 122 | + super().__init__( |
| 123 | + dtype=np.dtype(dtype), dims=dims, dimsd=(len(thetas), det_count), name=name |
| 124 | + ) |
| 125 | + |
| 126 | + @reshaped |
| 127 | + def _matvec(self, x): |
| 128 | + y_id, y = astra.create_sino(x, self.proj_id) |
| 129 | + astra.data2d.delete(y_id) |
| 130 | + return y |
| 131 | + |
| 132 | + @reshaped |
| 133 | + def _rmatvec(self, x): |
| 134 | + y_id, y = astra.create_backprojection(x, self.proj_id) |
| 135 | + astra.data2d.delete(y_id) |
| 136 | + return y |
| 137 | + |
| 138 | + def __del__(self): |
| 139 | + astra.projector.delete(self.proj_id) |
0 commit comments