Notebooks
F
FastAI
60 Medical.Imaging

60 Medical.Imaging

colabmachine-learninggpudeep-learningnotebooksPythonnbsfastaipytorch
[ ]
[ ]

Medical Imaging

Helpers for working with DICOM files

[ ]
[ ]
[ ]
[ ]

Patching

[ ]
[ ]

fastai.medical.imaging uses pydicom.dcmread to read a DICOM file. To view the header of a DICOM, specify the path of a test file and call dcmread.

[ ]
Dataset.file_meta -------------------------------
,(0002, 0000) File Meta Information Group Length  UL: 176
,(0002, 0001) File Meta Information Version       OB: b'\x00\x01'
,(0002, 0002) Media Storage SOP Class UID         UI: CT Image Storage
,(0002, 0003) Media Storage SOP Instance UID      UI: 9999.180975792154576730321054399332994563536
,(0002, 0010) Transfer Syntax UID                 UI: Explicit VR Little Endian
,(0002, 0012) Implementation Class UID            UI: 1.2.40.0.13.1.1.1
,(0002, 0013) Implementation Version Name         SH: 'dcm4che-1.4.38'
,-------------------------------------------------
,(0008, 0018) SOP Instance UID                    UI: ID_e0cc6a4b5
,(0008, 0060) Modality                            CS: 'CT'
,(0010, 0020) Patient ID                          LO: 'ID_a107dd7f'
,(0020, 000d) Study Instance UID                  UI: ID_6468bdd34a
,(0020, 000e) Series Instance UID                 UI: ID_4be303ae64
,(0020, 0010) Study ID                            SH: ''
,(0020, 0032) Image Position (Patient)            DS: [-125.000, -122.268, 115.936]
,(0020, 0037) Image Orientation (Patient)         DS: [1.000000, 0.000000, 0.000000, 0.000000, 0.978148, -0.207912]
,(0028, 0002) Samples per Pixel                   US: 1
,(0028, 0004) Photometric Interpretation          CS: 'MONOCHROME2'
,(0028, 0010) Rows                                US: 256
,(0028, 0011) Columns                             US: 256
,(0028, 0030) Pixel Spacing                       DS: [0.488281, 0.488281]
,(0028, 0100) Bits Allocated                      US: 16
,(0028, 0101) Bits Stored                         US: 16
,(0028, 0102) High Bit                            US: 15
,(0028, 0103) Pixel Representation                US: 1
,(0028, 1050) Window Center                       DS: "40.0"
,(0028, 1051) Window Width                        DS: "100.0"
,(0028, 1052) Rescale Intercept                   DS: "-1024.0"
,(0028, 1053) Rescale Slope                       DS: "1.0"
,(7fe0, 0010) Pixel Data                          OW: Array of 131072 elements
[ ]
pydicom.dataset.FileDataset
[ ]
[ ]
[ ]
[ ]
[ ]
tensor([[-1024., -1024., -1024.,  ..., -1024., -1024., -1024.],
,        [-1024., -1024., -1024.,  ..., -1024., -1024., -1024.],
,        [-1024., -1024., -1024.,  ..., -1024., -1024., -1024.],
,        ...,
,        [-1024., -1024., -1024.,  ..., -1024., -1024., -1024.],
,        [-1024., -1024., -1024.,  ..., -1024., -1024., -1024.],
,        [-1024., -1024., -1024.,  ..., -1024., -1024., -1024.]])
[ ]

scaled_px uses RescaleSlope and RescaleIntercept values to correctly scale the image so that they represent the correct tissue densities. You can observe what scaled_px does by viewing the the pixel distribution of a dicom image. The histogram below displays the current pixel distribution which shows a pixel range between -1133 and 2545.

[ ]
Output

As shown in the header of the test image the RescaleIntercept has a value of -1024.0 and a RescaleSlope value of 1.0. scaled_px will scale the pixels by these values.

[ ]
Output

The pixel distibution is now between -2157 and 1521

[ ]
[ ]

For example with n_bins set to 1 this means the bins will be split into 3 distinct bins (the beginning, the end and the number of bins specified by n_bins.

[ ]
tensor([-1076.,    40.,  2375.])
[ ]
Output

with n_bins at 100

[ ]
tensor([-1076., -1026., -1024., -1021.,    28.,    30.,    31.,    32.,    33.,
,           34.,    35.,    36.,    37.,    38.,    39.,    40.,    41.,    42.,
,           44.,    48.,    52.,    58.,    66.,    72.,    76.,    80.,    85.,
,           91.,    94.,    98.,   103.,   111.,   123.,   161.,   219.,   478.,
,          829.,   999.,  1027.,  1038.,  1044.,  1047.,  1049.,  1050.,  1051.,
,         1052.,  1053.,  1054.,  1055.,  1056.,  1057.,  1058.,  1059.,  1060.,
,         1062.,  1066.,  1108.,  1265.,  1453.,  1616.,  1741.,  1838.,  1943.,
,         2051.,  2220.,  2375.])
[ ]
Output
[ ]
[ ]

The test image has pixel values that range between -1000 and 2500

[ ]
Output

hist_scaled provides a way of scaling the input pixel values to between 0 and 1

[ ]
Output
[ ]
[ ]
Output
[ ]
Output

Dicom images can contain a high amount of voxel values and windowing can be thought of as a means of manipulating these values in order to change the apperance of the image so particular structures are highlighted. A window has 2 values:

  • l = window level or center aka brightness
  • w = window width or range aka contrast
[ ]
[ ]
[ ]
[ ]
Output
[ ]
[ ]
Output
[ ]
[ ]
[ ]
Output
[ ]
Output

Some dicom datasets such as the The Thyroid Segmentation in Ultrasonography Dataset is a dataset where each image has multiple frames per file (hundreds in this case). By default the show function will display 1 frame but if the dataset has multiple frames you can specify the number of frames to view.

[ ]
[ ]
Output
[ ]
[ ]
0.19049072265625

pct_in_window can be used to check what percentage of the image is composed of meaningful pixels (pixels within the specified window)

[ ]
[ ]
Output
[ ]
[ ]
Output

Images are often affected by random variations in intensity values, called noise. Gaussian noise contains variatons in intensity that are drawn from a Gaussian or normal distribution. A Guassian filter is usually used to blur edges and remove smaller or thinner areas in order to preserve the most important information

[ ]
[ ]
[ ]
Output
[ ]
[ ]
[ ]
Output
[ ]
[ ]
[ ]
torch.Size([1, 128, 128])
Output

Comparing the original image with the image from using the mask and crop_resize function

[ ]
Output
[ ]
[ ]

to_nchan takes a tensor or a dicom as the input and returns multiple one channel images (the first depending on the choosen windows and a normalized image). Setting bins to 0 only returns the windowed image.

[ ]
Output
[ ]
Output
[ ]
[ ]
[ ]
Output
[ ]
[ ]
[ ]
[ ]
Output
[ ]
[ ]

Check to see the current size of the dicom image

[ ]
(256, 256)
[ ]
(1792, 1792)
Output
[ ]
[ ]
(200, 200)
[ ]
[ ]
[ ]
Output
[ ]
[ ]

as_dict takes in 2 parameters: px_summ which by default is set to True and this returns additional stats such as minimal pixel value, maximum pixel value, the mean pixel value and the image standard deviation. The window parameter calculates the pct_in_window value depending on the window that is specified.

[ ]
[ ]
[ ]

Creating a dataframe of the values within the header of the dicom

[ ]
[ ]
[ ]
Output

Export -

[ ]
[ ]