title: MRI (brain tumor) image processing and segmentation, skull removing
body: <p>I need help for image segmentation. I have a MRI image of brain with tumor. I need to remove cranium (skull) from MRI and then segment only tumor object. How could I do that in python? with image processing. I have tried make contours, but I don't know how to find and remove the largest contour and get only brain without a skull. 
  Thank's a lot.</p>
  
  <pre><code>def get_brain(img):
    row_size = img.shape[0]
    col_size = img.shape[1]
  
    mean = np.mean(img)
    std = np.std(img)
    img = img - mean
    img = img / std
  
   middle = img[int(col_size / 5):int(col_size / 5 * 4), int(row_size / 5):int(row_size / 5 * 4)]
    mean = np.mean(middle)
    max = np.max(img)
    min = np.min(img)
  
  
    img[img == max] = mean
    img[img == min] = mean
  
    kmeans = KMeans(n_clusters=2).fit(np.reshape(middle, [np.prod(middle.shape), 1]))
    centers = sorted(kmeans.cluster_centers_.flatten())
    threshold = np.mean(centers)
    thresh_img = np.where(img &lt; threshold, 1.0, 0.0)  # threshold the image
  
  
    eroded = morphology.erosion(thresh_img, np.ones([3, 3]))
    dilation = morphology.dilation(eroded, np.ones([5, 5]))
  </code></pre>
  
  <p>These images are similar to the ones I'm looking at:</p>
  
  <p><img src="https://static1.squarespace.com/static/5443d7c7e4b06e8b47de9a55/t/59c165c8cf81e0541542e70e/1505846731211/" alt="Skull + Brain"></p>
  
  <p><a href="https://i.stack.imgur.com/FHgVm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FHgVm.png" alt="Skull + Brain 2"></a></p>
  
  <p>Thanks for answers.</p>