2012-10-10 36 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++) { 

否則你的循環將永遠不會停止。