我想把DICOM图像从int16转换成uint8。我在Python中使用Z_axis = bytescale(img)完成了这项工作,但这与在MATLAB中使用im2uint8得到的结果不同。在MATLAB中,使用im2uint8转换成uint8后的DICOM图像的最小值和最大值分别为(124,136)。但是在Python中,使用bytescale进行转换后,这些值是(0255)。在
Python代码:for person in range(0, len(dirs1)):
if not os.path.exists(os.path.join(directory, dirs1[person])):
Pathnew = os.path.join(directory, dirs1[person])
os.makedirs(Pathnew)
for root, dirs, files in os.walk(os.path.join(path, dirs1[person])):
dcmfiles = [_ for _ in files if _.endswith('.dcm')]
for dcmfile in dcmfiles:
dcm_image = pydicom.read_file(os.path.join(root, dcmfile))
img = dcm_image.pixel_array
Z_axis = bytescale(img)
minVal = Z_axis.min()
maxVal = Z_axis.max()
Matlab代码:
^{pr2}$
显示时图像看起来是相等的,但数值不是。那么,bytescale和{}函数是否相等?如果不是,我想要像Python中的im2uint8这样的结果。我应该选择什么样的功能(尤其是DICOM图像)?在
例如,在MATLAB中读取DICOM文件后:img = dicomread([galleryPath Files2(j).name]);
img = [ -1024, -1024, 16;
-1024, 8, 11;
17, 5, 8];
但在Python中,阅读后的相同图像是:dcm_image = pydicom.read_file(os.path.join(root, dcmfile))
img = dcm_image.pixel_array
img = array([[ -1024, -1024, 27],
[ -1024, 27, 26],
[ 24, 26, 23]])
我不知道为什么在MATLAB和Python中它们是不同的。在MATLAB中应用im2uint8后,输出为:Z_axis = im2uint8(img)
Z_axis =
3×3 uint8 matrix
124 124 128
124 128 128
128 128 128
在Python中应用bytescale后,输出是:bytescale(img)
Z_axis =
array([[0, 0, 83],
[0, 83, 83],
[83, 83, 83]], dtype=uint8)