Helpers for working with DICOM files
from nbdev.showdoc import *
matplotlib.rcParams['image.cmap'] = 'bone'

Patching

Path.dcmread[source]

Path.dcmread()

# #export
# @patch
# def png16read(self:Path): return array(Image.open(self), dtype=np.uint16)
TEST_DCM = Path('images/sample.dcm')
dcm = TEST_DCM.dcmread()

pixels[source]

pixels()

pixel_array as a tensor

scaled_px[source]

scaled_px()

pixels scaled by RescaleSlope and RescaleIntercept

array_freqhist_bins[source]

array_freqhist_bins(n_bins=100)

A numpy based function to split the range of pixel values into groups, such that each group has around the same number of pixels

Tensor.freqhist_bins[source]

Tensor.freqhist_bins(n_bins=100)

A function to split the range of pixel values into groups, such that each group has around the same number of pixels

Tensor.hist_scaled_pt[source]

Tensor.hist_scaled_pt(brks=None)

Tensor.hist_scaled[source]

Tensor.hist_scaled(brks=None)

Dataset.hist_scaled[source]

Dataset.hist_scaled(brks=None, min_px=None, max_px=None)

Tensor.windowed[source]

Tensor.windowed(w, l)

Dataset.windowed[source]

Dataset.windowed(w, l)

class TensorCTScan[source]

TensorCTScan(x, **kwargs) :: TensorImageBW

class PILCTScan[source]

PILCTScan() :: PILBase

This class represents an image object. To create :py:class:~PIL.Image.Image objects, use the appropriate factory functions. There's hardly ever any reason to call the Image constructor directly.

  • :py:func:~PIL.Image.open
  • :py:func:~PIL.Image.new
  • :py:func:~PIL.Image.frombytes

show[source]

show(scale=True, cmap=<matplotlib.colors.LinearSegmentedColormap object at 0x7f9cd7bad610>, min_px=-1100, max_px=None, ax=None, figsize=None, title=None, ctx=None)

scales = False, True, dicom_windows.brain, dicom_windows.subdural
titles = 'raw','normalized','brain windowed','subdural windowed'
for s,a,t in zip(scales, subplots(2,2,imsize=4)[1].flat, titles):
    dcm.show(scale=s, ax=a, title=t)
dcm.show(cmap=plt.cm.gist_ncar, figsize=(6,6))

Dataset.pct_in_window[source]

Dataset.pct_in_window(dcm:Dataset, w, l)

% of pixels in the window (w,l)

dcm.pct_in_window(*dicom_windows.brain)
0.19049072265625

uniform_blur2d[source]

uniform_blur2d(x, s)

ims = dcm.hist_scaled(), uniform_blur2d(dcm.hist_scaled(),50)
show_images(ims, titles=('orig', 'blurred'))

gauss_blur2d[source]

gauss_blur2d(x, s)

Tensor.mask_from_blur[source]

Tensor.mask_from_blur(x:Tensor, window, sigma=0.3, thresh=0.05, remove_max=True)

Dataset.mask_from_blur[source]

Dataset.mask_from_blur(x:Dataset, window, sigma=0.3, thresh=0.05, remove_max=True)

mask = dcm.mask_from_blur(dicom_windows.brain)
wind = dcm.windowed(*dicom_windows.brain)

_,ax = subplots(1,1)
show_image(wind, ax=ax[0])
show_image(mask, alpha=0.5, cmap=plt.cm.Reds, ax=ax[0]);

mask2bbox[source]

mask2bbox(mask)

bbs = mask2bbox(mask)
lo,hi = bbs
show_image(wind[lo[0]:hi[0],lo[1]:hi[1]]);

crop_resize[source]

crop_resize(x, crops, new_sz)

px256 = crop_resize(to_device(wind[None]), bbs[...,None], 128)[0]
show_image(px256)
px256.shape
torch.Size([1, 128, 128])

Tensor.to_nchan[source]

Tensor.to_nchan(x:Tensor, wins, bins=None)

Dataset.to_nchan[source]

Dataset.to_nchan(x:Dataset, wins, bins=None)

Tensor.to_3chan[source]

Tensor.to_3chan(x:Tensor, win1, win2, bins=None)

Dataset.to_3chan[source]

Dataset.to_3chan(x:Dataset, win1, win2, bins=None)

show_images(dcm.to_nchan([dicom_windows.brain,dicom_windows.subdural,dicom_windows.abdomen_soft]))

Tensor.save_jpg[source]

Tensor.save_jpg(x:Dataset'>), path, wins, bins=None, quality=90)

Dataset.save_jpg[source]

Dataset.save_jpg(x:Dataset'>), path, wins, bins=None, quality=90)

Tensor.to_uint16[source]

Tensor.to_uint16(x:Dataset'>), bins=None)

Dataset.to_uint16[source]

Dataset.to_uint16(x:Dataset'>), bins=None)

Tensor.save_tif16[source]

Tensor.save_tif16(x:Dataset'>), path, bins=None, compress=True)

Dataset.save_tif16[source]

Dataset.save_tif16(x:Dataset'>), path, bins=None, compress=True)

_,axs=subplots(1,2)
with tempfile.TemporaryDirectory() as f:
    f = Path(f)
    dcm.save_jpg(f/'test.jpg', [dicom_windows.brain,dicom_windows.subdural])
    show_image(Image.open(f/'test.jpg'), ax=axs[0])
    dcm.save_tif16(f/'test.tif')
    show_image(Image.open(str(f/'test.tif')), ax=axs[1]);

Dataset.set_pixels[source]

Dataset.set_pixels(px)

Dataset.zoom[source]

Dataset.zoom(ratio)

Dataset.zoom_to[source]

Dataset.zoom_to(sz)

shape[source]

shape(x:Image)

dcm2 = TEST_DCM.dcmread()
dcm2.zoom_to(90)
test_eq(dcm2.shape, (90,90))
dcm2 = TEST_DCM.dcmread()
dcm2.zoom(0.25)
dcm2.show()

Dataset.as_dict[source]

Dataset.as_dict(px_summ=True, window=(80, 40))