函数"im2uint8"(在MATLAB中)和"bytescale"(在Python中)之间的区别

Ell*_*lie 5 python matlab dicom

我想将DICOM图像从int16转换为uint8.我已经在Python中使用了它Z_axis = bytescale(img),但是这给出了与im2uint8在MATLAB中使用不同的结果.在MATLAB中,使用uint8转换后的DICOM图像的最小值和最大值im2uint8分别为(124,136).但是使用转换后Python中的这些值bytescale是(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()
Run Code Online (Sandbox Code Playgroud)

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));
Run Code Online (Sandbox Code Playgroud)

显示时图像看起来相同,但数值不是.那么,是bytescaleim2uint8功能等于或不?如果没有,我想要像im2uint8Python 一样的结果.我应该选择什么样的功能(特别是对于DICOM图像)?

例如,在读取DICOM文件后的MATLAB中:

img = dicomread([galleryPath Files2(j).name]);
img = [ -1024,   -1024,   16;
        -1024,       8,   11;
           17,       5,    8];
Run Code Online (Sandbox Code Playgroud)

但在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]])
Run Code Online (Sandbox Code Playgroud)

我不知道他们为什么在MATLAB和Python中有所不同.im2uint8在MATLAB中应用后,输出为:

Z_axis = im2uint8(img)
Z_axis =
 3×3 uint8 matrix
   124    124   128
   124    128   128
   128    128   128
Run Code Online (Sandbox Code Playgroud)

bytescale在Python中应用后,输出为:

bytescale(img)
Z_axis = 
    array([[0,    0,   83],
           [0,   83,   83],
           [83,  83,   83]], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)

gno*_*ice 4

首先,关于读取数据的问题,我建议在Python中使用,因为这给了我与MATLAB中dcmread相同的精确数据。dicomread

其次,在 MATLAB 中,当im2uint8转换int16值时,它会缩放它们,假设数据的最小值和最大值分别等于 -32768 和 32767(即由 表示的最小值和最大值int16)。为了bytescale表现等效,我相信您需要相应地设置cmincmax参数(因为否则它们将分别默认为data.min()data.max())。这应该复制 Python 中的结果im2uint8

Z_axis = bytescale(img.astype(float), cmin=-32768, cmax=32767)
Run Code Online (Sandbox Code Playgroud)

注意:首先需要将数据转换为浮点数,以解决无法正确处理整数算术的明显错误(由Cris Luengobytescale提供)。