title: Binarize image data
body: <p>I have 10 greyscale brain MRI scans from <a href="http://brainweb.bic.mni.mcgill.ca/brainweb/" rel="nofollow noreferrer">BrainWeb</a>. They are stored as a 4d numpy array, <code>brains</code>, with shape <code>(10, 181, 217, 181)</code>. Each of the 10 brains is made up of 181 slices along the z-plane (going through the top of the head to the neck) where each slice is 181 pixels by 217 pixels in the x (ear to ear) and y (eyes to back of head) planes respectively.</p>
  
  <p>All of the brains are type <code>dtype('float64')</code>. The maximum pixel intensity across all brains is <code>~1328</code> and the minimum is <code>~0</code>. For example, for the first brain, I calculate this by <code>brains[0].max()</code> giving <code>1328.338086605072</code> and <code>brains[0].min()</code> giving <code>0.0003886114541273855</code>. Below is a plot of a slice of a <code>brain[0]</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/d5E9i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/d5E9i.png" alt="enter image description here"></a></p>
  
  <p><strong>I want to binarize all these brain images by rescaling the pixel intensities from <code>[0, 1328]</code> to <code>{0, 1}</code></strong>. <strong>Is my method correct?</strong></p>
  
  <p>I do this by first normalising the pixel intensities to <code>[0, 1]</code>: </p>
  
  <pre><code>normalized_brains = brains/1328 
  </code></pre>
  
  <p>And then by using the binomial distribution to binarize each pixel:</p>
  
  <pre><code>binarized_brains = np.random.binomial(1, (normalized_brains))
  </code></pre>
  
  <p>The plotted result looks correct:</p>
  
  <p><a href="https://i.stack.imgur.com/Wfq4X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wfq4X.png" alt="enter image description here"></a></p>
  
  <p>A 0 pixel intensity represents black (background) and 1 pixel intensity represents white (brain).</p>
  
  <p>I experimented by implementing another method to normalise an image from <a href="https://stackoverflow.com/questions/46689428/convert-np-array-of-type-float64-to-type-uint8-scaling-values">this post</a> but it gave me just a black image. This is because <code>np.finfo(np.float64)</code> is <code>1.7976931348623157e+308</code>, so the normalization step</p>
  
  <pre><code>normalized_brains = brains/1.7976931348623157e+308
  </code></pre>
  
  <p>just returned an array of zeros which in the binarizition step also led to an array of zeros.</p>
  
  <p><strong>Am I binarising my images using a correct method?</strong></p>