title: Difference between the functions "im2uint8" (in MATLAB) and "bytescale" (in Python)
body: <p>I want to convert a DICOM image from int16 to uint8. I have done it in Python using <code>Z_axis = bytescale(img)</code>, but this gives different results than using <code>im2uint8</code> in MATLAB. In MATLAB, The minimum and maximum values of a DICOM image after converting to uint8 using <code>im2uint8</code> are (124, 136), respectively. But these values in Python after converting using <code>bytescale</code> are (0, 255).</p>
  
  <p>Python code:</p>
  
  <pre><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()
  </code></pre>
  
  <p>Matlab code:</p>
  
  <pre><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));
  </code></pre>
  
  <p>The images look equal when displayed, but the numeric values are not. So, are the <code>bytescale</code> and <code>im2uint8</code> functions equal or not? If not, I want results like <code>im2uint8</code> in Python. What kind of function should I choose (especially for DICOM images)?</p>
  
  <p>For example, in MATLAB after reading a DICOM file:</p>
  
  <pre><code>img = dicomread([galleryPath Files2(j).name]);
  img = [ -1024,   -1024,   16;
          -1024,       8,   11;
             17,       5,    8];
  </code></pre>
  
  <p>But in Python, the same image after reading is:</p>
  
  <pre><code>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]])
  </code></pre>
  
  <p>I don't know why they are different in MATLAB and Python. After applying <code>im2uint8</code> in MATLAB, the output is:</p>
  
  <pre><code>Z_axis = im2uint8(img)
  Z_axis =
   3×3 uint8 matrix
     124    124   128
     124    128   128
     128    128   128
  </code></pre>
  
  <p>And after applying <code>bytescale</code> in Python, the output is:</p>
  
  <pre><code>bytescale(img)
  Z_axis = 
      array([[0,    0,   83],
             [0,   83,   83],
             [83,  83,   83]], dtype=uint8)
  </code></pre>