OpenCV下的多目标跟踪

OpenCV下的多目标跟踪

flyfish

运行环境
Qt 5.12.2
OS: Ubuntu 18.04
opencv-4.1.0
opencv_contrib-4.1.0

用鼠标画出你要跟踪的目标,按ENTER键,画下一个框,按ESC退出画框开始执行跟踪
将原实例代码改造成使用摄像头跟踪
代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

inline cv::Ptr createTrackerByName(cv::String name)
{
    cv::Ptr tracker;

    if (name == "KCF")
        tracker = cv::TrackerKCF::create();
    else if (name == "TLD")
        tracker = cv::TrackerTLD::create();
    else if (name == "BOOSTING")
        tracker = cv::TrackerBoosting::create();
    else if (name == "MEDIAN_FLOW")
        tracker = cv::TrackerMedianFlow::create();
    else if (name == "MIL")
        tracker = cv::TrackerMIL::create();
    else if (name == "GOTURN")
        tracker = cv::TrackerGOTURN::create();
    else if (name == "MOSSE")
        tracker = cv::TrackerMOSSE::create();
    else if (name == "CSRT")
        tracker = cv::TrackerCSRT::create();
    else
        CV_Error(cv::Error::StsBadArg, "Invalid tracking algorithm name\n");

    return tracker;
}
int main( int argc, char** argv ){


  // set the default tracking algorithm
  std::string trackingAlg = "MEDIAN_FLOW";

  MultiTracker trackers;

  // container of the tracked objects

  vector objects;

  VideoCapture cap(0);

  if(!cap.isOpened())
  {
      std::cout<<"error open."<> frame;
  //! [selectmulti]
  vector ROIs;

  selectROIs("tracker",frame,ROIs);
  //! [selectmulti]

  //quit when the tracked object(s) is not provided
  if(ROIs.size()<1)
    return 0;

  // initialize the tracker
  //! [init]
  std::vector > algorithms;
  for (size_t i = 0; i < ROIs.size(); i++)
  {
      algorithms.push_back(createTrackerByName(trackingAlg));
      objects.push_back(ROIs[i]);
    }

  trackers.add(algorithms,frame,objects);
  //! [init]

  // do the tracking
  printf("Start the tracking process, press ESC to quit.\n");

while (cap.isOpened()){

    cap >> frame;

    //update the tracking result
    trackers.update(frame);

    // draw the tracked object
    for(unsigned i=0;i

Qt配置

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt



INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv4 \
/usr/local/include/opencv4/opencv2 \




LIBS += /usr/local/lib/libopencv_calib3d.so \
         /usr/local/lib/libopencv_core.so    \
        /usr/local/lib/libopencv_highgui.so \
        /usr/local/lib/libopencv_imgproc.so \
        /usr/local/lib/libopencv_imgcodecs.so\
        /usr/local/lib/libopencv_objdetect.so\
        /usr/local/lib/libopencv_photo.so \
        /usr/local/lib/libopencv_dnn.so \
        /usr/local/lib/libopencv_features2d.so \
        /usr/local/lib/libopencv_stitching.so \
        /usr/local/lib/libopencv_flann.so\
        /usr/local/lib/libopencv_videoio.so \
        /usr/local/lib/libopencv_video.so\
        /usr/local/lib/libopencv_ml.so \
        /usr/local/lib/libopencv_tracking.so \


SOURCES += \
        main.cpp

你可能感兴趣的:(多目标跟踪)