Push_back 正在替换当前点而不是向矢量添加新点
Push_back is replacing current point instead of adding a new one to the vector
我无法将新点附加到我当前的点向量的末尾。它目前正在做的是用我要添加的新点覆盖现有向量,使向量只剩下 1 个元素。
这是class定义(.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>);
};
这就是 class 的 .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);
}
}
这就是这两个函数在主函数中的调用方式。请注意,detectBox 是这样声明的:
vector<RECT> 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。
我的 class 定义中的函数有问题(我是 classes 的新手),还是我从主循环调用函数的方式有问题?
这很可能不是您想要的:
person personn;
personn.addposition(pt);
personn.drawhistory(&img_8bit, personn.history);
您的 presonn
位于循环体的本地,因此在每次迭代中您创建一个新的 person
,添加一个位置并打印它。只需在循环外声明 personn
。
PS:这个问题的一个例子可能是这样的:
#include <vector>
#include <iostream>
int main() {
for (int i=0; i<5; i++) {
std::vector<int> vect;
vect.push_back(i);
std::cout << vect.size() << std::endl;
}
}
它重现了您遇到的问题,它进行了编译,并且只包含执行此操作所需的最少代码。如果您自己创建了它,那么您可能自己发现了错误。当然,找到错误的根源并不总是那么容易。调试器可能有助于找到出错的地方。
我无法将新点附加到我当前的点向量的末尾。它目前正在做的是用我要添加的新点覆盖现有向量,使向量只剩下 1 个元素。
这是class定义(.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>);
};
这就是 class 的 .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);
}
}
这就是这两个函数在主函数中的调用方式。请注意,detectBox 是这样声明的:
vector<RECT> 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。
我的 class 定义中的函数有问题(我是 classes 的新手),还是我从主循环调用函数的方式有问题?
这很可能不是您想要的:
person personn;
personn.addposition(pt);
personn.drawhistory(&img_8bit, personn.history);
您的 presonn
位于循环体的本地,因此在每次迭代中您创建一个新的 person
,添加一个位置并打印它。只需在循环外声明 personn
。
PS:这个问题的一个例子可能是这样的:
#include <vector>
#include <iostream>
int main() {
for (int i=0; i<5; i++) {
std::vector<int> vect;
vect.push_back(i);
std::cout << vect.size() << std::endl;
}
}
它重现了您遇到的问题,它进行了编译,并且只包含执行此操作所需的最少代码。如果您自己创建了它,那么您可能自己发现了错误。当然,找到错误的根源并不总是那么容易。调试器可能有助于找到出错的地方。