C ++ int出现计数未知范围

时间:2012-10-10 20:43:00

标签: c++ opencv 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取决于受训对象的数量并且总是在变化。

这三个错误是(并由// ERROR表示):

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;
        }
    }

4 个答案:

答案 0 :(得分:1)

要访问数组中的项目,您应该使用[]而不是()

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

答案 1 :(得分: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

(他们可能不是那个顺序。我觉得他们会是这样但是我忘记了地图如何命令他们的内容。)

答案 2 :(得分:0)

代码存在一些问题:

1)向量的数组访问使用数组下标operator []而不是函数operator()

2)方法和成员的命名使用不一致的样式,成员和方法应该仅基于名称来区分。只有一个容易错过的's'的成员和方法不同就是要求麻烦。

3)考虑使用地图或优先级队列而不是矢量,这应该删除大量非算法细节。

答案 3 :(得分:0)

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

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

这相当于写作:

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

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

我认为是:

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

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