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 code:
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 code:
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));
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)?
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];
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]])
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
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)