現在のポイントのベクトルの最後に新しいポイントを追加する際に問題があります。現在実行しているのは、追加しようとしている新しいポイントで既存のベクトルを上書きし、1つの要素だけを持つベクトルを残します。Push_backは、新しいポイントをベクターに追加するのではなく、現在のポイントを置き換えています。
これは、クラス定義(.hファイル)です。
class person
{
public:
person();
~person();
int ID;
std::vector<cv::Point> history;
void addposition(cv::Point);
void person::drawhistory(cv::Mat*,std::vector<cv::Point>);
};
そして、これは、クラスの.cppファイルに表示されますどのように関数の宣言です:
person::person()
{
}
person::~person()
{
}
void person::addposition(cv::Point inpt) {
std::cout << "Adding a position ----" << std::endl;
std::cout << "Pushing inpt.x: " << inpt.x << std::endl;
std::cout << "Pushing inpt.y: " << inpt.y << std::endl;
history.push_back(inpt);
std::cout << "Current Size: " << history.size() << std::endl;
if (history.size()>15)
{
history.erase(history.begin());
}
}
void person::drawhistory(cv::Mat* image, std::vector<cv::Point> hist) {
cv::Point pt;
for (cv::Point const& pt : hist)
{
std::cout << "Printing the History" << std::endl;
std::cout << "Current Pt.x: " << pt.x << std::endl;
std::cout << "Current Pt.y: " << pt.y << std::endl;
cv::circle(*image, pt, 5, cv::Scalar(0, 0, 0), -1);
}
}
そして、これがありますどのようにこれら2つの関数がmain関数で呼び出されているかを示します。
vector<RECT> detectBox
をし、それが正しくフレームに必要なポイントを記憶しているので、それが問題の原因ではないことをかなり確信してイム:detectBoxはそのように宣言されていることに注意してください
for (RECT const& rect : *detectBox)
{
std::cout << "Inside the LOOOOOP" << std::endl;
//This is simply finding the middle point of the rectangle currently stored in detectBox
pt.y = (rect.bottom + rect.top)/2;
pt.x = (rect.left + rect.right)/2;
person personn;
personn.addposition(pt);
personn.drawhistory(&img_8bit, personn.history);
cv::circle(img_8bit, pt, 3, cv::Scalar(255, 255, 0), -1);
}
cv::imshow("8Bit", img_8bit);
私は点をベクトルに押し込むのは簡単だと思いますが、何らかの理由で点をベクトルの底に追加しないでください。また、保存したポイントの数を15に保つ消去ステップを追加したことにも注意してください。
私のクラス定義の関数に問題がありますか(私はクラスに慣れていませんか?メインループから関数を呼び出す?
ループのたびに 'person'の新しいインスタンスを作成します。そのようなインスタンスはそれぞれ生まれて、それに追加された単一のポイントを得て、その後死ぬ。変数をループ外に宣言します。 –
現在のサイズの_history_を印刷すると、サイズが1だけ戻されるため、ベクターが最新の15個のポイントまで構築されることを期待しています。 –