【问题标题】:Group the similar boxes将相似的框分组
【发布时间】:2013-08-21 14:33:03
【问题描述】:

我有一组 (X,Y) 坐标,将单位正方形分成子矩形。假设我的坐标是 -

         (    x1,    y1)    (    x2,    y2)      

         (0.0000,0.0000)    (0.3412,0.4175)   
         (0.7445,0.0000)    (1.0000,0.6553)   
         (0.7445,0.6553)    (1.0000,1.0000)   
         (0.0000,0.6553)    (0.7445,1.0000)   
         (0.3412,0.0000)    (0.7445,0.4175)   
         (0.3412,0.4175)    (0.7445,0.6553)   
         (0.0000,0.4175)    (0.3412,0.6553)....etc (total 10,000 coordinates)

作为一个例子,我只取了 16 组数据,这些坐标像这样分割我的正方形-

相似框的定义

那些具有相似数量的邻居的盒子被认为是相似的盒子。对于上面的图像 box[8]、box[13] 等有 4 个最近邻。所以它们被认为是相似的盒子。

下面的图片应该可以清楚地说明这一点-

::我的问题::

从图中我们可以看到-

对于 box[8],最近的盒子是:

box(1)(有 4 个邻居)

box[4](也有 4 个邻居)

box[14](有 4 个邻居)

box[16](有 4 个邻居)

所以在这种情况下,最近框的邻居之和 = 4+4+4+4 =16

再次对于 box[13],最近的盒子是:

box[3](有 6 个邻居)

box[5](也有 4 个邻居)

box[6](有 3 个邻居)

box[12](有 3 个邻居)

所以在这种情况下,最近框的邻居之和 = 6+4+3+3 =16

这里是(相似框)box[8] 和 box[13] 的邻居总数 = 16+16 =32。

同样,我想对所有有 4 个邻居的盒子进行分组,并找到它们最近的盒子的邻居的总和。并继续为每个相似的组。

我的代码

这是我的代码。

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stdio.h>

using namespace std;

class Rect {
public:
double x1, x2, y1, y2; // coordinates

Rect(double X1, double Y1, double X2, double Y2) {
  if (X1 < X2) {
    x1 = X1; x2 = X2;
  } else {
    x2 = X1; x1 = X2;
  }
  if (Y1 < Y2) {
    y1 = Y1; y2 = Y2;
  } else {
    y2 = Y1; y1 = Y2;
  }


}

bool isAdjacent(Rect rect) {
    if (x1 == rect.x1 || x1 == rect.x2 ||
        x2 == rect.x1 || x2 == rect.x2) {
      // use only < when comparing y1 and rect.y2 avoids sharing only a corner
      if (y1 >= rect.y1 && y1 < rect.y2) {
        return true;
      }
      if (y2 > rect.y1 && y2 <= rect.y2) {
        return true;
      }
      if (rect.y1 >= y1 && rect.y1 < y2) {
        return true;
      }
      if (rect.y2 > y1 && rect.y2 <= y2) {
        return true;
      }
    }
    if (y1 == rect.y1 || y1 == rect.y2 ||
        y2 == rect.y1 || y2 == rect.y2) {
      if (x1 >= rect.x1 && x1 < rect.x2) {
        return true;
      }
      if (x2 > rect.x1 && x2 <= rect.x2) {
        return true;
      }
      if (rect.x1 >= x1 && rect.x1 < x2) {
        return true;
      }
      if (rect.x2 > x1 && rect.x2 <= x2) {
        return true;
      }
    }
    return false;
  }

};



void isNearest(int b){

vector<Rect> rects;     
                //Rect(  x1 ,  y1  ,   x2  ,  y2   ) 
  rects.push_back(Rect(0.0000,0.0000, 0.8147,0.1355));
  rects.push_back(Rect(0.8147,0.0000, 1.0000,0.1355));

  rects.push_back(Rect(0.8147,0.1355, 0.9058,0.8350));
  rects.push_back(Rect(0.0000,0.1355, 0.1270,0.9689));

  rects.push_back(Rect(0.9058,0.1355, 0.9134,0.2210));
  rects.push_back(Rect(0.9058,0.8350, 1.0000,1.0000));
  rects.push_back(Rect(0.8147,0.8350, 0.9058,1.0000));


  rects.push_back(Rect(0.1270,0.1355, 0.6324,0.3082));
  rects.push_back(Rect(0.1270,0.9689, 0.8147,1.0000));
  rects.push_back(Rect(0.0000,0.9689, 0.1270,1.0000));

  rects.push_back(Rect(0.9134,0.1355, 1.0000,0.2210));
  rects.push_back(Rect(0.9134,0.2210, 1.0000,0.8350));
  rects.push_back(Rect(0.9058,0.2210, 0.9134,0.8350));


  rects.push_back(Rect(0.6324,0.1355, 0.8147,0.3082));
  rects.push_back(Rect(0.6324,0.3082, 0.8147,0.9689));
  rects.push_back(Rect(0.1270,0.3082, 0.6324,0.9689));


  int nearBox_count = 0;

  double TotalArea=0;


  for (int x = 0; x < rects.size(); ++x) {

    if (rects[b].isAdjacent(rects[x])) {


      if (x==b) {
continue; //this is our box , so do not count it.
}


nearBox_count++;

printf("box[%d] is nearest to box[%d]  \n", (b+1), (x+1));

}
}

printf("Total number of nearest box for [%d] is %d  \n",(b+1),nearBox_count );
printf("\n");

}


int main() {

  for (int i = 0; i < 16; ++i)
  {
    isNearest(i);
  }

return 0;
}

它给出了这样的正确结果-

box[1] is nearest to box[2]  
box[1] is nearest to box[4]  
box[1] is nearest to box[8]  
box[1] is nearest to box[14]  
Total number of nearest box for [1] is 4  

box[2] is nearest to box[1]  
box[2] is nearest to box[3]  
box[2] is nearest to box[5]  
box[2] is nearest to box[11]  
Total number of nearest box for [2] is 4  

box[3] is nearest to box[2]  
box[3] is nearest to box[5]  
box[3] is nearest to box[7]  
box[3] is nearest to box[13]  
box[3] is nearest to box[14]  
box[3] is nearest to box[15]  
Total number of nearest box for [3] is 6  

box[4] is nearest to box[1]  
box[4] is nearest to box[8]  
box[4] is nearest to box[10]  
box[4] is nearest to box[16]  
Total number of nearest box for [4] is 4  

box[5] is nearest to box[2]  
box[5] is nearest to box[3]  
box[5] is nearest to box[11]  
box[5] is nearest to box[13]  
Total number of nearest box for [5] is 4  

box[6] is nearest to box[7]  
box[6] is nearest to box[12]  
box[6] is nearest to box[13]  
Total number of nearest box for [6] is 3  

box[7] is nearest to box[3]  
box[7] is nearest to box[6]  
box[7] is nearest to box[9]  
box[7] is nearest to box[15]  
Total number of nearest box for [7] is 4  

box[8] is nearest to box[1]  
box[8] is nearest to box[4]  
box[8] is nearest to box[14]  
box[8] is nearest to box[16]  
Total number of nearest box for [8] is 4  

box[9] is nearest to box[7]  
box[9] is nearest to box[10]  
box[9] is nearest to box[15]  
box[9] is nearest to box[16]  
Total number of nearest box for [9] is 4  

box[10] is nearest to box[4]  
box[10] is nearest to box[9]  
Total number of nearest box for [10] is 2  

box[11] is nearest to box[2]  
box[11] is nearest to box[5]  
box[11] is nearest to box[12]  
Total number of nearest box for [11] is 3  

box[12] is nearest to box[6]  
box[12] is nearest to box[11]  
box[12] is nearest to box[13]  
Total number of nearest box for [12] is 3  

box[13] is nearest to box[3]  
box[13] is nearest to box[5]  
box[13] is nearest to box[6]  
box[13] is nearest to box[12]  
Total number of nearest box for [13] is 4  

box[14] is nearest to box[1]  
box[14] is nearest to box[3]  
box[14] is nearest to box[8]  
box[14] is nearest to box[15]  
Total number of nearest box for [14] is 4  

box[15] is nearest to box[3]  
box[15] is nearest to box[7]  
box[15] is nearest to box[9]  
box[15] is nearest to box[14]  
box[15] is nearest to box[16]  
Total number of nearest box for [15] is 5  

box[16] is nearest to box[4]  
box[16] is nearest to box[8]  
box[16] is nearest to box[9]  
box[16] is nearest to box[15]  
Total number of nearest box for [16] is 4  

虽然它可以识别最近的盒子并计算邻居的数量,但我不知道如何对相似的盒子进行分组(如上所述)并找到总和。

我被困在这里。谁能帮帮我?

更新的代码片段

vector<CheckRect> rects;

unsigned isNearest(unsigned b, vector<unsigned>& neighbours) {

  unsigned nearBox_count = 0;

  for (unsigned x = 0; x < rects.size(); ++x) {
    if (rects[b].isAdjacent(rects[x])) {
      if (x==b) continue; //this is our box , so do not count it.
      nearBox_count++;
      printf("box[%d] is nearest to box[%d]  \n", (b+1), (x+1));
      neighbours.push_back(x);
    }
  }

  printf("Total number of nearest box for [%d] is %d  \n",
        (b+1), nearBox_count );
  printf("\n");

  return nearBox_count;
}

int main(){

cin>>N;

for(int b=0; b<N; b++){

  ifstream inputFile1("RectCoordinates.txt"); //input from the file previously generated
  int rect_number;
  double xa0,ya0,xa1,ya1;
  int neighbours;
  isNearest( b, &neighbours);// This is the line that causing my ERROR


  }
 vector<unsigned> nearBox_count(rects.size());
  vector< vector<unsigned> > neighbours(rects.size());
  for (unsigned i = 0; i < rects.size(); ++i) {
    nearBox_count[i] = isNearest(i, neighbours[i]);
  }

  // Calculate the sums of neighbouring boxes
  vector<unsigned> neighCount(rects.size(), 0);
  for (unsigned i = 0; i < rects.size(); i++) {
    for (unsigned j = 0; j < neighbours[i].size(); j++) {
      neighCount[i] += nearBox_count[neighbours[i][j]];
    }
  }

  // Calculate your result
  map<unsigned,unsigned> finalCount;
  for (unsigned i = 0; i < rects.size(); i++)
  {
    if (finalCount.count(nearBox_count[i]) == 0)
      finalCount[nearBox_count[i]] = neighCount[i];
    else
      finalCount[nearBox_count[i]] += neighCount[i];
  }

  // Print the result
  for (map<unsigned,unsigned>::iterator it = finalCount.begin();
        it != finalCount.end(); ++it) {
    printf("Sum neighbours for the neighbours of similar boxes with %d "
           "neighbours is %d\n", it->first, it->second);
  }

  return 0;
}

给我错误-

ss.cpp: In function ‘int main()’:
ss.cpp:102:29: error: invalid initialization of reference of type ‘std::vector<unsigned int>&’ from expression of type ‘unsigned int’
ss.cpp:22:10: error: in passing argument 2 of ‘unsigned int isNearest(unsigned int, std::vector<unsigned int>&)’

我该如何解决?

【问题讨论】:

  • @hacks 是的,我是一名物理专业的学生。研究这类问题一段时间。这是我遇到的问题的一部分。 :(
  • 我看过你的很多Box问题,所以才问你。
  • @hacks 你是对的。我希望这可能是关于盒子的最后一个问题。但这一切实际上都取决于结果。
  • 您想修复错误吗?只需从导致它的行中删除&amp;。参数类型是 reference 而不是 pointer。顺便说一句,这对于曾经写过CC++ 程序员来说是一个常见的错误。
  • @RondogiannisAristophanes 这实际上不是错误。错误是aries0152 试图将整数而不是整数向量传递给函数isNearest&amp; 也应该被删除。

标签: c++ algorithm


【解决方案1】:

与其尝试在某些数据结构中维护矩形之间的关系,不如让矩形对象本身更智能并了解它的邻居数量以及它们是谁。

例如(不完整的原型来说明想法):

class Rect {
public:
//methods
Rect(double X1, double Y1, double X2, double Y2);

//const access
double getX1() const;
double getX2() const;
double getY1() const;
double getY2() const;

int numNeighbors() const { return neighbors.size();}
int sumOfNeighbors() const { int res(0); for(size_t i=0;i< neighbors.size();++i) res += neighbors[i]->numNeighbors(); return res;}
std::vector<Rect*> getNeighbors() {return neighbors};

void addNeighbor(Rect* newNeighbor) {neighbors.push_back(newNeighbor);}

//data
private:

double x1, x2, y1, y2; // coordinates
std::vector<Rect*> neighbors;
};

使用这样的 rect 类,您可以为每个矩形添加邻居,检索每个矩形的所有邻居及其所有邻居 - 所有关系都在 rect 本身而不是一些外部对象中维护,主程序的代码应该非常最小。

填充矩形后,您可以简单地遍历它们,选择具有所需数量的邻居,并对它们执行任何操作。

【讨论】:

    【解决方案2】:

    我想如果你想彻底简化这一切,你可以使用 Kobelevskiy 先生的建议:

    #include <iostream>
    #include <cstdlib>
    #include <vector>
    #include <stdio.h>
    
    using namespace std;
    
    class Rect {
    public:
    double x1, x2, y1, y2; // coordinates
    
    //methods
    Rect(double X1, double Y1, double X2, double Y2) {
      if (X1 < X2) {
        x1 = X1; x2 = X2;
      } else {
        x2 = X1; x1 = X2;
      }
      if (Y1 < Y2) {
        y1 = Y1; y2 = Y2;
      } else {
        y2 = Y1; y1 = Y2;
      }
    }
    
    ~Rect()
    {
    };
    
    int numNeighbors() const { return neighbors.size();}
    int sumOfNeighbors() const { int res(0); for(size_t i=0;i< neighbors.size();++i) res += neighbors[i]->numNeighbors(); return res;}
    std::vector<Rect*> getNeighbors() {return neighbors;};
    
    void addNeighbor(Rect* newNeighbor) {neighbors.push_back(newNeighbor);}
    
    //data
    std::vector<Rect*> neighbors;
    
    bool isAdjacent(Rect* rect) {
        if (x1 == rect->x1 || x1 == rect->x2 ||
            x2 == rect->x1 || x2 == rect->x2) {
          // use only < when comparing y1 and rect->y2 avoids sharing only a corner
          if (y1 >= rect->y1 && y1 < rect->y2) {
            return true;
          }
          if (y2 > rect->y1 && y2 <= rect->y2) {
            return true;
          }
          if (rect->y1 >= y1 && rect->y1 < y2) {
            return true;
          }
          if (rect->y2 > y1 && rect->y2 <= y2) {
            return true;
          }
        }
        if (y1 == rect->y1 || y1 == rect->y2 ||
            y2 == rect->y1 || y2 == rect->y2) {
          if (x1 >= rect->x1 && x1 < rect->x2) {
            return true;
          }
          if (x2 > rect->x1 && x2 <= rect->x2) {
            return true;
          }
          if (rect->x1 >= x1 && rect->x1 < x2) {
            return true;
          }
          if (rect->x2 > x1 && rect->x2 <= x2) {
            return true;
          }
        }
        return false;
      }
    
    };
    
    vector<Rect*> rects;
    
    void CalculateAdjacentsForRect(unsigned int rects_element){
    
        for (unsigned int x = 0; x < rects.size(); x++) {
            if (rects[rects_element]->isAdjacent(rects[x])) {
                if (x==rects_element) {
                    continue; //this is our box , so do not count it.
                }
                rects[rects_element]->addNeighbor(rects[x]);
            }
        }
    }
    
    const int MAX_ADJACENT_RECTS = 10;
    
    int main() {
    
                        //Rect(  x1 ,  y1  ,   x2  ,  y2   )
        rects.push_back(&Rect(0.0000,0.0000, 0.8147,0.1355));
        rects.push_back(&Rect(0.8147,0.0000, 1.0000,0.1355));
    
        rects.push_back(&Rect(0.8147,0.1355, 0.9058,0.8350));
        rects.push_back(&Rect(0.0000,0.1355, 0.1270,0.9689));
    
        rects.push_back(&Rect(0.9058,0.1355, 0.9134,0.2210));
        rects.push_back(&Rect(0.9058,0.8350, 1.0000,1.0000));
        rects.push_back(&Rect(0.8147,0.8350, 0.9058,1.0000));
    
    
        rects.push_back(&Rect(0.1270,0.1355, 0.6324,0.3082));
        rects.push_back(&Rect(0.1270,0.9689, 0.8147,1.0000));
        rects.push_back(&Rect(0.0000,0.9689, 0.1270,1.0000));
    
        rects.push_back(&Rect(0.9134,0.1355, 1.0000,0.2210));
        rects.push_back(&Rect(0.9134,0.2210, 1.0000,0.8350));
        rects.push_back(&Rect(0.9058,0.2210, 0.9134,0.8350));
    
    
        rects.push_back(&Rect(0.6324,0.1355, 0.8147,0.3082));
        rects.push_back(&Rect(0.6324,0.3082, 0.8147,0.9689));
        rects.push_back(&Rect(0.1270,0.3082, 0.6324,0.9689));
    
        for (unsigned int i = 0; i < rects.size(); i++)
        {
            CalculateAdjacentsForRect(i);
        }
    
        for (unsigned int i = 0; i < rects.size(); i++)
        {
            cout << "\nRect" << i << " has a neighbor sum of " << rects[i]->sumOfNeighbors();
        }
    
        cout << "\n";
    
        for (int ix = 0; ix < MAX_ADJACENT_RECTS; ix++)
        {
            int num_rects_with_this_num_of_adjacents = 0;
            int num_adjacents_total_for_similar_rects = 0;
            for (unsigned int i = 0; i < rects.size(); i++) {
                if ( rects[i]->numNeighbors() == ix ) {
                   num_rects_with_this_num_of_adjacents++;
                   num_adjacents_total_for_similar_rects += rects[i]->sumOfNeighbors();
                }
            }
            cout << "\nThere are " << num_rects_with_this_num_of_adjacents << " rects with " << ix << " adjacent rects. They have a cum neighbor sum of " << num_adjacents_total_for_similar_rects;
        }
    
        return 0;
    }
    

    【讨论】:

    • 谢谢,我学会了解决这个问题的新方法。投票赞成。 :)
    • @aries0152 查看此版本。 Kobelevskiy 先生的框架大大简化了这一切。我想最好对这类事情使用指针。当您尝试通过向量引用和比较元素时,它肯定会有所帮助。我的非指针版本遇到了很多问题,这些问题随这个版本消失了。
    【解决方案3】:

    除了尝试计算您的价值外,我还对您的代码进行了一些小改动。

    由于您的所有列表索引都不是负数,并且将来您可能会拥有大量的矩形,因此我建议您将所有ints 转换为unsigned。这有一个额外的好处,即在下面的代码中抑制某些关于比较有符号和无符号整数的编译器警告。

    我建议您进行的第二个更改是只声明一次rects,而不是每次迭代isNearest。在下面的代码中,我通过使rects 成为全局变量并创建一个单独的函数来初始化它来实现这一点。通过将rects 设为全局变量,您现在可以将所有16s 替换为rects.size()(减少添加完整数据集时忘记更改16 的机会)。

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <vector>
    #include <map>
    #include <stdio.h>
    
    using namespace std;
    
    class Rect {
    public:
      double x1, x2, y1, y2; // coordinates
    
      Rect(double X1, double Y1, double X2, double Y2) {
        if (X1 < X2) {
          x1 = X1; x2 = X2;
        } else {
          x2 = X1; x1 = X2;
        }
        if (Y1 < Y2) {
          y1 = Y1; y2 = Y2;
        } else {
          y2 = Y1; y1 = Y2;
        }
    }
    
    bool isAdjacent(Rect rect) {
        if (x1 == rect.x1 || x1 == rect.x2 ||
            x2 == rect.x1 || x2 == rect.x2) {
          // use only < when comparing y1 and rect.y2 avoids sharing only a corner
          if (y1 >= rect.y1 && y1 < rect.y2) {
            return true;
          }
          if (y2 > rect.y1 && y2 <= rect.y2) {
            return true;
          }
          if (rect.y1 >= y1 && rect.y1 < y2) {
            return true;
          }
          if (rect.y2 > y1 && rect.y2 <= y2) {
            return true;
          }
        }
        if (y1 == rect.y1 || y1 == rect.y2 ||
            y2 == rect.y1 || y2 == rect.y2) {
          if (x1 >= rect.x1 && x1 < rect.x2) {
            return true;
          }
          if (x2 > rect.x1 && x2 <= rect.x2) {
            return true;
          }
          if (rect.x1 >= x1 && rect.x1 < x2) {
            return true;
          }
          if (rect.x2 > x1 && rect.x2 <= x2) {
            return true;
          }
        }
        return false;
      }
    };
    
    vector<Rect> rects;
    
    unsigned isNearest(unsigned b, vector<unsigned>& neighbours) {
    
      unsigned nearBox_count = 0;
    
      for (unsigned x = 0; x < rects.size(); ++x) {
        if (rects[b].isAdjacent(rects[x])) {
          if (x==b) continue; //this is our box , so do not count it.
          nearBox_count++;
          printf("box[%d] is nearest to box[%d]  \n", (b+1), (x+1));
          neighbours.push_back(x);
        }
      }
    
      printf("Total number of nearest box for [%d] is %d  \n",
            (b+1), nearBox_count );
      printf("\n");
    
      return nearBox_count;
    }
    
    void initRects(void) {
    
                    //Rect(  x1 ,  y1  ,   x2  ,  y2   ) 
      rects.push_back(Rect(0.0000,0.0000, 0.8147,0.1355));
      rects.push_back(Rect(0.8147,0.0000, 1.0000,0.1355));
    
      rects.push_back(Rect(0.8147,0.1355, 0.9058,0.8350));
      rects.push_back(Rect(0.0000,0.1355, 0.1270,0.9689));
    
      rects.push_back(Rect(0.9058,0.1355, 0.9134,0.2210));
      rects.push_back(Rect(0.9058,0.8350, 1.0000,1.0000));
      rects.push_back(Rect(0.8147,0.8350, 0.9058,1.0000));
    
    
      rects.push_back(Rect(0.1270,0.1355, 0.6324,0.3082));
      rects.push_back(Rect(0.1270,0.9689, 0.8147,1.0000));
      rects.push_back(Rect(0.0000,0.9689, 0.1270,1.0000));
    
      rects.push_back(Rect(0.9134,0.1355, 1.0000,0.2210));
      rects.push_back(Rect(0.9134,0.2210, 1.0000,0.8350));
      rects.push_back(Rect(0.9058,0.2210, 0.9134,0.8350));
    
    
      rects.push_back(Rect(0.6324,0.1355, 0.8147,0.3082));
      rects.push_back(Rect(0.6324,0.3082, 0.8147,0.9689));
      rects.push_back(Rect(0.1270,0.3082, 0.6324,0.9689));
    }
    
    void readRects(const string& filename) {
    
      ifstream fpInput(filename.c_str());
      double dTemp[4];
    
      while (true) {
        for (unsigned i = 0; i < 4; i++) fpInput >> dTemp[i];
        if (!fpInput.good()) break;
        rects.push_back(Rect(dTemp[0], dTemp[1], dTemp[2], dTemp[3]));
      }
    
      fpInput.close();
    }
    
    int main() {
    
      // Initialize the vector rects
      //initRects();
      readRects("RectCoordinates.txt");
    
      vector<unsigned> nearBox_count(rects.size());
      vector< vector<unsigned> > neighbours(rects.size());
      for (unsigned i = 0; i < rects.size(); ++i) {
        nearBox_count[i] = isNearest(i, neighbours[i]);
      }
    
      // Calculate the sums of neighbouring boxes
      vector<unsigned> neighCount(rects.size(), 0);
      for (unsigned i = 0; i < rects.size(); i++) {
        for (unsigned j = 0; j < neighbours[i].size(); j++) {
          neighCount[i] += nearBox_count[neighbours[i][j]];
        }
      }
    
      // Calculate your result
      map<unsigned,unsigned> finalCount;
      for (unsigned i = 0; i < rects.size(); i++)
      {
        if (finalCount.count(nearBox_count[i]) == 0) {
          finalCount[nearBox_count[i]] = neighCount[i];
        } else {
          finalCount[nearBox_count[i]] += neighCount[i];
        }
      }
    
      // Print the result
      for (map<unsigned,unsigned>::iterator it = finalCount.begin();
            it != finalCount.end(); ++it) {
        printf("Sum neighbours for the neighbours of similar boxes with %d "
               "neighbours is %d\n", it->first, it->second);
      }
    
      return 0;
    }
    

    更新: 上面的代码现在可以通过在源文件中指定Rects 或从外部文件加载来使用。在上面的修改示例中,输入文件是RectCoordinates.txt

    0.0000  0.0000  0.8147  0.1355
    0.8147  0.0000  1.0000  0.1355
    
    0.8147  0.1355  0.9058  0.8350
    0.0000  0.1355  0.1270  0.9689
    
    0.9058  0.1355  0.9134  0.2210
    0.9058  0.8350  1.0000  1.0000
    0.8147  0.8350  0.9058  1.0000
    
    
    0.1270  0.1355  0.6324  0.3082
    0.1270  0.9689  0.8147  1.0000
    0.0000  0.9689  0.1270  1.0000
    
    0.9134  0.1355  1.0000  0.2210
    0.9134  0.2210  1.0000  0.8350
    0.9058  0.2210  0.9134  0.8350
    
    
    0.6324  0.1355  0.8147  0.3082
    0.6324  0.3082  0.8147  0.9689
    0.1270  0.3082  0.6324  0.9689
    

    以上输出结果:

    Sum neighbours for the neighbours of similar boxes with 2 neighbours is 8
    Sum neighbours for the neighbours of similar boxes with 3 neighbours is 32
    Sum neighbours for the neighbours of similar boxes with 4 neighbours is 165
    Sum neighbours for the neighbours of similar boxes with 5 neighbours is 22
    Sum neighbours for the neighbours of similar boxes with 6 neighbours is 25
    

    【讨论】:

    • 不错!我正在考虑使用文本文件并从中获取矩形值的输入。到目前为止,它似乎像我想要的那样工作。让我检查一下其他值。
    • 您的代码给了我正确的结果。但是我在从文本文件中获取矩形输入时遇到了问题。它显示错误-“来自‘unsigned int’类型的表达式的‘std::vector&’类型的引用无效初始化”。我该如何解决?我已经更新了我的代码。请检查。
    • @aries0152 我更新了我的帖子。代码现在读入包含数据点的文件。该错误是因为您尝试将int 而不是vector&lt;unsigned int&gt; 传递给更新的函数isNearestisNearest 返回邻居的数量并将实际邻居存储在变量 neighbours 中。
    【解决方案4】:

    好吧,我想你可以使用向量,但这种方式会占用大量内存空间。 说,制作向量 4neighbours - 乍一看,我会说每个盒子至少有 4 个邻居(但是你必须为每个可能的邻居数量做这个 - 希望它可以被计算出来,它不会是超级大)。然后,在检查每个矩形的邻居数时,对相应的向量使用 pushback。

    printf("Total number of nearest box for [%d] is %d  \n",(b+1),nearBox_count );
    if(nearBox_count == 4)
     4neighbours.pushback(rects[b]);
    else if (nearBox_count == 5)
     5neighbours.pushback(rects[b]);
    else if -etc-
    

    然后,遍历向量并为向量的每个成员检查邻居,并为每个邻居计算邻居并将其相加(您将嵌套两个循环 - 一个循环内的循环)

    示例:未编码

    int TotalNeighbours = 0;
    while(go through all the boxes in the vector)
    {
        while(go through all the neighbours of each box)
        {
         TotalNeighbours++;
        }
    }
    

    我相信这样的事情可能会奏效,但有上述限制。 取决于最大邻居数:

    • 大量内存使用;
    • 大量编写代码(大量 if 语句);

    编辑:一个盒子可以有 0 个邻居,其余的至少有 1 个邻居,而不是 4 个。

    【讨论】:

    • 我已经想到了。但是这样我必须使用大约 40-50 个 if 语句。
    • @aries0152 是的,如果我没记错的话,每个可能的邻居数都有一个 if 语句。这确实是最后的手段。如果没有出现更好/可理解的解决方案并且您有时间,那么大量代码可以解决问题。但是,在这种情况下,我相信会出现更好的方法。
    • @Sampaio 虽然不在上面给出的示例中,但边缘框可能有 1 个邻居,如果只有一个框,则可能有 0 个邻居。
    • @ilent2 你是对的,我(错误地)假设盒子会随着数字(盒子(x)
    猜你喜欢
    • 2015-11-16
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多