C++ Can';t构造四叉树?
我正在尝试构造一个四叉树,但遇到了一些困难。它用于读取二进制图像(在别处处理)并执行各种操作。当然,首先必须构造四叉树。我希望继续细分树,直到所有像素都是一种纯色(黑色或白色),以便操作 我有下面的函数,它只调用一个助手函数来处理构建树的冗长递归过程C++ Can';t构造四叉树?,c++,quadtree,C++,Quadtree,我正在尝试构造一个四叉树,但遇到了一些困难。它用于读取二进制图像(在别处处理)并执行各种操作。当然,首先必须构造四叉树。我希望继续细分树,直到所有像素都是一种纯色(黑色或白色),以便操作 我有下面的函数,它只调用一个助手函数来处理构建树的冗长递归过程 void Quadtree::constructQuadtree(Image* mImage){ if (mImage->imgPixels == 0) { return; } root = new
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;
}
QTNode*四叉树::buildQTRecur(图像*mImage,int startRow,int startCol,int subImageDim){
如果(此->根==NULL){
返回此->根目录;
}
如果(子图像尺寸>=1){
int initialValue=0;
布尔一致=假;
//检查子正方形是否均匀为黑色或白色(或灰色)
对于(int i=startRow;iimgPixels[i*mImage->rows+j];
否则{
if(mImage->imgPixels[i*(mImage->rows)+j]!=初始值){
一致=正确;
打破
}
}
}
}
//是统一的
国际单项体育联合会(制服){
此->根->值=初始值;
此->根->NW=NULL;
此->根->SE=NULL;
此->根->SW=NULL;
此->根->NE=NULL;
返回此->根目录;
}
else{//需要划分-不统一
此->根->值=2;//灰色节点
此->根->NW=新QTNode();
此->根->NE=新QTNode();
此->根->SE=新QTNode();
此->根->SW=新QTNode();
//递归地将子正方形拆分为四个更小的子正方形,其尺寸为原始尺寸的一半。
此->根->NW=buildQTRecur(mImage、startRow、startCol、subImageDim/2);
此->根->NE=buildQTRecur(mImage、startRow、startCol+subImageDim/2、subImageDim/2);
此->根->SW=buildQTRecur(mImage,startRow+subImageDim/2,startCol,subImageDim/2);
此->根->SE=buildQTRecur(mImage,startRow+subImageDim/2,startCol+subImageDim/2,subImageDim/2);
}
}
返回此->根目录;
}
当我试图运行它时,我陷入了一个无限循环。请让我知道,如果看到任何其他内容(如我的节点构造函数)或任何附加信息以提供帮助,是否会有所帮助
谢谢。我发现您的代码中有几个问题:
- 谁负责创建子节点?如果你两者都写
然后首先分配一个新的四叉树,然后覆盖它this->root->NW = new QTNode(); this->root->NW = buildQTRecur(mImage, startRow, startCol, subImageDim/2);
- 你得到了计算均匀分布的逻辑
- 如果您找到两个不同的像素,则执行
。但它只会从内部循环中走出来。你应该考虑把它放在一个助手函数中,并做一个<代码>返回<代码>,以立即从两个循环中跳出。中断操作
- 为了提高效率,你不应该写信
放if ((i == startRow) && (j == startCol)) initialValue = mImage->imgPixels[i*mImage->rows+j];
循环前initialValue = mImage->imgPixels[startRow*mImage->rows+startCol];