| Total Complexity | 0 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import torchio as tio |
||
| 2 | import matplotlib.pyplot as plt |
||
| 3 | |||
| 4 | # ---- Load example MRI ---- |
||
| 5 | subject = tio.datasets.Colin27() |
||
| 6 | |||
| 7 | # Get full 3D volume |
||
| 8 | image_3d = subject.t1.data.squeeze().numpy() |
||
| 9 | |||
| 10 | # Pick center slice (axial) |
||
| 11 | slice_idx = image_3d.shape[2] // 2 |
||
| 12 | image = image_3d[:, :, slice_idx] |
||
| 13 | |||
| 14 | # ---- Apply custom transform ---- |
||
| 15 | transform = tio.RandomBiasFieldDenoise(noise_reduction_factor=0.3) |
||
| 16 | denoised_subject = transform(subject) |
||
| 17 | |||
| 18 | # Extract denoised slice |
||
| 19 | denoised_3d = denoised_subject.t1.data.squeeze().numpy() |
||
| 20 | denoised_image = denoised_3d[:, :, slice_idx] |
||
| 21 | |||
| 22 | # ---- Plot ---- |
||
| 23 | fig, axes = plt.subplots(1, 2, figsize=(10, 5)) |
||
| 24 | |||
| 25 | axes[0].imshow(image, cmap='gray') |
||
| 26 | axes[0].set_title("Original") |
||
| 27 | axes[0].axis("off") |
||
| 28 | |||
| 29 | axes[1].imshow(denoised_image, cmap='gray') |
||
| 30 | axes[1].set_title("After Denoise") |
||
| 31 | axes[1].axis("off") |
||
| 32 | |||
| 33 | plt.tight_layout() |
||
| 34 | plt.show() |
||
| 35 |