0

四分木を構築しようとしていますが、問題があります。バイナリ イメージ (別の場所で処理) を読み取り、さまざまな操作を実行するためのものです。ただし、もちろん、最初に四分木を構築する必要があります。操作しやすいように、すべてのピクセルが単色 (黒または白) になるまでツリーを分割し続けたいと思います。

次の関数があります。これは、ツリーを構築するための長い再帰プロセスを処理するヘルパー関数を呼び出すだけです。

void Quadtree::constructQuadtree(Image* mImage){

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

    root = new QTNode(); 


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

}

ツリー構築の大部分を処理するヘルパー関数は次のとおりです。

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;

}

実行しようとすると無限ループに陥ります。ノード コンストラクターやその他の支援情報など、他に役立つ情報があればお知らせください。

ありがとうございました。

4

1 に答える 1