函数"im2uint8"之间的区别在于:(在MATLAB中)和"bytescale"(在Python中) [英] Difference between the functions "im2uint8" (in MATLAB) and "bytescale" (in Python)
问题描述
我想将DICOM图像从int16转换为uint8.我已经在Python中使用 Z_axis = bytescale(img)
完成了此操作,但这与在MATLAB中使用 im2uint8
产生了不同的结果.在MATLAB中,使用 im2uint8
转换为uint8后,DICOM图像的最小值和最大值分别为(124,136).但是在使用 bytescale
进行转换后,Python中的这些值为(0,255).
I want to convert a DICOM image from int16 to uint8. I have done it in Python using Z_axis = bytescale(img)
, but this gives different results than using im2uint8
in MATLAB. In MATLAB, The minimum and maximum values of a DICOM image after converting to uint8 using im2uint8
are (124, 136), respectively. But these values in Python after converting using bytescale
are (0, 255).
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代码:
for j = 1 : length(Files2)
img = dicomread([galleryPath Files2(j).name]);
Z_axis = im2uint8(img);
minVal = min(min(Z_axis));
maxVal = max(max(Z_axis));
图像在显示时看起来是相等的,但数值却不相等.那么, bytescale
和 im2uint8
函数是否相等?如果没有,我想要Python中的 im2uint8
之类的结果.我应该选择哪种功能(特别是对于DICOM图像)?
The images look equal when displayed, but the numeric values are not. So, are the bytescale
and im2uint8
functions equal or not? If not, I want results like im2uint8
in Python. What kind of function should I choose (especially for DICOM images)?
例如,在MATLAB中读取DICOM文件后:
For example, in MATLAB after reading a DICOM file:
img = dicomread([galleryPath Files2(j).name]);
img = [ -1024, -1024, 16;
-1024, 8, 11;
17, 5, 8];
但是在Python中,读取后的图像是:
But in Python, the same image after reading is:
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
后,输出为:
I don't know why they are different in MATLAB and Python. After applying im2uint8
in MATLAB, the output is:
Z_axis = im2uint8(img)
Z_axis =
3×3 uint8 matrix
124 124 128
124 128 128
128 128 128
然后在Python中应用 bytescale
后,输出为:
And after applying bytescale
in Python, the output is:
bytescale(img)
Z_axis =
array([[0, 0, 83],
[0, 83, 83],
[83, 83, 83]], dtype=uint8)
推荐答案
首先,关于读取数据的问题,我建议使用 dicomread
.
Firstly, regarding the issue with reading the data, I would suggest using dcmread
in Python, as that gave me the same exact data as dicomread
in MATLAB.
其次,在MATLAB中,当 im2uint8
转换 int16
的值,它将假定分别等于-32768和32767的数据的最小值和最大值(即,由 int16
表示的最小值和最大值)来缩放它们).对于 bytescale
> 具有相同的行为,我相信您需要相应地设置 cmin
和 cmax
参数(因为否则它们将默认为 data.min()
和 data.max()
).这应该在Python中复制 im2uint8
的结果:
Secondly, in MATLAB when im2uint8
converts int16
values it will scale them assuming minimum and maximum values for the data equal to -32768 and 32767, respectively (i.e. the minimum and maximum values representable by an int16
). For bytescale
to behave equivalently, I believe you need to set the cmin
and cmax
arguments accordingly (since they will otherwise default to data.min()
and data.max()
, respectively). This should replicate the results of im2uint8
in Python:
Z_axis = bytescale(img.astype(float), cmin=-32768, cmax=32767)
注意:必须先将数据转换为浮点数,以解决 bytescale
中的明显错误,该错误无法正确处理整数算术(由
Note: conversion of the data to float first is necessary to account for an apparent bug in bytescale
that doesn't handle the integer arithmetic properly (found courtesy of Cris Luengo).
这篇关于函数"im2uint8"之间的区别在于:(在MATLAB中)和"bytescale"(在Python中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!