Beispiel #1
0
def img_to_h5(img_dir, h5_output_name):
    """ Saves all slices from an image to a h5 file. """
    img = load_dicom_series(img_dir)

    with h5py.File(h5_output_name, 'w') as f:
        f.create_dataset(H5_DATASET_NAME, data=img)

        print("Saved")
Beispiel #2
0
def dicom_to_h5(root_dir, h5):
    sub_dirs = [x[0] for x in os.walk(root_dir)]  # Gather all subdirectories in 'root_dir'
    for directory in sub_dirs:
        # print(directory)
        file_list = glob.glob(directory + '/*.dcm')  # Look for .dcm files
        if not file_list:  # If we find a dir with a .dcm series, process it
            continue

        dcm_filename = file_list[0]  # Checking just one .dcm file is sufficient
        img = SimpleITK.ReadImage(dcm_filename)  # Read single .dcm file to obtain metadata

        # Extract some metadata that we want to keep
        patient_id = img.GetMetaData('0010|0020').strip()
        patient_age = img.GetMetaData('0010|1010').strip()
        series_number = int(img.GetMetaData('0020|0011').strip())
        series_description = img.GetMetaData('0008|103e').strip()

        data_path = patient_id + '/' + series_description
        print('Converting: {}'.format(data_path))

        # If we find a DICOM series that already exists, we check if the series number is higher. If so, remove
        # series that is already present and add this one.
        create = False

        if data_path in h5:
            if h5[data_path]['pixel_array'].attrs.get('SeriesNr') < series_number:
                del h5[data_path]
                print('New series has higher series number, so adding.')
                create = True
            else:
                print('New series has lower series number, so not adding.')
        else:
            create = True

        if create:
            group = h5.create_group(data_path)
            pixeldata = group.create_dataset('pixel_array', data=load_dicom_series(directory))
            pixeldata.attrs.create('Age', patient_age)
            pixeldata.attrs.create('SeriesNr', series_number)
Beispiel #3
0
def dicom_to_h5(root_dir, h5):
    sub_dirs = [x[0] for x in os.walk(root_dir)]  # Gather all subdirectories in 'root_dir'
    ages = {}
    for directory in sub_dirs:
        # print(directory)
        if directory.split(os.sep)[-1] == 'Ktrans': #if the file is from Ktrans, look for the .mhd
            Im_Ktrans = True
            file_list = glob.glob(directory + '/*.mhd')
        else:
            Im_Ktrans = False
            file_list = glob.glob(directory + '/*.dcm')  # Look for .dcm files
        if not file_list:  # If we find a dir with a .dcm or a .mhd series, process it
            continue

        series_filename = file_list[0]  # Checking just one .dcm file is sufficient
        img = sitk.ReadImage(series_filename)  # Read single .dcm file to obtain metadata

        # Extract some metadata that we want to keep
        if Im_Ktrans:
            patient_id = series_filename.split(os.sep)[-3]
            patient_age = ages[patient_id]
            series_number = 0
            series_description = 'Ktrans'
        else:
            patient_id = img.GetMetaData('0010|0020').strip()
            patient_age = img.GetMetaData('0010|1010').strip()
            series_number = int(img.GetMetaData('0020|0011').strip())
            series_description = img.GetMetaData('0008|103e').strip()

            # Add the age info in the dictionary for Ktrans
            ages[patient_id] = patient_age


        # Combine series description and series number to create a unique identifier for this DICOM series.
        # Should be unique for each patient. Only exception is ProstateX-0025, hence the try: except: approach.
        data_path = patient_id + '/' + series_description #+ '_' + series_number
        print('Converting: {}'.format(data_path))

        # If we find a DICOM series that already exists, we check if the series number is higher. If so, remove
        # series that is already present and add this one.
        create = False

        if data_path in h5:
            if h5[data_path]['pixel_array'].attrs.get('SeriesNr') < series_number:
                del h5[data_path]
                print('New series has higher series number, so adding.')
                create = True
            else:
                print('New series has lower series number, so not adding.')
        else:
            create = True

        if create:
            #print(patient_id)
            group = h5.create_group(data_path)
            if Im_Ktrans:
                data = sitk.GetArrayFromImage(img)
                pixeldata = group.create_dataset('pixel_array', data=data)
            else:
                pixeldata = group.create_dataset('pixel_array', data=load_dicom_series(directory))
            pixeldata.attrs.create('Age', patient_age)
            pixeldata.attrs.create('SeriesNr', series_number)