Python SimpleITK.ImageSeriesReader() Examples

The following are 6 code examples of SimpleITK.ImageSeriesReader(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module SimpleITK , or try the search function .
Example #1
Source File: crop_transform.py    From 3D-CNNs-for-Liver-Classification with Apache License 2.0 10 votes vote down vote up
def _transform(dcm_dir,save_dir):
###   from dcm to nii
    reader = sitk.ImageSeriesReader()
    dicom_names = reader.GetGDCMSeriesFileNames(dcm_dir)
    reader.SetFileNames(dicom_names)
    image2 = reader.Execute()
    
###   transform 3D image to array
    image_array = sitk.GetArrayFromImage(image2)  #  z,y,x

###   crop the dark voxel
#    new_array,range_list = get_bound(image_array)


###   transform array to 3D image
    image3 = sitk.GetImageFromArray(image_array)

###   save 3D image
    name = dcm_dir.split('/')[-1] + '.nii'
    save_path = os.path.join(save_dir,name)   #   get the save path
    sitk.WriteImage(image3,save_path) 
Example #2
Source File: io.py    From torchio with MIT License 5 votes vote down vote up
def _read_dicom(directory: TypePath):
    directory = Path(directory)
    if not directory.is_dir():  # unreachable if called from _read_sitk
        raise FileNotFoundError(f'Directory "{directory}" not found')
    reader = sitk.ImageSeriesReader()
    dicom_names = reader.GetGDCMSeriesFileNames(str(directory))
    if not dicom_names:
        message = (
            f'The directory "{directory}"'
            ' does not seem to contain DICOM files'
        )
        raise FileNotFoundError(message)
    reader.SetFileNames(dicom_names)
    image = reader.Execute()
    return image 
Example #3
Source File: dicom2Nii.py    From medSynthesisV1 with MIT License 5 votes vote down vote up
def main():
    path='/home/dongnie/warehouse/pelvicSeg/newData/pelvic_0118/'
    subpath='atkinson_lafayette'
    outfn=subpath+'.nii.gz'
    inputdir=path+subpath
    scan=ScanFile(path)  
    subdirs=scan.scan_subdir()  
    for subdir in subdirs:
        if subdir==path or subdir=='..':
            continue
        
        print 'subdir is, ',subdir
        
        ss=subdir.split('/')
        print 'ss is, ',ss, 'and s7 is, ',ss[7]
        
        outfn=ss[7]+'.nii.gz'
        
        reader = sitk.ImageSeriesReader()

        dicom_names = reader.GetGDCMSeriesFileNames(subdir)
        reader.SetFileNames(dicom_names)
        
        image = reader.Execute()
        
        size = image.GetSize()
        print( "Image size:", size[0], size[1], size[2] )
        
        print( "Writing image:", outfn)
        
        sitk.WriteImage(image,outfn) 
Example #4
Source File: demo.py    From Recursive-Cascaded-Networks with MIT License 5 votes vote down vote up
def load_dcm(fpath):
    reader = sitk.ImageSeriesReader()
    dicom_names = reader.GetGDCMSeriesFileNames(fpath)
    reader.SetFileNames(dicom_names)
    reader.MetaDataDictionaryArrayUpdateOn()
    reader.LoadPrivateTagsOn()
    image = reader.Execute()
    image_array = sitk.GetArrayFromImage(image) # z, y, x
    return image_array, reader 
Example #5
Source File: convert.py    From dsb-2017 with Apache License 2.0 5 votes vote down vote up
def read_scan(path):
    uid = os.path.basename(path)
    if uid.split('.')[-1] == 'mhd':
        uid = uid[:-4]
        return sitk.ReadImage(path), uid

    reader = sitk.ImageSeriesReader()
    image_files = reader.GetGDCMSeriesFileNames(path)
    assert len(image_files) > 0
    if len(image_files) < settings.chunk_size:
        print('Ignoring %s - only %d slices' % (path, len(image_files)))
        return None, uid

    reader.SetFileNames(image_files)
    return reader.Execute(), uid 
Example #6
Source File: Step1_PreProcessing.py    From 3D-RU-Net with GNU General Public License v3.0 4 votes vote down vote up
def ReadImageAndLabel(CasePath,inverted=False):
    #Reading Images
    Reader = sitk.ImageSeriesReader()
    name=findfiles(CasePath+'img/','*.dcm')
    for i in range(len(name)):
        name[i]=int(name[i][0:-4])
    name=sorted(name)
    name=name[::-1]
    for i in range(len(name)):
        #print name[i],'\n'
        name[i]=CasePath+'img/'+str(name[i])+'.dcm'
 
    Reader.SetFileNames(name)
    Image = Reader.Execute()
    Spacing=Image.GetSpacing()
    Origin = Image.GetOrigin()
    Direction = Image.GetDirection()
    


    #Reading Labels
    name=findfiles(CasePath+'label/','*.PNG')
    name=sorted(name)
    for i in range(len(name)):
        name[i]=CasePath+'label/'+name[i]
    #print name
    #Sometimes labels are inverted along Z axis and should be rectified in this dataset
    if inverted:
        pass
    else:
        name=name[::-1]
#    for i in range(len(name)):
#        print name[i]+'\n'
    Reader.SetFileNames(name)
    Label = Reader.Execute()
    LabelArray=sitk.GetArrayFromImage(Label)
    LabelArray=((255-LabelArray[:,:,:,1])).astype(np.uint8)/255
    Label=sitk.GetImageFromArray(LabelArray)
    Label.SetSpacing(Spacing)
    Label.SetOrigin(Origin)
    Label.SetDirection(Direction)
    return Image,Label