2017-06-06 28 views
0

私はopenCVライブラリが新しく、アンドロイドアプリで学校プロジェクトのリアルタイムオブジェクト検出を試みています。このチュートリアル(https://www.youtube.com/watch?v=bSeFrPrqZ2A)に従って、アンドロイド携帯の色でオブジェクトを検出することができます。今私はこのビデオ(https://www.youtube.com/watch?v=QTYSRZD4vyI)のようにオブジェクトの軌跡を描こうとしています。移動するオブジェクトの軌跡を追跡する方法openCV C++

以下は、最初の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); 

//(traj、フレーム、フレーム)を追加します。 (0、255、0)、2、(0、255)、0、255)。 }

2番目の動画に表示されているオブジェクトの軌跡を取得するには、このコードにどのように追加できますか?どんな提案も大歓迎です。ありがとうございました。それを見つけた

答えて

関連する問題