示例#1
0
def convert_ima(input_dir, output_dir):
    import dicom2nifti
    import dicom2nifti.settings as settings
    settings.disable_validate_orthogonal()
    settings.enable_resampling()
    settings.set_resample_spline_interpolation_order(1)
    settings.set_resample_padding(-1000)
    dicom2nifti.convert_directory(input_dir, output_dir)
示例#2
0
    def test_convert_directory(self):

        tmp_output_dir = tempfile.mkdtemp()
        try:
            settings.enable_resampling()
            settings.set_resample_spline_interpolation_order(1)
            settings.set_resample_padding(-1000)
            settings.disable_validate_slice_increment()
            settings.disable_validate_orthogonal()
            convert_directory.convert_directory("/Users/abrys/Downloads/dicom_CT_UNKNOWN_3",
                                                "/Users/abrys/Downloads")

        finally:
            shutil.rmtree(tmp_output_dir)
示例#3
0
    def test_convert_directory(self):

        tmp_output_dir = tempfile.mkdtemp()
        try:
            settings.enable_resampling()
            settings.set_resample_spline_interpolation_order(1)
            settings.set_resample_padding(-1000)
            settings.disable_validate_slice_increment()
            settings.disable_validate_orthogonal()
            convert_directory.convert_directory("/Users/abrys/Downloads/test",
                                                "/Users/abrys/Downloads")

        finally:
            shutil.rmtree(tmp_output_dir)
def generate_inconsistent_slice_incement():
    settings.disable_validate_orthogonal()
    settings.disable_validate_slice_increment()
    settings.enable_resampling()
    settings.set_resample_padding(0)
    settings.set_resample_spline_interpolation_order(1)

    dicom_directory = os.path.join(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'tests',
        'data', 'failing', 'sliceincrement', '002')

    output_file = dicom_directory + '_ground_truth.nii.gz'
    reoriented_file = dicom_directory + '_ground_truth_reoriented.nii.gz'
    generate_ground_truth(dicom_directory, output_file, reoriented_file)

    settings.disable_resampling()
    settings.enable_validate_slice_increment()
    settings.enable_validate_orientation()
示例#5
0
    def test_inconsistent_slice_increment_resampling(self):
        tmp_output_dir = tempfile.mkdtemp()
        try:
            settings.disable_validate_orthogonal()
            settings.disable_validate_slice_increment()
            settings.enable_resampling()
            settings.set_resample_padding(0)
            settings.set_resample_spline_interpolation_order(1)
            results = convert_generic.dicom_to_nifti(read_dicom_directory(test_data.FAILING_SLICEINCREMENT_2),
                                                     os.path.join(tmp_output_dir, 'test.nii.gz'))
            assert_compare_nifti(results['NII_FILE'],
                                 ground_thruth_filenames(test_data.FAILING_SLICEINCREMENT_2)[0])
            self.assertTrue(isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

        finally:
            settings.disable_resampling()
            settings.enable_validate_slice_increment()
            settings.enable_validate_orientation()
            shutil.rmtree(tmp_output_dir)
def generate_inconsistent_slice_incement():
    settings.disable_validate_orthogonal()
    settings.disable_validate_slice_increment()
    settings.enable_resampling()
    settings.set_resample_padding(0)
    settings.set_resample_spline_interpolation_order(1)

    dicom_directory = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
                                   'tests',
                                   'data',
                                   'failing',
                                   'sliceincrement',
                                   '002')

    output_file = dicom_directory + '_ground_truth.nii.gz'
    reoriented_file = dicom_directory + '_ground_truth_reoriented.nii.gz'
    generate_ground_truth(dicom_directory, output_file, reoriented_file)

    settings.disable_resampling()
    settings.enable_validate_slice_increment()
    settings.enable_validate_orientation()
示例#7
0
    def test_inconsistent_slice_increment_resampling(self):
        tmp_output_dir = tempfile.mkdtemp()
        try:
            settings.disable_validate_orthogonal()
            settings.disable_validate_slice_increment()
            settings.enable_resampling()
            settings.set_resample_padding(0)
            settings.set_resample_spline_interpolation_order(1)
            results = convert_generic.dicom_to_nifti(
                read_dicom_directory(test_data.FAILING_SLICEINCREMENT_2),
                os.path.join(tmp_output_dir, 'test.nii.gz'))
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(test_data.FAILING_SLICEINCREMENT_2)[0])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

        finally:
            settings.disable_resampling()
            settings.enable_validate_slice_increment()
            settings.enable_validate_orientation()
            shutil.rmtree(tmp_output_dir)
示例#8
0
 def setUp(self):
     settings.disable_validate_orthogonal()
     settings.enable_resampling()
     settings.set_resample_padding(-1000)
     settings.set_resample_spline_interpolation_order(1)
示例#9
0
# Import necessary dcm2niix python wrapper
import dicom2nifti
import dicom2nifti.settings as settings

# Set settings for conversion
settings.enable_resampling()
settings.set_resample_spline_interpolation_order(1)
settings.set_resample_padding(-3024)
dicom2nifti.settings.disable_validate_orthogonal()

# Get directory of .dcm folder
import os
import shutil

dirs = os.listdir("D:\MDRP/train/")

# I'm going to output the file to a temporary folder because I can't rename the file
# The script names the file as the modality instead of original folder name and I can't find how to change it in the python wrapper
temp_folder = 'D:\MDRP/preprocessing/temp/'

# This is the main output folder where we want our correctly named files 
output_folder = 'D:\MDRP/preprocessing/nifti_output/'

for file in dirs:
    try:
        original_file = file

        # First we list the directories
        dicom_directory = 'D:\MDRP/train/' + (original_file)

        # Converting a file in the dcm directory to the temp folder
#  [email protected], [email protected], [email protected]

import os, argparse, dicom2nifti, dicom2nifti.settings as settings
from tqdm import tqdm


if __name__ == '__main__':

	parser = argparse.ArgumentParser('Dicom2Nifti')
	parser.add_argument('--dicom', type=str, default=None, help='Directory for DICOM Image Series/Directories')
	parser.add_argument('--output', type=str, default=None, help='Output NIFTI Filename/Directory')
	parser.add_argument('--batch', action='store_true', default=False, help='Batch Generation')

	settings.disable_validate_slice_increment()
	settings.enable_resampling()
	settings.set_resample_spline_interpolation_order(1)
	settings.set_resample_padding(-1000)

	'''Ref: https://dicom2nifti.readthedocs.io/en/latest/_modules/dicom2nifti/convert_dicom.html#dicom_series_to_nifti'''
	'''Ref: https://dicom2nifti.readthedocs.io/en/latest/readme.html#from-python'''
	args = parser.parse_args()
	if args.batch:
		os.mkdir(args.output) if not os.path.exists(args.output) else None
		for dir_ in tqdm(os.listdir(args.dicom)):
			# dicom2nifti.convert_directory(args.dicom, args.output)
			intputd = os.path.join(args.dicom, dir_)
			outputf = os.path.join(args.output, dir_)
			dicom2nifti.convert_dicom.dicom_series_to_nifti(intputd, outputf)
	else:
		dicom2nifti.convert_dicom.dicom_series_to_nifti(args.dicom, args.output)