2012-10-10 121 views
2

这将会很长,所以我提前道歉。但是我想确认上下文是可以理解的,因为我已经阅读了许多关于这个主题的文章。我没有发现解决数字基于未知数字范围的问题。C++ int发生次数未知范围

我想确定一个集合的每个整数的总出现次数。我遇到的问题是,测试集是固定的,例如10个数字的数组,但每个数字的范围是未知的。提出的问题是,当试图使用数组来计算总数时,数组范围在运行时不能变化。我尝试使用vector<int> ArrayName重试此尝试,但我可以在运行时重新调整总计数组大小,但在计算中使用vector<int>值时会遇到错误。

我将介绍的代码是使用OpenCV进行人脸检测。我研究并利用各种样本的代码创建了一个基本的检测程序,然后研究并移植了一些Java代码,以便在面部移动并更新到新位置时处理跟踪。所有这些代码正在工作。

我想在哪里使用请求的信息,是我想存储最后一个数组,例如10,检测到一个脸部的主体,并找到一个随时间检测到最多的主体并将其视为主体。假设我们正在检测10个,并且最后十个帧被检测到如下(-1是未知的面):-1, -1, 0, 0, 0, 3, 0, -1, 0, 2。在发送之后,0发生得最为频繁,因此主题为0。范围未知的原因是因为主题ID取决于受过训练的主题数量,并且一直在变化。

三个错误(以及//错误表示):

invalid use of member (did you forget the '&' ?), 
no match for call to '(std::vector<int>) (int)', 
no match for call to '(std::vector<int>) (int)&' 

下面是代码:

struct FaceStruct { 
    private: 
     static const int NewLife = 120; //Divide by FPS for Length in Time 
     static const int TotalTrackedSubjects = 10; 
     int Life; 
     vector<int> Subjects; 
    public: 
     void Init(Rect, int); 
     void addSubject(int); 
     int Subject(); 
     Rect Location; 
     bool Used; 
     void Weaken(); 
     void Renew(); 
     bool Dead(); 
    }; 
    void FaceStruct::Init(Rect location, int subject = -1) { 
     Location = location; 
     Subjects.resize(TotalTrackedSubjects - 1); 
     for(int i = 0; TotalTrackedSubjects - 1; i++) { 
      Subjects(i) = -1; //ERROR 
     } 
     Renew(); 
    } 
    void FaceStruct::addSubject(int subject) { 
     for(int i = 0; TotalTrackedSubjects - 2; i++) { 
      Subjects(i) = Subjects(i + 1); //ERROR 
     } 
     Subjects(TotalTrackedSubjects - 1) = subject; //ERROR 
    } 
    int FaceStruct::Subject() { 
     int count_range = -1; 
     for(int i = 0; TotalTrackedSubjects - 1; i++) { 
      if(Subjects(i) > count_range) count_range = Subjects(i); //ERROR 
     } 
     if(count_range < 0) { //Subject Unknown 
      return -1; 
     } else if(count_range == 0) { //Subject is 0, Handle 0's 
      int totals = 0; 
      for(int i = 0; TotalTrackedSubjects - 1; i++) { 
       if(Subjects(i) == 0) totals++; //ERROR 
      } 
      return totals; 
     } else { //Use count_range 
      vector<int> totals; 
      int unknowns = 0; 
      totals.resize(count_range); 
      for(int i = 0; TotalTrackedSubjects - 1; i++) { 
       if(Subjects(i) < 0) { //ERROR 
        unknowns++; 
       } else { 
        totals(Subjects(i)) = totals(Subjects(i)) + 1; //ERROR 
       } 
      } 
      int largest = -1; 
      for(int i = 0; totals.size() - 1; i++) { 
       if(totals(i) > largest) largest = totals(i); //ERROR 
      } 
      return largest; 
     } 
    } 
    void FaceStruct::Weaken() { 
     Life--; 
    } 
    void FaceStruct::Renew() { 
     Life = NewLife; 
    } 
    bool FaceStruct::Dead() { 
     if(Life < 1) { 
      return true; 
     } else { 
      return false; 
     } 
    } 
+2

我想你在继续阅读之前阅读[一些C++书籍](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。您的for循环何时停止? –

+0

我以前从未遇到过问题。 TotalTrackedSubjects在Struct的顶部被定义为一个const int 10,那么它会以10 - 1结束...不是? –

+0

如果这就是你认为的那样,这并不是从0到9。 – Matt

回答

1

到在阵列中访问的项应该使用[]代替()

所以Subjects(i)应该Subjects[i]
totals(Subjects(i)) = totals(Subjects(i)) + 1; 应该totals[ Subjects[i] ] = totals[ Subjects[i] ] + 1;

+0

+1,因为这绝对是问题的一部分。 – Wug

+0

你是完全正确的。我昨天写了代码,失败并且报废了它。然后我在大约五分钟后重写了它,所以我可以问这个问题......让我修复并看看这次会发生什么。 –

+0

修复()s肯定拿出了所有的错误,项目编译得很好,但是现在执行时我有一个未知的崩溃。我会进一步研究。 –

1

听起来象是一个地图可以做的相当好...

#include <map> 

int example_values[10] = {-1, -1, 0, 0, 0, 3, 0, -1, 0, 2}; 

map<int, int> counts; 
for (int i = 0; i < 10; ++i) ++counts[example_values[i]]; 

for (map<int, int>::iterator i = counts.begin(); i != counts.end(); ++i) 
    cout << i->first << ": " << i->second << endl; 

输出:

-1: 3 
0: 5 
2: 1 
3: 1 

(他们可能不会在这个顺序。我觉得他们会,但我忘了地图究竟如何订购它们的内容)

0

没有与代码的几个问题:

1)向量数组访问使用数组下标运算符[]不是函数运算符()

2)方法和成员的命名使用不一致的样式,成员和方法只能根据名称区分。有一个成员和方法只有一个容易错过的''是不同的是要求麻烦。

3)考虑使用映射或优先级队列,而不是向量,这应该删除很多非算法细节。

0

您的循环条件看起来不对。

for(int i = 0; TotalTrackedSubjects - 1; i++) { 

即相当于写作:

int i = 0; 
while(9){ 
    ... 
    i++ 
} 

for循环构建体的第二部分是退出条件。

我认为是:

for(int i = 0; i < TotalTrackedSubjects; i++) { 

否则你的循环将永远不会停止。