Pydicom是一个用于处理DICOM格式文件的Python包,可以处理包括如医学图像(CT等)、报告等。Window Center 又叫 Window Level 代表可视范围(或是ROI)的 CT 值范围中心,Window Width 则是可视范围大小
dcm=dicom.read_file(path)
print(dcm)
print(dcm.WindowWidth)
print(dcm.WindowCenter)
print(dcm.RescaleSlope)
print(dcm.RescaleIntercept)
def get_pixels_hu(ct_array,slope,intercept):
"""
某些扫描仪具有圆柱扫描边界,但输出图像为方形。 落在这些边界之外的像素获得固定值-2000。 第一步是将这些值设置为0,当前对应于air。 接下来,回到HU单位,乘以重新缩放斜率并添加截距
HU = pixel_val*slope+ intercept
其中,slope,intercept可以从元数据中读取
窗宽窗位是CT图像特有的概念,MRI图像中可没有此概念;
CT 图像必须先转换成 HU值再做窗宽窗位调整。
:param ct_array:
:param slope:
:param intercept:
:return:CT图像的HU值
"""
ct_array = ct_array.astype(np.int16)
# 设置边界外的元素为0
ct_array[ct_array == -2000] = 0
if slope != 1:
ct_array = slope * ct_array.astype(np.float64)
ct_array = ct_array.astype(np.int16)
ct_array += np.int16(intercept)
print(ct_array)
return ct_array
def CT_lower_upper(WL,WW):
"""
:param WL: 窗位
:param WW: 窗宽
:return: 可视范围
"""
lower = WL - WW/2
upper = WL + WW/2
return lower,upper
def convert_dcm2jpg(array,lower,upper,save_path):
# 读取dcm文件
print(np.max(array))
# print(img_array.shape)
# print(img_array)
#将像素值转换到0-255之间
img_array = (array - lower) / (upper - lower)
img_array [img_array > 1] = 1
img_array [img_array < 0] = 0
img= (img_array * 255).astype('uint8')
#大于255
#小于0
print(np.max(img_array))
cv2.imwrite(save_path, img)
倘若图像矩阵的矩阵元素不在0-1之间,那么imwrite会把超过1的元素都显示为h黑色,需注意。