这会很长,所以我提前道歉。但我想确保上下文被理解,因为我已经阅读了很多关于这个主题的帖子。我发现没有一个可以解决根据未知数字范围计算数字的问题。
我试图确定一组中每个整数的总出现次数。我遇到的问题是,测试集是固定的,比如 10 个数字的数组,但每个数字的范围未知。提出的问题是,当尝试使用数组来统计总数时,数组范围在运行时不能是可变的。我使用 vector<int> ArrayName
重试了此尝试,而我可以在运行时重新调整总计数组的大小,但使用 vector<int>
时遇到错误计算中的值。
我将展示的代码是使用OpenCV
用于人脸检测。我研究并利用了各种示例中的代码来创建一个基本的检测程序,然后研究并移植了一些 Java 代码来处理人脸移动和更新到新位置时的跟踪。所有这些代码都有效。
我想使用请求的信息的地方是我想存储最后一个数组,例如 10
,检测到一张脸的主体,并找到随着时间的推移检测到最多的一张,并将其视为主体。假设我们正在检测 10 个,最后十帧检测如下(-1 是未知面孔):-1, -1, 0, 0, 0, 3, 0, -1, 0, 2
。统计后,0
出现最频繁,因此主题是 0
。 Range 未知的原因是受试者 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;
}
}
最佳答案
要访问数组中的项目,您应该使用 [] 而不是 ()
所以Subjects(i)
应该是Subjects[i]
和
总计(主题(i))=总计(主题(i))+ 1;
应该是总计[主题[i]]=总计[主题[i]]+1;
关于C++ int 出现次数未知范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12828016/