|
import numpy as np |
|
import nibabel as nib |
|
import os |
|
from glob import glob |
|
import argparse |
|
|
|
|
|
def parse_command_line(): |
|
print('---'*10) |
|
print('Parsing Command Line Arguments') |
|
parser = argparse.ArgumentParser( |
|
description='Defacing protocol') |
|
parser.add_argument('-sc', metavar='Scans', type=str, |
|
help="An integer belonging to the scan ids you wish to choose as template") |
|
parser.add_argument('-mk', metavar='Masks', type=str, |
|
help="An integer belonging to the scan ids you wish to choose as template segmentation id") |
|
parser.add_argument('-bp', metavar='base path', type=str, |
|
help="Absolute path of the base directory") |
|
argv = parser.parse_args() |
|
return argv |
|
|
|
|
|
def deface(input_file, mask_file, output_file=None, suffix=" (masked)", write=True): |
|
|
|
input = nib.load(input_file) |
|
|
|
|
|
segmentation = nib.load(mask_file) |
|
|
|
input_array = input.get_fdata() |
|
segmentation_array = segmentation.get_fdata() |
|
mask = 1-segmentation_array |
|
|
|
|
|
output_array = input_array * mask |
|
output = nib.Nifti1Image(output_array, input.affine, input.header) |
|
|
|
|
|
if write: |
|
if output_file is None: |
|
output_file = input_file.split(".")[0] + suffix + ".nii.gz" |
|
output.to_filename(output_file) |
|
else: |
|
|
|
output.to_filename(output_file) |
|
return output |
|
|
|
|
|
def main(): |
|
args = parse_command_line() |
|
base = args.bp |
|
images = args.sc |
|
masks = args.mk |
|
|
|
CT_images = sorted(glob(os.path.join(base, images, '*.nii.gz'))) |
|
mask_images = sorted(glob(os.path.join(base, masks, '*.nii.gz'))) |
|
|
|
num = len(CT_images) |
|
print(num) |
|
|
|
for i in range(num): |
|
deface(CT_images[i], mask_images[i], output_file=None, |
|
suffix=' (masked)', write=True) |
|
|
|
|
|
if __name__ == '__main__': |
|
main() |
|
|