title: How to find the largest connected region using scipy.ndimage?
body: <p>I have a binary image. The binary image has some isolate regions like noise. I know that the expected region much larger than these isolate regions. Hence, I used the connected components to remove the isolate regions by finding the largest connected region. I have to use scipy package. I found it has some functions to do it. However, I still am in far away the result. How can I use the functions to obtain a binary image that can ignore the isolated region? Thanks</p>
  
  <pre><code>from scipy import ndimage
  label_im, nb_labels = ndimage.label(binary_img)
  
  # Find the largest connected component
  sizes = ndimage.sum(binary_img, label_im, range(nb_labels + 1))
  mask_size = sizes &lt; 1000
  remove_pixel = mask_size[label_im]
  label_im[remove_pixel] = 0
  labels = np.unique(label_im)
  binary_img= np.searchsorted(labels, label_im)
  #Select the biggest connected component
  binary_img[binary_img &lt; binary_img.max()]=0
  binary_img[binary_img &gt;= binary_img.max()]=1
  </code></pre>