OpenCV 3.2 Tracking 物體跟蹤

bbzz2發表於2017-07-20

跟蹤就是在連續視訊幀中定位物體,通常的跟蹤演算法包括以下幾類:

1. Dense OpticalFlow 稠密光流

2. Sparse OpticalFlow 稀疏光流 最典型的如KLT演算法(Kanade-Lucas-Tomshi)

3. Kalman Filter

4. Meanshift andCamshift

5. Multiple objecttracking

需要注意跟蹤和識別的區別,通常來說跟蹤可以比識別快很多,且跟蹤失敗了可以找回來。

OpenCV 3以後實現了很多追蹤演算法,都實現在contrib模組中,安裝參考

下面code實現了跟蹤筆記本攝像頭畫面中的固定區域物體,可以選用OpenCV實現的演算法

#include<opencv2/opencv.hpp>

#include<opencv2/tracking.hpp>

 

usingnamespace std;

usingnamespace cv;

 

int main(int argc,char** argv){

  // can change to BOOSTING, MIL, KCF (OpenCV 3.1), TLD, MEDIANFLOW, orGOTURN (OpenCV 3.2)

  Ptr<Tracker> tracker = Tracker::create("MEDIANFLOW");

  VideoCapture video(0);

  if(!video.isOpened()){

    cerr << "cannot read video!" << endl;

    return -1;

  }

  Mat frame;

  video.read(frame);

  Rect2d box(270,120, 180, 260);

  tracker->init(frame, box);

  while(video.read(frame)){

    tracker->update(frame, box);

    rectangle(frame, box, Scalar(255,0, 0), 2, 1);

    imshow("Tracking", frame);

    int k=waitKey(1);

    if(k==27) break;

  }

}


這裡介紹一下使用OpenCV進行目標跟蹤的方法,之前用過一點。opencv中目標跟蹤的一些比較新的方法(比如MIL、KCF、TLD等)都在opencv_contrib庫裡,所以首先你需要安裝它,詳見Opencv3.1.0+opencv_contrib配置及使用SIFT測試 。 
關於tracking這一塊可參考Opencv的官網 Tracking API 、Introduction to OpenCV Tracker 。 
如果只是想要測試下非常簡單,官網都給了例程;要深入瞭解其原理,檢視相關論文即可。關於其效果的話,看你的使用場合和怎麼理解了。 
這裡貼一下我根據例程稍作修改的測試程式。 
原始碼:

#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>
using namespace std;
using namespace cv;

int main(int argc, char** argv) {
    // show help
    /*
    if (argc<2) {
        cout <<
            " Usage: tracker <video_name>\n"
            " examples:\n"
            " example_tracking_kcf Bolt/img/%04d.jpg\n"
            " example_tracking_kcf faceocc2.webm\n"
            << endl;
        return 0;
    }
    */
    // declares all required variables
    Rect2d roi;
    Mat frame;
    // create a tracker object
    Ptr<Tracker> tracker = Tracker::create("KCF");
    // set input video
//  std::string video = argv[1];
    VideoCapture cap("dount.avi");
    // get bounding box
    cap >> frame;
    roi = selectROI("tracker", frame);
    //quit if ROI was not selected
    if (roi.width == 0 || roi.height == 0)
        return 0;
    // initialize the tracker
    tracker->init(frame, roi);
    // perform the tracking process
    printf("Start the tracking process, press ESC to quit.\n");
    for (;; ) {
        // get frame from the video
        cap >> frame;
        // stop the program if no more images
        if (frame.rows == 0 || frame.cols == 0)
            break;
        // update the tracking result
        tracker->update(frame, roi);
        // draw the tracked object
        rectangle(frame, roi, Scalar(255, 0, 0), 2, 1);
        // show image with the tracked object
        imshow("tracker", frame);
        //quit on ESC button
        if (waitKey(1) == 27)break;
    }
    return 0;
}

#include <iostream>
#include <cstring>
using namespace std;
using namespace cv;
int main( int argc, char** argv ){
// show help
if(argc<2){
cout<<
" Usage: tracker <video_name>\n"
" examples:\n"
" example_tracking_kcf Bolt/img/%04d.jpg\n"
" example_tracking_kcf faceocc2.webm\n"
<< endl;
return 0;
}
// declares all required variables
Rect2d roi;
Mat frame;
// create a tracker object
Ptr<Tracker> tracker = Tracker::create( "KCF" );
// set input video
std::string video = argv[1];
VideoCapture cap(video);
// get bounding box
cap >> frame;
roi=selectROI("tracker",frame);
//quit if ROI was not selected
if(roi.width==0 || roi.height==0)
return 0;
// initialize the tracker
tracker->init(frame,roi);
// perform the tracking process
printf("Start the tracking process, press ESC to quit.\n");
for ( ;; ){
// get frame from the video
cap >> frame;
// stop the program if no more images
if(frame.rows==0 || frame.cols==0)
break;
// update the tracking result
tracker->update(frame,roi);
// draw the tracked object
rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
// show image with the tracked object
imshow("tracker",frame);
//quit on ESC button
if(waitKey(1)==27)break;
}
return 0;
}


相關文章