法泰 .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
.
TEST_DCM = Path('images/sample.dcm')
dcm = TEST_DCM.dcmread ()
dcm
type(dcm)
dcm.pixels
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
.
plt.hist(dcm.pixels.flatten () .numpy () );
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
将通过这些值缩放像素。
plt.hist(dcm.scaled_px.flatten () .numpy () );
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
.
t_bin = dcm.pixels.freqhist_bins(n_bins=1)
t_bin
plt.hist(t_bin.numpy () , bins=t_bin, color='c')
plt.plot(t_bin, torch.linspace(0,1,len(t_bin)));
with n_bins
at 100
t_bin = dcm.pixels.freqhist_bins(n_bins=100)
t_bin
plt.hist(t_bin.numpy () , bins=t_bin, color='c'); plt.plot(t_bin, torch.linspace(0,1,len(t_bin)));
The test image has pixel values that range between -1000
and 2500
plt.hist(dcm.pixels.flatten () .numpy () , bins=100);
hist_scaled
provides a way of scaling the input pixel values to between 0
and 1
tensor_hists = dcm.pixels.hist_scaled ()
plt.hist(tensor_hists.flatten () .numpy () , bins=100);
data_scaled = dcm.hist_scaled ()
plt.imshow(data_scaled, cmap=plt.cm.bone);
data_scaled = dcm.hist_scaled(min_px=100, max_px=1000)
plt.imshow(data_scaled, cmap=plt.cm.bone);
Dicom图像可能包含大量的体素值,并且可以将开窗视为一种操纵这些值以改变图像外观的方法,以便突出显示特定的结构。一个窗口有两个值:
l
=窗口水平或中心又称为亮度w
=窗口宽度或范围,也称为对比度
plt.imshow(dcm.windowed(*dicom_windows.brain), cmap=plt.cm.bone);
tensor_ct = TensorCTScan(dcm.pixel_array)
tensor_ct.show () ;
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))
一些dicom数据集,例如 超声数据集中的甲状腺分割 is a dataset where each image has multiple frames per file (hundreds in this case). By default the show
功能 will display 1 frame but if the dataset has multiple frames you can specify the number of frames to view.
dcm.show ()
dcm.pct_in_window(*dicom_windows.brain)
pct_in_window
可用于检查由有意义的像素(指定窗口内的像素)组成的图像百分比
ims = dcm.hist_scaled () , uniform_blur2d(dcm.hist_scaled () , 20), uniform_blur2d(dcm.hist_scaled () , 50)
show_images(ims, titles=('original', 'blurred 20', 'blurred 50'))
ims = dcm.hist_scaled () , gauss_blur2d(dcm.hist_scaled () , 20), gauss_blur2d(dcm.hist_scaled () , 50)
show_images(ims, titles=('original', 'gauss_blur 20', 'gauss_blur 50'))
图像通常受强度值随机变化(称为噪声)的影响。高斯噪声包含从高斯分布或正态分布得出的强度变化。高斯滤镜通常用于模糊边缘并去除较小或较薄的区域,以保留最重要的信息
mask = dcm.mask_from_blur(dicom_windows.brain, sigma=0.9, thresh=0.1, remove_max=True)
wind = dcm.windowed(*dicom_windows.brain)
_,ax = subplots(1,3)
show_image(wind, ax=ax[0], title='window')
show_image(mask, alpha=0.5, cmap=plt.cm.Reds, ax=ax[1], title='mask')
show_image(wind, ax=ax[2])
show_image(mask, alpha=0.5, cmap=plt.cm.Reds, ax=ax[2], title='window and mask');