public static Utils.RectData[] TwoPass(Bitmap labels, Bitmap srcBitmap, Tree root, int background)
        {
            DisjointSets linked = new DisjointSets(root.Width * root.Height);
            int          bottom = root.Top + root.Height;
            int          right  = root.Left + root.Width;

            int numelements = IdentifyComponents(labels, srcBitmap, linked, root, root.GetHashCode(), bottom, right, background);

            //Run the second pass through the image to label each component
            //and create bounding rectangles
            LabelComponentsAndSetBoundingBoxes(labels, linked, root.Top, root.Left, bottom, right, background);


            return(GetRectsFromLabeledImage(labels, srcBitmap, numelements, root.Top, root.Left, bottom, right, background));
        }
		public static Utils.RectData[] TwoPass(Bitmap labels, Bitmap srcBitmap, Tree root, int background)
		{
			DisjointSets linked = new DisjointSets(root.Width * root.Height);
			int bottom = root.Top + root.Height;
			int right = root.Left + root.Width;

			int numelements = IdentifyComponents(labels, srcBitmap, linked, root, root.GetHashCode(), bottom, right, background);

			//Run the second pass through the image to label each component
			//and create bounding rectangles
			LabelComponentsAndSetBoundingBoxes(labels, linked, root.Top, root.Left, bottom, right, background);


			return GetRectsFromLabeledImage(labels, srcBitmap, numelements, root.Top, root.Left, bottom, right, background);
		}
 private static void LabelComponentsAndSetBoundingBoxes(Bitmap labels, DisjointSets linked, int top, int left, int bottom, int right, int background)
 {
     //Run the second pass through the image to label each component
     //and create bounding rectangles
     for (int row = top; row < bottom; row++)
     {
         for (int col = left; col < right; col++)
         {
             if (labels[row, col] != background)
             {
                 int id = linked.Find(labels[row, col]);
                 labels[row, col] = id;
             }
         }
     }
 }
		private static void LabelComponentsAndSetBoundingBoxes(Bitmap labels, DisjointSets linked, int top, int left, int bottom, int right, int background)
		{
			//Run the second pass through the image to label each component
			//and create bounding rectangles
			for (int row = top; row < bottom; row++)
			{
				for (int col = left; col < right; col++)
				{
					if (labels[row, col] != background)
					{
						int id = linked.Find(labels[row, col]);
						labels[row, col] = id;
					}
				}
			}
		}
		private static int IdentifyComponents(Bitmap labels, Bitmap srcBitmap, DisjointSets linked, Tree toProcess, int treeNodeIndex, int bottom, int right, int background)
		{
			int numElements = 0;

			//Run the first pass through the image to find any foreground content
			int[] neighbors = new int[8];
			for (int row = toProcess.Top; row < bottom; row++)
			{
				for (int col = toProcess.Left; col < right; col++)
				{
					int sourcepixel = srcBitmap[row, col];

					if (sourcepixel == treeNodeIndex)
					{
						int neighborCount = NeighborsConnected8(neighbors, toProcess, row, col, labels, background);
						if (neighborCount == 0)
						{

							labels[row, col] = numElements+1;
							numElements++;

						}
						else
						{
							int min = Min(neighbors, neighborCount);
							labels[row, col] =  min;
							for(int i = 0; i < neighborCount; i++)
							{
								linked.union(min, neighbors[i]);

							}
						}

					}
					else
						labels[row, col] = background;
				}
			}

			return numElements;

		}
        private static int IdentifyComponents(Bitmap labels, Bitmap srcBitmap, DisjointSets linked, Tree toProcess, int treeNodeIndex, int bottom, int right, int background)
        {
            int numElements = 0;

            //Run the first pass through the image to find any foreground content
            int[] neighbors = new int[8];
            for (int row = toProcess.Top; row < bottom; row++)
            {
                for (int col = toProcess.Left; col < right; col++)
                {
                    int sourcepixel = srcBitmap[row, col];

                    if (sourcepixel == treeNodeIndex)
                    {
                        int neighborCount = NeighborsConnected8(neighbors, toProcess, row, col, labels, background);
                        if (neighborCount == 0)
                        {
                            labels[row, col] = numElements + 1;
                            numElements++;
                        }
                        else
                        {
                            int min = Min(neighbors, neighborCount);
                            labels[row, col] = min;
                            for (int i = 0; i < neighborCount; i++)
                            {
                                linked.union(min, neighbors[i]);
                            }
                        }
                    }
                    else
                    {
                        labels[row, col] = background;
                    }
                }
            }

            return(numElements);
        }