0

I am trying to construct a quadtree, and having some difficulty. It is meant to read a binary image (handled elsewhere) and perform various operations. However, of course, first the quad tree must be construct. I wish to continue to subdivide the tree until all the pixels are one solid colour (black or white) for convenience of manipulation.

I have the following function, which simply calls a helper function handling the lengthy recursive process of building the tree.

void Quadtree::constructQuadtree(Image* mImage){

    if (mImage->imgPixels == 0) {
        return;
    }

    root = new QTNode(); 


    this->root = buildQTRecur(mImage, 0, 0, mImage->rows);

}

Here is the helper function that handles the bulk of the tree building:

QTNode* Quadtree::buildQTRecur(Image* mImage, int startRow, int startCol, int subImageDim) {

if (this->root == NULL) {
    return this->root;
}


 if (subImageDim >= 1) {

  int initialValue = 0;

  bool uniform = false;

  // Check to see if subsquare is uniformly black or white (or grey)

  for (int i = startRow; i < startRow + subImageDim; i++)
  {
     for (int j = startCol; j < startCol + subImageDim; j++)
     {
        if ((i == startRow) && (j == startCol))

           initialValue = mImage->imgPixels[i*mImage->rows+j];

        else {

           if (mImage->imgPixels[i*(mImage->rows)+j] != initialValue) {
              uniform = true;

              break;

           }
        }
     }
  }

  // Is uniform

  if (uniform) {

    this->root->value = initialValue; 

    this->root->NW = NULL;
    this->root->SE = NULL;
    this->root->SW = NULL;
    this->root->NE = NULL;

    return this->root;

   }

  else { // Division required - not uniform

     this->root->value = 2; //Grey node

     this->root->NW = new QTNode();
     this->root->NE = new QTNode();
     this->root->SE = new QTNode();
     this->root->SW = new QTNode();

     // Recursively split up subsquare into four smaller subsquares with dimensions half that of the original.

     this->root->NW = buildQTRecur(mImage, startRow, startCol, subImageDim/2); 
     this->root->NE = buildQTRecur(mImage, startRow, startCol+subImageDim/2, subImageDim/2); 
     this->root->SW = buildQTRecur(mImage, startRow+subImageDim/2, startCol, subImageDim/2); 
     this->root->SE = buildQTRecur(mImage, startRow+subImageDim/2, startCol+subImageDim/2, subImageDim/2); 

  }

 }

 return this->root;

}

I get stuck in an infinite loop when I try to run it. Please let me know if it would be helpful to see anything else, such as my node constructor, or any additional information to assist!

Thank you.

Rome_Leader
  • 2,518
  • 9
  • 42
  • 73
  • This is when the best way to get an answer is to debug. Set breakpoints, conditional breakpoints and investigate. – Csaba Toth Jul 22 '13 at 23:35

1 Answers1

0

I see several problems in your code:

  • Who is responsible to create the subnode ? If you write both

    this->root->NW = new QTNode();
    this->root->NW = buildQTRecur(mImage, startRow, startCol, subImageDim/2);
    

    then you fisrt allocate a new quadtree and then overwrite it.

  • You get the logic to compute uniform reversed.

  • If you find two different pixel, then you do a break. But it only get out the inner loop. You should consider putting this in a helper function and do a return here to get out of both loop at once.
  • for efficiency reason you shouldn't write

    if ((i == startRow) && (j == startCol))
         initialValue = mImage->imgPixels[i*mImage->rows+j];
    

    Just put

    initialValue = mImage->imgPixels[startRow*mImage->rows+startCol];
    

    before the loop

hivert
  • 10,579
  • 3
  • 31
  • 56