如何绘制运动时跟踪一个对象

how to draw the movement when tracking an object?

本文关键字:运动 跟踪 一个对象 绘制 何绘制      更新时间:2023-10-16

我使用webcam和使用openCV检索每帧和跟踪对象的位置。

基本上,我在每一帧中都有一个点。但是我怎样才能实时绘制运动图呢?

我是否需要一个计时器来记录特定时间内的几个点并绘制线条?

在while循环中,我只获取一帧,我不认为如果我在当前帧上画一条线,我仍然可以在下一帧中保持这条线。那么我应该如何显示这个运动呢?

while( true )
    {
        //Read the video stream
        capture = cvCaptureFromCAM(1);
        frame = cvQueryFrame( capture );
        //Apply the classifier to the frame
        detectAndDisplay(frame); // I got a point from this function
        // waitkey enter
        int c = waitKey(10);
        if( (char)c == 27 ) { exit(0); } 
    }

使用矢量来保存位置,然后在每一帧上绘制它们。注意,函数需要返回检测到的点。我改了它的名字,因为它在那个点不画。

vector<CvPoint> trajectory;
Vec3b mycolor(100,0,0);
while( true )
{
    //Read the video stream
    capture = cvCaptureFromCAM(1);
    frame = cvQueryFrame( capture );
    //Apply the classifier to the frame
    CvPoint cur_pnt=detect(frame); // I got a point from this function
    trajectory.push_back(cur_point);
    //Draw points.
    for (int i=0;i<trajectory.size();i++)
        frame.at<Vec3b>(trajectory[i].x,trajectory[i].y)=mycolor;
    // waitkey enter
    int c = waitKey(10);
    if( (char)c == 27 ) { exit(0); } 
}