Helpers for working with DICOM files
# @patch
# def png16read(self:Path): return array(Image.open(self), dtype=np.uint16)
TEST_DCM = Path('images/sample.dcm')
dcm = TEST_DCM.dcmread()
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))
dcm.pct_in_window(*dicom_windows.brain)
ims = dcm.hist_scaled(), uniform_blur2d(dcm.hist_scaled(),50)
show_images(ims, titles=('orig', 'blurred'))
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]);
bbs = mask2bbox(mask)
lo,hi = bbs
show_image(wind[lo[0]:hi[0],lo[1]:hi[1]]);
px256 = crop_resize(to_device(wind[None]), bbs[...,None], 128)[0]
show_image(px256)
px256.shape
show_images(dcm.to_nchan([dicom_windows.brain,dicom_windows.subdural,dicom_windows.abdomen_soft]))
_,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]);
dcm2 = TEST_DCM.dcmread()
dcm2.zoom_to(90)
test_eq(dcm2.shape, (90,90))
dcm2 = TEST_DCM.dcmread()
dcm2.zoom(0.25)
dcm2.show()