|
| 1 | +r""" |
| 2 | +MRI modelling |
| 3 | +============= |
| 4 | +This example shows how to use the :py:class:`pylops.medical.mri.MRI2D` operator |
| 5 | +to create K-space undersampled MRI data. |
| 6 | +""" |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +import pylops |
| 11 | + |
| 12 | +plt.close("all") |
| 13 | +np.random.seed(0) |
| 14 | + |
| 15 | +############################################################################### |
| 16 | +# Let"s start by loading the Shepp-Logan phantom model. |
| 17 | +x = np.load("../testdata/optimization/shepp_logan_phantom.npy") |
| 18 | +x = x / x.max() |
| 19 | +nx, ny = x.shape |
| 20 | + |
| 21 | +############################################################################### |
| 22 | +# Next, we create a mask to simulate undersampling in K-space and apply it to |
| 23 | +# the phantom model. |
| 24 | + |
| 25 | +# Passing mask as array |
| 26 | +mask = np.zeros((nx, ny)) |
| 27 | +mask[:, np.random.randint(0, ny, 2 * ny // 3)] = 1 |
| 28 | +mask[:, ny // 2 - 20 : ny // 2 + 10] = 1 |
| 29 | + |
| 30 | +Mop = pylops.medical.MRI2D(dims=(nx, ny), mask=mask) |
| 31 | + |
| 32 | +d = Mop @ x |
| 33 | +x_adj = Mop.H @ d |
| 34 | + |
| 35 | +fig, axs = plt.subplots(1, 3, figsize=(12, 5)) |
| 36 | +axs[0].imshow(x, cmap="gray", vmin=0, vmax=1) |
| 37 | +axs[0].set_title("Original Image") |
| 38 | +axs[1].imshow(np.abs(d), cmap="jet", vmin=0, vmax=1) |
| 39 | +axs[1].set_title("K-space Data") |
| 40 | +axs[2].imshow(x_adj.real, cmap="gray", vmin=0, vmax=1) |
| 41 | +axs[2].set_title("Adjoint Reconstruction") |
| 42 | +fig.tight_layout() |
| 43 | + |
| 44 | +############################################################################### |
| 45 | +# Alternatively, we can create the same mask by specifying a sampling pattern |
| 46 | +# using the ``mask`` keyword argument. Here, we create a ``vertical-reg`` mask |
| 47 | +# that samples K-space lines in the vertical direction with a regular pattern. |
| 48 | + |
| 49 | +# Vertical uniform with center |
| 50 | +Mop = pylops.medical.MRI2D( |
| 51 | + dims=(nx, ny), mask="vertical-reg", nlines=ny // 2, perc_center=0.0 |
| 52 | +) |
| 53 | + |
| 54 | +d = Mop @ x |
| 55 | +x_adj = (Mop.H @ d).reshape(nx, ny) |
| 56 | + |
| 57 | +fig, axs = plt.subplots(1, 3, figsize=(12, 5)) |
| 58 | +axs[0].imshow(x, cmap="gray", vmin=0, vmax=1) |
| 59 | +axs[0].set_title("Original Image") |
| 60 | +axs[1].imshow(np.abs(Mop.ROp.H @ d).reshape(nx, ny), cmap="jet", vmin=0, vmax=1) |
| 61 | +axs[1].set_title("K-space Data") |
| 62 | +axs[2].imshow(x_adj.real, cmap="gray", vmin=0, vmax=1) |
| 63 | +axs[2].set_title("Adjoint Reconstruction") |
| 64 | +fig.tight_layout() |
| 65 | + |
| 66 | +############################################################################### |
| 67 | +# Similarly, we can create a ``vertical-uni`` mask that randomly samples |
| 68 | +# K-space lines in the vertical direction. |
| 69 | + |
| 70 | +# Vertical uniform with center |
| 71 | +Mop = pylops.medical.MRI2D( |
| 72 | + dims=(nx, ny), mask="vertical-uni", nlines=40, perc_center=0.1 |
| 73 | +) |
| 74 | + |
| 75 | +d = Mop @ x |
| 76 | +x_adj = (Mop.H @ d).reshape(nx, ny) |
| 77 | + |
| 78 | +fig, axs = plt.subplots(1, 3, figsize=(12, 5)) |
| 79 | +axs[0].imshow(x, cmap="gray", vmin=0, vmax=1) |
| 80 | +axs[0].set_title("Original Image") |
| 81 | +axs[1].imshow(np.abs(Mop.ROp.H @ d).reshape(nx, ny), cmap="jet", vmin=0, vmax=1) |
| 82 | +axs[1].set_title("K-space Data") |
| 83 | +axs[2].imshow(x_adj.real, cmap="gray", vmin=0, vmax=1) |
| 84 | +axs[2].set_title("Adjoint Reconstruction") |
| 85 | +fig.tight_layout() |
| 86 | + |
| 87 | +############################################################################### |
| 88 | +# Finally, we can create a sampling pattern with radial lines using the |
| 89 | +# ``radial-uni`` (or ``radial-reg``) option. |
| 90 | + |
| 91 | +# Radial uniform |
| 92 | +Mop = pylops.medical.MRI2D(dims=(nx, ny), mask="radial-uni", nlines=40) |
| 93 | + |
| 94 | +d = Mop @ x |
| 95 | +x_adj = (Mop.H @ d).reshape(nx, ny) |
| 96 | + |
| 97 | +fig, axs = plt.subplots(1, 3, figsize=(12, 5)) |
| 98 | +axs[0].imshow(x, cmap="gray", vmin=0, vmax=1) |
| 99 | +axs[0].set_title("Original Image") |
| 100 | +axs[1].imshow(np.abs(Mop.ROp.H @ d).reshape(nx, ny), cmap="jet", vmin=0, vmax=1) |
| 101 | +axs[1].set_title("K-space Data") |
| 102 | +axs[2].imshow(x_adj.real, cmap="gray", vmin=0, vmax=1) |
| 103 | +axs[2].set_title("Adjoint Reconstruction") |
| 104 | +fig.tight_layout() |
0 commit comments