opencv跟踪算法

前言

OpenCV有8种不同的跟踪器类型:BOOSTING,MIL,KCF,TLD,MEDIANFLOW,GOTURN,MOSSE,CSRT。

一、OPENCV TRACK

c++代码如下:

C++代码:

#include "pch.h"
#include 
#include 
#include 
using namespace cv;
using namespace std;
vector<string> trackerTypes = {"BOOSTING", "MIL", "KCF", "TLD", "MEDIANFLOW", "GOTURN", "MOSSE", "CSRT"};

Ptr<Tracker> createTrackerByName(string trackerType)
{
  Ptr<Tracker> tracker;
  if (trackerType == trackerTypes[0])
      tracker = TrackerBoosting::create();
  else if (trackerType == trackerTypes[1])
      tracker = TrackerMIL::create();
  else if (trackerType == trackerTypes[2])
      tracker = TrackerKCF::create();
  else if (trackerType == trackerTypes[3])
      tracker = TrackerTLD::create();
  else if (trackerType == trackerTypes[4])
      tracker = TrackerMedianFlow::create();
  else if (trackerType == trackerTypes[5])
      tracker = TrackerGOTURN::create();
  else if (trackerType == trackerTypes[6])
      tracker = TrackerMOSSE::create();
  else if (trackerType == trackerTypes[7])
      tracker = TrackerCSRT::create();
  else
  {
      cout << "Incorrect tracker name" << endl;
      cout << "Available trackers are: " << endl;
      for (vector<string>::iterator it = trackerTypes.begin(); it != trackerTypes.end(); ++it)
      {
          std::cout << " " << *it << endl;
      }
  }
  return tracker;
}

/**
* @brief Get the Random Colors object 随机涂色
*
* @param colors
* @param numColors
*/
void getRandomColors(vector<Scalar> &colors, int numColors)
{
  RNG rng(0);
  for (int i = 0; i < numColors; i++)
  {
      colors.push_back(Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)));
  }
}
int main(int argc, char *argv[])
{
  // Set tracker type. Change this to try different trackers. 选择追踪器类型
  string trackerType = trackerTypes[7];
  // set default values for tracking algorithm and video 视频读取
  string videoPath = "video/run.mp4";

  // Initialize MultiTracker with tracking algo 边界框
  vector<Rect> bboxes;

  // create a video capture object to read videos 读视频
  cv::VideoCapture cap(videoPath);
  Mat frame;

  // quit if unable to read video file
  if (!cap.isOpened())
  {
      cout << "Error opening video file " << videoPath << endl;
      return -1;
  }
  // read first frame 读第一帧
  cap >> frame;
  // draw bounding boxes over objects 在第一帧内确定对象框
  /*
      先在图像上画框,然后按ENTER确定画下一个框。按ESC退出画框开始执行程序
  */
  cout << "\n==========================================================\n";
  cout << "OpenCV says press c to cancel objects selection process" << endl;
  cout << "It doesn't work. Press Esc to exit selection process" << endl;
  cout << "\n==========================================================\n";
  cv::selectROIs("MultiTracker", frame, bboxes, false);

  //自己设定对象的检测框
  //x,y,width,height
  //bboxes.push_back(Rect(388, 155, 30, 40));
  //bboxes.push_back(Rect(492, 205, 50, 80));
  // quit if there are no objects to track 如果没有选择对象
  if (bboxes.size() < 1)
  {
      return 0;
  }
  vector<Scalar> colors;
  //给各个框涂色
  getRandomColors(colors, bboxes.size());
  // Create multitracker 创建多目标跟踪类
  Ptr<MultiTracker> multiTracker = cv::MultiTracker::create();
  // initialize multitracker 初始化
  for (int i = 0; i < bboxes.size(); i++)
  {
      multiTracker->add(createTrackerByName(trackerType), frame, Rect2d(bboxes[i]));
  }

  // process video and track objects 开始处理图像
  cout << "\n==========================================================\n";
  cout << "Started tracking, press ESC to quit." << endl;
  while (cap.isOpened())
  {
      // get frame from the video 逐帧处理
      cap >> frame;
      // stop the program if reached end of video
      if (frame.empty())
      {
          break;
      }
      //update the tracking result with new frame 更新每一帧
      bool ok = multiTracker->update(frame);
      if (ok == true)
      {
          cout << "Tracking success" << endl;
      }
      else
      {
          cout << "Tracking failure" << endl;
      }
      // draw tracked objects 画框
      for (unsigned i = 0; i < multiTracker->getObjects().size(); i++)
      {
          rectangle(frame, multiTracker->getObjects()[i], colors[i], 2, 1);
      }

      // show frame
      imshow("MultiTracker", frame);

      // quit on x button
      if (waitKey(1) == 27)
      {
          break;
      }
  }
  waitKey(0);
  return 0;
}

python版本代码如下:

#python代码 
from __future__ import print_function
import sys
import cv2
from random import randint
trackerTypes = ['BOOSTING', 'MIL', 'KCF','TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
def createTrackerByName(trackerType):
# Create a tracker based on tracker name
if trackerType == trackerTypes[0]:
  tracker = cv2.TrackerBoosting_create()
elif trackerType == trackerTypes[1]:
  tracker = cv2.TrackerMIL_create()
elif trackerType == trackerTypes[2]:
  tracker = cv2.TrackerKCF_create()
elif trackerType == trackerTypes[3]:
  tracker = cv2.TrackerTLD_create()
elif trackerType == trackerTypes[4]:
  tracker = cv2.TrackerMedianFlow_create()
elif trackerType == trackerTypes[5]:
  tracker = cv2.TrackerGOTURN_create()
elif trackerType == trackerTypes[6]:
  tracker = cv2.TrackerMOSSE_create()
elif trackerType == trackerTypes[7]:
  tracker = cv2.TrackerCSRT_create()
else:
  tracker = None
  print('Incorrect tracker name')
  print('Available trackers are:')
  for t in trackerTypes:
    print(t)

return tracker

if __name__ == '__main__':

print("Default tracking algoritm is CSRT \n"
      "Available tracking algorithms are:\n")
for t in trackerTypes:
    print(t)

trackerType = "CSRT"

# Set video to load
videoPath = "video/run.mp4"

# Create a video capture object to read videos
cap = cv2.VideoCapture(videoPath)

# Read first frame
success, frame = cap.read()
# quit if unable to read the video file
if not success:
  print('Failed to read video')
  sys.exit(1)

## Select boxes
bboxes = []
colors = []

# OpenCV's selectROI function doesn't work for selecting multiple objects in Python
# So we will call this function in a loop till we are done selecting all objects
while True:
  # draw bounding boxes over objects
  # selectROI's default behaviour is to draw box starting from the center
  # when fromCenter is set to false, you can draw box starting from top left corner
  bbox = cv2.selectROI('MultiTracker', frame)
  bboxes.append(bbox)
  colors.append((randint(64, 255), randint(64, 255), randint(64, 255)))
  print("Press q to quit selecting boxes and start tracking")
  print("Press any other key to select next object")
  k = cv2.waitKey(0) &amp; 0xFF
  if (k == 113):  # q is pressed
    break

print('Selected bounding boxes {}'.format(bboxes))

## Initialize MultiTracker
# There are two ways you can initialize multitracker
# 1. tracker = cv2.MultiTracker("CSRT")
# All the trackers added to this multitracker
# will use CSRT algorithm as default
# 2. tracker = cv2.MultiTracker()
# No default algorithm specified

# Initialize MultiTracker with tracking algo
# Specify tracker type

# Create MultiTracker object
multiTracker = cv2.MultiTracker_create()

# Initialize MultiTracker
for bbox in bboxes:
  multiTracker.add(createTrackerByName(trackerType), frame, bbox)

# Process video and track objects
while cap.isOpened():
  success, frame = cap.read()
  if not success:
    break
  # get updated location of objects in subsequent frames
  success, boxes = multiTracker.update(frame)

  # draw tracked objects
  for i, newbox in enumerate(boxes):
    p1 = (int(newbox[0]), int(newbox[1]))
    p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
    cv2.rectangle(frame, p1, p2, colors[i], 2, 1)
  # show frame
  cv2.imshow('MultiTracker', frame)

  # quit on ESC button
  if cv2.waitKey(1) &amp; 0xFF == 27:  # Esc pressed
    break

你可能感兴趣的:(robomaster竞赛,opencv,算法,人工智能)