bool UFUnion(int p, int q)
 {
     int i = UFFind(p), j = UFFind(q);
     if (i == j) return false;
     if (size[i] > size[j])
         id[j] = i;
     else if (size[i] < size[j])
         id[i] = j;
     else
     {
         id[j] = i;
         size[i]++;
     }
     count--;
     return true;
 }
int RemoveBackground(Image *thresholded_image)
{

  // Dimension Space for label image and LabelSets
  Image *label_image = malloc(sizeof(Image)) ;
  UnionFind *LabelSets = malloc(sizeof(UnionFind)) ;

  if(LabelSets == NULL || label_image == NULL)
    {
      return(0) ;
    }

  // Create the Union Find to have 255 distinct sets
  UFCreate(255,LabelSets) ;

  int A[4],i,j,k,row,col ;

  GetSize(thresholded_image,&row,&col) ;
  SetSize(label_image,row,col) ;

  // Initialize the label image to 0
  Color clr,pixel ;
  for(i=0;i<row;i++)
    {
      for(j=0;j<col;j++)
	{
	  clr.red=0 ;
	  clr.blue=0 ;
	  clr.green=0 ;
	  SetPixel(label_image,i,j,clr) ;
	}
    }
  
  // LabelCounter will count how many labels I have set
  int LabelCounter=1 ;

  // Loop over the inner rows and columns
  for(i=1;i<row-1;i++)
    {
      for(j=1;j<col-1;j++)
	{
	  // For each inner pixel, check to see if it is a 
          // background pixel
	  clr = GetPixel(thresholded_image,i,j) ;

	  if(clr.red != 0)
	    {

	      // If it then get the labels of the pixels in the
	      // 1,2,3,4 positions. These get stored in the A array.
	      pixel=GetPixel(label_image,i-1,j-1) ;
	      A[0]=pixel.red ;
	      
	      pixel=GetPixel(label_image,i-1,j) ;
	      A[1]=pixel.red ;

	      pixel=GetPixel(label_image,i-1,j+1) ;
	      A[2]=pixel.red ;

	      pixel=GetPixel(label_image,i,j-1) ;
	      A[3]=pixel.red ;

	      // Now count how many zero labels you have and find the 
	      // minimum non-zero label.
	      int zerocount=0 ;
	      int minval = 255 ;

	      for(k=0;k<4;k++)
		{
		  if(A[k]==0)
		    {
		      zerocount++ ;
		    }
		  else
		    {
		      if(A[k] < minval)
			{
			  minval = A[k] ;
			}
		    }
		}

	      // if all the labels are zero, then the i,jth pixel is 
	      // set to a new label
	      if(zerocount==4)
		{
		  pixel.red = LabelCounter ;
		  SetPixel(label_image,i,j,pixel) ;
		  LabelCounter++ ;
		}
	      else
		{

		  // if there are non-zero labels then set the pixel's
		  // label to the smallest non-zero one. Union the sets
		  // as you go also.
		  pixel.red = minval ;
		  SetPixel(label_image,i,j,pixel) ;
		  for(k=0;k<4;k++)
		    {
		      if(A[k] != 0 && A[k] != minval)
			{
			  UFUnion(A[k],minval,LabelSets) ;
			}
		    }
		}
	      
	    }
	}
    }

  // Find which label each pixel should be. Keep a count of how many of
  // each label are used
  int label[255] ;
  for(i=0;i<255;i++)
    {
      label[i]=0 ;
    }

  for(i=0;i<row;i++)
    {
      for(j=0;j<col;j++)
	{
	  pixel = GetPixel(label_image,i,j) ;
	  if(pixel.red != 0)
	    {

	      label[UFFind(pixel.red, LabelSets)]++ ;
	      pixel.red = (5*(1+UFFind(pixel.red, LabelSets)))%255 ;
	      pixel.green = (5*pixel.red)%255 ;
	      pixel.blue = (10*pixel.red)%255 ;
	      SetPixel(label_image, i, j, pixel) ;
	    }
	}
    }

  // Which label represents the majority of non-background pixels
  int max = 0 ;
  int maxi = -1 ;
  int secondi = -1 ;

  for(i=0;i<255;i++)
    {
      if(label[i] > max)
	{
	  secondi = maxi ;
	  max = label[i] ;
	  maxi = i ;
	}
    }

  // Write out the color if it is not the largest label or if it is a 
  // background pixel.
  secondi = (5*(1+maxi))%255 ;
  for(i=0;i<row;i++)
    {
      for(j=0;j<col;j++)
	{
	  pixel=GetPixel(label_image,i,j) ;
	  if(pixel.red != secondi && pixel.red != 0)
	    {
	      pixel.red=0 ;
	      pixel.blue=0 ;
	      pixel.green=0 ;
	      SetPixel(thresholded_image, i, j, pixel) ;
	    }
	  else
	    {
	      pixel.red=255 ;
	      pixel.blue=255 ;
	      pixel.green=255 ;
	      SetPixel(thresholded_image, i, j, pixel) ;
	    }
	}
    }

  // free up some memory declared here.
  free(label_image) ;
  free(LabelSets) ;

  return(1) ;
}
 int UFFind(int p)
 {
     if (p == id[p]) return;
     return id[p] = UFFind(id[p]);
 }