title: Second pass of the two-pass algorithm (connected components 4- Connectivity)
body: <p>I have an assignment which aims to extracting the biggest object from a black and white image, where black is the background.I am using The 2-pass algorithm, here is a link that explains how it works:
  <a href="http://en.wikipedia.org/wiki/Connected-component_labeling#Two-pass" rel="nofollow">http://en.wikipedia.org/wiki/Connected-component_labeling#Two-pass</a></p>
  
  <p>My problems : 
  1.I am using an array of structure that I want to define its dimensions to be the same of the "input image" so { how can I do that }</p>
  
  <p>2.I made an array of two columns and number of rows as an equivalence table but I am not sure I am getting it right, how can I fix it ?</p>
  
  <ol start="3">
  <li>How can I use the equivalence table to "relabel the pixels in the second pass?how can I write the code of the second pass ?</li>
  </ol>
  
  <p>My code:</p>
  
  <pre><code>Image Image::MaxCC()
  {
      Image obj;
      obj.height = height;
      obj.width = width;
      short ** original = image;
      short ** output = new short*[height];
  
      for (int i = 0; i &lt; height; i++)
      {
          output[i] = new short[width];
      }
      obj.imageHeader = imageHeader;
      obj.image = output;
  
      //label array
      //structure 
      struct label
      {
          int lab;
          int counter;
      };
  
      label L[][]; //I want to use the dimensions of the input image which is obj.height and obj.width
  
      //Initialize 
  
      for (int i = 0; i &lt;= obj.height; i++)
      {
          for (int j = 0; j &lt;= obj.width; j++)
          {
              L[i][j].lab = 0;
              L[i][j].counter = 0;
          }
      }
       int N = 0;
       int count = 0;
  
       //equivlance tabel 
       int eq[100][2];
       int row = 1;
       int x = 1;
       int s;
  
  // conditions [FIRST ITERATION]
      for (int c = 0; c &lt; obj.width; c++)
      {
          for (int r = 0; r &lt; obj.height; r++)
          {
              // If the pixel is balck , add no label
              if (image[r][c] == 0)
                  obj.image[r][c] = 0;
  
              //Do the pixel's North and West neighbors have different pixel values than current pixel?
              else if (image[r - 1][c] == 0 &amp;&amp; image[r][c - 1] == 0)
              {
                  L[r][c].lab = N++;
                  L[r][c].counter = count++;
                  obj.image[r][c] = L[r][c].lab;
  
              }
  
              //Does the pixel to the left (West) have the same value as the current pixel?
              else if (image[r][c - 1] == image[r][c])
              {
                  L[r][c - 1].counter = count++;
                  obj.image[r][c] = L[r][c - 1].lab;
  
              }
  
              //Does the pixel to the left (West) have a different value and the one to the North the same value as the current pixel?
              else if (image[r - 1][c] == image[r][c] &amp;&amp; image[r][c - 1] != image[r][c])
              {
                  L[r - 1][c].counter = count++;
                  obj.image[r][c] = L[r - 1][c].lab;
              }
  
              //Do both pixels to the North and West have the same value as the current pixel but not the same label?
              else if (image[r - 1][c] == image[r][c] &amp;&amp; image[r][c - 1] == image[r][c] &amp;&amp; L[r - 1][c].lab != L[r][c - 1].lab)
              {
                  obj.image[r][c] = L[r - 1][c].lab;
                  eq[row][1] = x;
                  if (L[r - 1][c].counter &lt;&lt; L[r][c - 1].counter)
                  {
                      L[r - 1][c].counter = count++; 
                      s = L[r - 1][c].lab;
  
                  }
                  else
                  {
                      L[r][c - 1].counter = count++;
                      s = L[r][c - 1].lab;
                      eq[row][2] = s;  //..
                      x++; row++;
                  }
              }
  
          }
  
          //THE SECONF ITERATION ?
  
          //Iteration to get the maximum counter
          label max;
          int temp;
  
          for (int c = 0; c &lt; obj.width; c++)
          {
              for (int r = 0; r &lt; obj.height; c++)
              {
                  temp = L[r][c].counter;
                  if (temp &gt; max.counter)
                      max.counter = temp;
              }
          }
              //iteratio to extract the bigger object
              for (int c = 0; c &lt; obj.width; c++)
              {
                  for (int r = 0; r &lt; obj.height; c++)
                  {
                      if (max.lab == L[r][c].lab)
                          obj.image[r][c] = 255;
                      else
                          obj.image[r][c] = 0;
                  }
              }
  
      }
  
      return obj;
  
  }
  </code></pre>