|
a |
|
b/data_conversion/resample_ct2pt.py |
|
|
1 |
''' |
|
|
2 |
Copyright (c) Microsoft Corporation. All rights reserved. |
|
|
3 |
Licensed under the MIT License. |
|
|
4 |
''' |
|
|
5 |
|
|
|
6 |
import numpy as np |
|
|
7 |
import os |
|
|
8 |
import SimpleITK as sitk |
|
|
9 |
import pandas as pd |
|
|
10 |
from pathlib import Path |
|
|
11 |
import glob |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
def resample_ct_to_pt_geometry( |
|
|
15 |
ctpath: str, |
|
|
16 |
ptpath: str, |
|
|
17 |
savedir: str = '' |
|
|
18 |
): |
|
|
19 |
""" Function to resample CT images to the corresponding PET image geometry. |
|
|
20 |
This functions assumes that the CT and PET are already coregistered. |
|
|
21 |
|
|
|
22 |
Args: |
|
|
23 |
ctpath (str): path to NIFTI file for (high-resolution) CT image |
|
|
24 |
ptpath (str): path to NIFTI file for PET image |
|
|
25 |
savedir (str, optional): Directory to write the downsampled CT NIFTI image. Defaults to ''. |
|
|
26 |
""" |
|
|
27 |
ctimg = sitk.ReadImage(ctpath) |
|
|
28 |
ptimg = sitk.ReadImage(ptpath) |
|
|
29 |
resampled_ctimg = sitk.Resample(ctimg, ptimg, interpolator=sitk.sitkLinear, defaultPixelValue=-1024) |
|
|
30 |
resampled_ct_filepath = os.path.join(savedir, os.path.basename(ctpath)) |
|
|
31 |
|
|
|
32 |
sitk.WriteImage(resampled_ctimg, resampled_ct_filepath) |
|
|
33 |
print('Resampled CT to PET geometry') |
|
|
34 |
print(f'Saving the low-resolution CT NIFTI image at {resampled_ct_filepath}') |
|
|
35 |
|
|
|
36 |
def resample_gt_to_pt_geometry( |
|
|
37 |
gtpath: str, |
|
|
38 |
ptpath: str, |
|
|
39 |
savedir: str = '' |
|
|
40 |
): |
|
|
41 |
""" Function to resample GT images (if applicable) to the corresponding PET image geometry. |
|
|
42 |
You may or may not need to do this resampling. Do this if your ground truth segmentations |
|
|
43 |
were performed on CT images, and hence your GT masks are in the geometry of CT instead of PET. |
|
|
44 |
If the annoatations were performed on PET, then the GT mask and PET should (ideally) be in the |
|
|
45 |
same geometry and hence this step may not be required. |
|
|
46 |
|
|
|
47 |
Args: |
|
|
48 |
gtpath (str): path to NIFTI file for (high-resolution) GT image |
|
|
49 |
ptpath (str): path to NIFTI file for PET image |
|
|
50 |
savedir (str, optional): Directory to write the downsampled GT NIFTI image. Defaults to ''. |
|
|
51 |
""" |
|
|
52 |
gtimg = sitk.ReadImage(gtpath) |
|
|
53 |
ptimg = sitk.ReadImage(ptpath) |
|
|
54 |
resampled_gtimg = sitk.Resample(gtimg, ptimg, interpolator=sitk.sitkNearestNeighbor, defaultPixelValue=0) |
|
|
55 |
resampled_gt_filepath = os.path.join(savedir, os.path.basename(gtpath)) |
|
|
56 |
|
|
|
57 |
sitk.WriteImage(resampled_gtimg, resampled_gt_filepath) |
|
|
58 |
print('Resampled GT to PET geometry') |
|
|
59 |
print(f'Saving the low-resolution CT NIFTI image at {resampled_gt_filepath}') |