First, whether or SimpleITK pydicom are required prior to import into the python in the library, if you are using pycharm IDE, you can create python3 virtual environment, and then by file-setting-Project interpreter in a virtual environment, in which the module is added direct search name these two libraries, click install.
pydicom extract single dicom image
. 1 Import pydicom 2 from matplotlib Import pyplot . 3 . 4 DS = pydicom.read_file ( ' : C / the Users / **** / Desktop / CT000000.dcm ' ) # position DICOM file . 5 Print (ds.dir ()) # Print All DICOM TAG name . 6 Print (ds.dir ( ' Pixe ' )) # print a 'pat' of DICOM TAG . 7 Print (ds.PatientName, ds.PatientSex, ds.PatientID, ds.PatientBirthDate, ds.PatientAge) # Print DICOM TAG corresponding attribute value . 8 Print (ds.data_element (' PatientName ' )) # print a complete data element, comprising DICOMTAG encoded value (Group, the Element), the VR, the Value . 9 Print (ds.data_element ( ' PatientID ' ) .VR, ds.data_element ( ' PatientID ' ) .Value ) 10 pixel_bytes = ds.PixelData # original binary file . 11 12 is PIX = ds.pixel_array # pixel value matrix 13 is Print (pix.shape) # printing matrix dimension 14 pyplot.imshow (PIX, CMap = pylab.cm.bone) 15 pyplot .Show () # cmap represent colormap, it can be set to different values to obtain different display, print dicom pictures
Note that, at this time may be error, error place is ds.pixel_array, because some dicom file format can not be extracted with pydicom, reference answers .
extracting an image sequence dicom pydicom
. 1 Import pydicom 2 Import numpy . 3 from matplotlib Import pyplot . 4 . 5 # with lstFilesDCM as list storing DICOM files in . 6 PathDicom = " : D / dicom_image / V " # files python files in the same directory folder . 7 lstFilesDCM = [] . 8 9 # all dicom file read 10 for diName, subdirList, fileList in os.walk (PathDicom): 11 for filename in fileList: 12 IF " .DCM " in filename.lower (): # determine whether the file dicom file 13 Print (filename) 14 lstFilesDCM.append (os.path.join (diName, filename)) # added to the list 15 16 # # as the first picture Referring to FIG . 17 RefDs = pydicom.read_file (lstFilesDCM [10]) # read dicom first image 18 is # Print (RefDs) . 19 # Print (RefDs.pixel_array) 20 is # Print (RefDs.PatientPosition) 21 is pyplot.imshow (RefDs .pixel_array, CMap = pyplot.cm.bone) 22 is pyplot.show () 23 is 24 #Dimensional array, each record length, width, number of layers (i.e. the number of data dicom) 25 ConstPixelDims = (int (RefDs.Rows), int (RefDs.Columns), len (lstFilesDCM)) 26 is Print (ConstPixelDims) 27 28 # obtain spacing value (mm units) 29 # PixelSpacing - each pixel of the actual length and width, units (mm) 30 # SliceThickness - thickness of each slice unit (mm) 31 is ConstPixelSpacing = (a float (RefDs.PixelSpacing [0]), a float (RefDs.PixelSpacing [. 1 ]), a float (RefDs.SliceThickness)) 32 33 is # three-dimensional data 34 is X = numpy.arange (0.0, (ConstPixelDims [0] +. 1) ConstPixelSpacing * [0], constPixelSpacing [0]) # 0 to (plus a first dimension of the interval between pixels *), in steps of constpixelSpacing 35 Y = numpy.arange (0.0, (ConstPixelDims [. 1] +. 1) * ConstPixelSpacing [. 1], ConstPixelSpacing [. 1]) # 36 Z = numpy.arange (0.0, (ConstPixelDims [2] +. 1) * ConstPixelSpacing [2 ], ConstPixelSpacing [2]) # 37 [ Print (len (X), " XXXX " ) 38 is 39 ArrayDicom = numpy.zeros (ConstPixelDims, DTYPE = RefDs.pixel_array.dtype) 40 41 is # traverse all dicom files, read image data stored in numpy array 42 is for filenameDCM in lstFilesDCM: 43 is DS = pydicom.read_file (filenameDCM) 44 is ArrayDicom [:,:, lstFilesDCM.index (filenameDCM)] = ds.pixel_array 45 46 is 47 # axial surface of the display 48 # dpi means the number of pixels per inch, the greater the dpi, the print out of the picture clearer. It does not refer to the size of the picture. 49 # pixel in the display area at the time of printing with a resolution of the field that is used to print your images are only to consider the issue of the resolution of 50 pyplot.figure (dpi = 1000 ) 51 # coordinates axes are the same length becomes 52 is # pyplot.axes (). set_aspect ( 'equal', 'datalim') 53 is pyplot.axes (). set_aspect ( ' equal ' ) 54 is # picture becomes gray color 55 pyplot.set_cmap ( pyplot.gray ()) 56 is 57 ispyplot.imshow (ArrayDicom [:,:, 360]) # third dimension is represented now show the first layers 58 pyplot.show () 59 56 is # coronal surface of the display 60 pyplot.figure (= 100 dpi ) 61 is pyplot. . axes () set_aspect ( ' equal ' , ' datalim ' ) 62 is pyplot.set_cmap (pyplot.gray ()) 63 is pyplot.imshow (ArrayDicom [:, 90 ,:]) 64 pyplot.show ()
SimpleITK open a single image dicom
. 1 Import SimpleITK AS sitk 2 Import numpy AS NP . 3 from matplotlib Import pyplot . 4 . 5 File = sitk.ReadImage ( ' C: / the Users / **** / Desktop / CT1227429.dcm ' ) . 6 Print (file.GetSize ()) . 7 Print (file.GetOrigin ()) # coordinate origin . 8 Print (file.GetSpacing ()) # pixel pitch . 9 Print (file.GetDirection ()) # direction 10 pixel_array = sitk.GetArrayFromImage (File) # pixel matrix 11 print(pixel_array.shape) # 打印矩阵维度 12 image_array = np.squeeze(pixel_array) 13 print(image_array.shape) # 14 pyplot.imshow(image_array) 15 pyplot.show()
SimpleITK open multiple images dicom
. 1 Reader = sitk.ImageSeriesReader () 2 reader.MetaDataDictionaryArrayUpdateOn () # This step is loaded meta information disclosure . 3 reader.LoadPrivateTagsOn () # This step is loaded proprietary meta information . 4 series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs (DirectoryPath) # ID acquisition sequence according to the file folder, a folder which is usually all slices of a patient, will be divided into several series . 5 dicom_names = reader.GetGDCMSeriesFileNames (DirectoryPath, series_ID) # select one of the serial ID, to obtain a plurality of the file name of the sequence . 6 reader.SetFileNames (dicom_names) # file name is set . 7 image3D reader.Execute = () # read sequence dicom 8 # read key belonging to the slice by slice index, and then obtain a value corresponding to an index key by slicing 9 reader.GetMetaDataKeys(slice_index) 10 reader.GetMetaData(slice_index,key)