title: Python: Intersection over Union
body: <p>I have the following question. I try to calculate the Intersection over Union, which is the overlap of two components divided by the unioin of two components.  Lets assume component1 is a matrix with ones where the first object is and component2 is a matrix with ones where the second object is. The Overlap i can caluclate with <code>np.logical_and(component == 1, component2 == 1)</code>. But how can I caclulate the Union? Im only interested in objects which are connected.</p>
  
  <pre><code>import numpy as np
  component1 = np.array([[0,1,1],[0,1,1],[0,1,1]])
  component2 = np.array([[1,1,0],[1,1,0],[1,1,0]])
  overlap = np.logical_and(component == 1, component2 == 1)
  union = ?
  IOU = len(overlap)/len(union)
  </code></pre>