如何跟踪移动对象openCV C ++的轨迹

时间:2017-06-06 00:24:57

标签: android c++ opencv computer-vision

我是openCV库的新手,我正在尝试为Android应用程序上的学校项目进行实时对象检测。按照本教程(https://www.youtube.com/watch?v=bSeFrPrqZ2A),我能够在我的Android手机上按颜色检测对象。现在我试图像在这个视频中那样绘制对象的轨迹(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(帧,intToString(X)+ “” + intToString(Y),点(X,Y + 30),1,1,标量(0,255,0),2); }

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

1 个答案:

答案 0 :(得分:0)

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

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