Eight different target tracking algorithms on OpenCV (return)

Reprinted from: http://m.elecfans.com/article/722414.html

Eight different target tracking algorithms on OpenCV

Li Qian published on 2018-08-05 20257

 

description

Editor's note: Target tracking is an important branch of machine learning, coupled with its wide application in daily life and military operations, many domestic and foreign scholars have studied this. This article will discuss eight different target tracking algorithms on OpenCV.

Although the well-known centroid tracker performs well, it requires us to run a target detector on every frame of the input video. For most environments, detecting on each frame is very computationally intensive.

Therefore, we want to apply a one-time target detection method, and then target tracking can be carried out on subsequent frames to make this task faster and more efficient.

The question here is: Can OpenCV help us achieve this goal tracking?

The answer is yes.

OpenCV target tracking

First, we will roughly introduce eight target detection algorithms built on OpenCV. Later I will explain how to use these algorithms for real-time target tracking. Finally, we will compare the effects of various OpenCV target tracking and summarize the environments that various methods can adapt to.

Eight OpenCV target tracking installations

Video taken by drone, using MedianFlow for target tracking

You may be surprised that OpenCV has eight different target tracking tools, all of which can be used in the field of computer vision.

The eight tools include:

BOOSTING Tracker: Same as the machine learning algorithm behind Haar cascades (AdaBoost), but it has been more than ten years since its birth. This tracker is slower and does not perform well, but it is necessary to mention it as a veteran. (Minimum support for OpenCV 3.0.0)

MIL Tracker: It is more accurate than the previous tracker, but the failure rate is higher. (Minimum support for OpenCV 3.0.0)

KCF Tracker: Faster than BOOSTING and MIL, but it performs poorly under occlusion. (Minimum support for OpenCV 3.1.0)

CSRT Tracker: Slightly more accurate than KCF, but not as fast as the latter. (Minimum support for OpenCV 3.4.2)

MedianFlow Tracker: It performs well in reporting errors, but for fast-jumping or fast-moving objects, the model will fail. (Minimum support for OpenCV 3.0.0)

TLD Tracker: I'm not sure if there is any incompatibility between OpenCV and TLD, but TLD has a lot of false positives, so it is not recommended. (Minimum support for OpenCV 3.0.0)

MOSSE Tracker: The speed is really fast, but not as high as the accuracy of CSRT and KCF. If you are looking for speed, choose it. (Minimum support for OpenCV 3.4.1)

GOTURN Tracker: This is the only deep learning-based target detector in OpenCV. It requires an additional model to run, and this article will not explain it in detail. (Minimum support for OpenCV 3.2.0)

My personal suggestion:

If you pursue high accuracy and can tolerate slower speeds, then use CSRT

If the accuracy requirements are not harsh and you want to pursue speed, then choose KCF

Just use MOSSE if you want to save time

Starting from OpenCV 3, object detectors have been developed rapidly. The following table summarizes the edible trackers in different versions of OpenCV:

Get started

To use OpenCV for target tracking, first open a new file, name it opencv_object_tracker.py, and insert the following code:

We start to enter the necessary installation package, make sure you have installed OpenCV (I recommend version 3.4 or higher), and then you have to install imutils:

After entering the installation package, we begin to analyze the command line parameters:

Our command line parameters include:

--video: Alternative route to the input video file. If this parameter is invalid, the script will use your webcam.

--tracker: Assuming that the default tracker is set to kcf, a whole list of possible tracker codes represents the next code block or the part below.

Let's deal with the different categories of trackers:

In Figure 2, we mentioned that not all trackers use OpenCV 3 or higher. In version 3.3, the same installation changes have taken place. Before 3.3, the tracker must be created with cv2. Tracker_create, and the name of the tracker must be marked with an uppercase string (lines 22 and 23).

For versions above 3.3, each tracker can be created with its own function, such as cv2. TrackerKCF_create. The dictionary OPENCV_OBJECT_TRACKERS contains 7 OpenCV object trackers (lines 30-38). It maps the command line parameter string of the target tracker to the actual OpenCV tracker function.

The purpose of tracker in line 42 is based on tracker command line parameters and related important information from OPENCV_OBJECT_TRACKERS.

Note: I did not add GOTURN to the tracker settings here because it requires additional model files.

We also initialize initBB (line 46). When we select the target object with the mouse, this variable will display the bounding box coordinates of the target object.

Next, let us initialize the video stream and FPS:

Lines 49-52 are the steps to access the webcam. Here we set a one-second pause time to allow the camera sensor to "warm up".

Then the --video command line parameter will appear, so we can initialize the video stream from the video file (line 55-56).

Here are the steps to iterate the frame number from the video stream:

We extract a frame in lines 65 and 66, and deal with the situation where there is no frame number in the video file in lines 69 and 70.

In order to make our algorithm process the number of frames faster, we use resize to adjust the input video frame to 50 pixels (74 lines). The less data processed here, the faster the speed.

After that, we extract the width and height of the video frame, and then we will use the height (75 lines).

After the target object is selected, we can use the following code to process:

If the target object has been selected, we need to continuously update the position of the target object. To do this, we use the update method on line 80, which will locate the new position of the target object and return a success and box value.

If all goes well, we can get the updated bounding box position in the frame. Note that the tracker may follow the target and report an error, so success may not always be True. Then update the FPS estimator.

Next, let us show the frame and select the target object with the mouse:

We will display the frame, and continue to iterate the loop, and then stop by typing other instructions.

After typing "s", we use cv2.selectROI to "select" a target ROI. This function allows you to manually select an ROI when the video is paused:

The user must draw the bounding box and press Enter or Space to confirm the selected area. If you need to reselect, just press the "ESCAPE" key.

Similarly, we can also use real target detectors for manual selection.

Finally, if the video has more frames or a "quit" situation occurs, how to exit this loop:

The last module shows how we can stop the loop, when all indicators are output and the window is closed.

Target tracking results

Tip: To ensure that you keep up with the progress of this article and use the OpenCV method in the article, please make sure that you download the code and video in the "Download" section.

After that, open a terminal and execute the following command:

If you downloaded the source code and video of this tutorial, the available parameters of --video are in the following files:

american_pharoah.mp4

dashcam_boston.mp4

drone.mp4

nascar_01.mp4

nascar_02.mp4

race.mp4

……

The parameters in --tracker are:

csrt

kcf

boosting

one thousand

tld

medianflow

moves

You can also use the computer's camera:

The following example will show how the OpenCV tracker can track over a longer period of time and compare it with a short period of time:

to sum up

This article explains how to use OpenCV for target object tracking. Specifically, we reviewed the eight algorithms of Curry:

CSRF

KCF

Boosting

ONE THOUSAND

TLD

MedianFlow

MOVES

GOTURN

We can use these eight trackers of OpenCV for different tasks, including sprinting, horse racing, racing, drone tracking and other high-speed video.

Guess you like

Origin blog.csdn.net/sinat_16643223/article/details/114990663