title: How to find all connected components in a binary image in Matlab?
body: <p>I have been trying to find all connected components using 8 neighbors in a binary image, without using the function "bwlabel".</p>
  
  <p>For example, my input matrix is:</p>
  
  <pre><code>a =
  
       1     1     0     0     0     0     0
       1     1     0     0     1     1     0
       1     1     0     0     0     1     0
       1     1     0     0     0     0     0
       0     0     0     0     0     1     0
       0     0     0     0     0     0     0
  </code></pre>
  
  <p>I would to have something like this:</p>
  
  <pre><code>a =
  
       1     1     0     0     0     0     0
       1     1     0     0     2     2     0
       1     1     0     0     0     2     0
       1     1     0     0     0     0     0
       0     0     0     0     0     3     0
       0     0     0     0     0     0     0
  </code></pre>
  
  <p>There are 3 connected objects in this image.</p>