This is going to be lengthy, so I apologize ahead of time. But I want to make sure the context is understood, as I have read numerous posts already on the subject. None that I have found solve the problem of tally the numbers based on an unknown range of numbers.
I am trying to determine the total occurrences of each integer for a set. The issue I am having is that, the test set would be fixed, say an array of 10 numbers, but the range of each number is unknown. The problem posed is that when trying to use an array to tally the totals, the array range cannot be variable at run-time. I retried this attempt using vector<int> ArrayName
, whereas I could re-size the array of totals at run-time, but ran into errors using the vector<int>
values in calculations.
The code I will be presenting is use of OpenCV
for face detection. I've studied and utilized code from various samples to create a basic detection program, then studied and ported some Java code to handle tracking when a face has moved and update to it's new location. All of this code is working.
Where I would like to use the requested information, is that I want to store an array of the last, say 10
, subjects detected of a face and find the one most detected over time and consider it as the subject. Let's say, we are detecting 10, and the last ten frames were detected as follows (-1 is unknown face): -1, -1, 0, 0, 0, 3, 0, -1, 0, 2
. After the tallies, 0
occurred most frequently and therefor the subject is 0
. The reason the Range is unknown, is because the subject ID is dependent on the number of subjects trained and is always changing.
The three errors are ( and denoted by //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)&'
Here is the code:
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;
}
}