如何跟踪运动物体的轨迹openCV C++

Posted

技术标签:

【中文标题】如何跟踪运动物体的轨迹openCV C++【英文标题】:How to track trajectory of moving object openCV C++ 【发布时间】:2017-06-06 00:24:57 【问题描述】:

我对 openCV 库还很陌生,我正在尝试在 android 应用程序上为学校项目进行实时对象检测。遵循本教程(https://www.youtube.com/watch?v=bSeFrPrqZ2A),我可以在我的安卓手机上通过颜色检测对象。现在,我正在尝试绘制对象的轨迹,就像在这个视频中一样 (https://www.youtube.com/watch?v=QTYSRZD4vyI)。

以下是第一个 youtube 视频中提供的一些源代码。

void searchForMovement(int& x, int& y, Mat& mRgb1, Mat& threshold)

morphOps(threshold);

Mat temp;
threshold.copyTo(temp);
//these two vectors needed for output of findContours
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
//find contours of filtered image using openCV findContours function
//In OpenCV, finding contours is like finding white object from black background.
// So remember, object to be found should be white and background should be black.
//CV_CHAIN_APPROX_SIMPLE to draw 4 points of the contour
findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );

double refArea = 0;
bool objectFound = false;
if (hierarchy.size() > 0) 
    int numObjects = hierarchy.size();
    //if number of objects greater than MAX_NUM_OBJECTS we have a noisy filter
    if(numObjects<MAX_NUM_OBJECTS)
        for (int index = 0; index >= 0; index = hierarchy[index][0]) 

            Moments moment = moments((cv::Mat)contours[index]);
            double area = moment.m00;

            //if the area is less than 20 px by 20px then it is probably just noise
            //if the area is the same as the 3/2 of the image size, probably just a bad filter
            //we only want the object with the largest area so we safe a reference area each
            //iteration and compare it to the area in the next iteration.
            if(area>MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea)
                x = moment.m10/area;
                y = moment.m01/area;
                objectFound = true;
                refArea = area;
            else objectFound = false;


        
        //let user know you found an object
        if(objectFound ==true)
            putText(mRgb1,"Tracking Object",Point(0,50),2,1,Scalar(0,255,0),2);
            //draw object location on screen
            drawObject(x,y,mRgb1);

    else putText(mRgb1,"TOO MUCH NOISE! ADJUST FILTER",Point(0,50),1,2,Scalar(0,0,255),2);

void drawObject(int x, int y,Mat &frame)
Mat traj;
traj = frame;
//use some of the openCV drawing functions to draw crosshairs
//on your tracked image!

//UPDATE:JUNE 18TH, 2013
//added 'if' and 'else' statements to prevent
//memory errors from writing off the screen (ie. (-25,-25) is not within the window!)

circle(frame,Point(x,y),20,Scalar(0,255,0),2);
if(y-25>0)
    line(frame,Point(x,y),Point(x,y-25),Scalar(0,255,0),2);
else line(traj,Point(x,y),Point(x,0),Scalar(0,255,0),2);
if(y+25<FRAME_HEIGHT)
    line(frame,Point(x,y),Point(x,y+25),Scalar(0,255,0),2);
else line(frame,Point(x,y),Point(x,FRAME_HEIGHT),Scalar(0,255,0),2);
if(x-25>0)
    line(traj,Point(x,y),Point(x-25,y),Scalar(0,255,0),2);
else line(frame,Point(x,y),Point(0,y),Scalar(0,255,0),2);
if(x+25<FRAME_WIDTH)
    line(frame,Point(x,y),Point(x+25,y),Scalar(0,255,0),2);
else line(frame,Point(x,y),Point(FRAME_WIDTH,y),Scalar(0,255,0),2);

// add(traj, frame, frame); putText(frame,intToString(x)+","+intToString(y),Point(x,y+30),1,1,Scalar(0,255,0),2);

如何添加到此代码以获取第二个视频中显示的对象的轨迹?任何建议将不胜感激。谢谢。

【问题讨论】:

【参考方案1】:

http://opencv-srf.blogspot.co.uk/2010/09/object-detection-using-color-seperation.html

找到了。在 android 中执行时,需要确保 lastX 和 lastY 也在更新。

【讨论】:

以上是关于如何跟踪运动物体的轨迹openCV C++的主要内容,如果未能解决你的问题,请参考以下文章

求基于OpenCV的三帧差分算法代码

现在想用OPENCV作运动目标的识别和跟踪,用啥方法最好,最快入门?

Unity 物体轨迹移动

如何使用视频对物体进行运动跟踪? [关闭]

用OpenCV怎么才能把运动目标的轨迹用点画出来?

opencv 目标跟踪一定要把被跟踪的物体圈出来吗?