[Opencv] video tracking algorithm KCF

Introduction to KCF Algorithm

KCF (Kernelized Correlation Filter) is a target tracking algorithm based on kernel correlation filter. It performs object localization by learning the appearance features of objects and using kernel correlation filters. KCF belongs to the single object tracker of traditional algorithms. The following is an introduction to the KCF tracking algorithm:

Target feature extraction: The KCF algorithm uses HOG (Histogram of Oriented Gradients) features to represent the appearance of the target. The HOG feature is a local texture feature, which describes the texture information of the target by calculating the gradient direction histogram around each pixel in the image.

Kernel correlation filter: The KCF algorithm uses a kernel correlation filter for target localization. The Kernel Correlation Filter is a method for object localization using the correlation between the filter and the object response. It predicts the location of an object in the next frame by learning the relationship between the object's appearance features and the object's response.

Learning process: The KCF algorithm learns the parameters of the filter by minimizing the error between the filter and the target response. It uses discrete Fourier transform to speed up the calculation of the filter, and updates the parameters of the filter through loops to adapt to changes in the appearance of the object.

Target tracking: In the target tracking phase, the KCF algorithm uses the learned filters to predict the appearance of the target. It correlates the image block of the current frame with the filter to obtain the target response map. Then, the location of the maximum response is found according to the target response graph, which is the location of the target.
The KCF algorithm has the following advantages:

Fast speed: The KCF algorithm uses discrete Fourier transform to accelerate the calculation of the filter, so it has a fast running speed.

Strong robustness: The KCF algorithm has good adaptability to the target's attitude and scale changes.

High accuracy: The KCF algorithm can accurately locate the target by learning the relationship between the target's appearance features and the target response.
The KCF algorithm is widely used in real-time applications and large-scale target tracking, such as video surveillance, pedestrian tracking, etc. It has a corresponding implementation in OpenCV, which can facilitate the development and application of target tracking.

Correlation is a measure of the similarity value of two signals. If the two signals are more similar, the higher the correlation value is. In the application of tracking, it is necessary to design a filter template so that when it acts on the tracking target, The obtained response is the largest, and the position of the maximum response value is the position of the target.

opencv implementation code c++

#include <opencv2/opencv.hpp>
int main()
{
    
    
    cv::VideoCapture capture(0); // 打开摄像头,如果是视频文件,可以指定文件路径
    if (!capture.isOpened())
    {
    
    
        std::cout << "无法打开摄像头或视频文件" << std::endl;
        return -1;
    }
    cv::Mat frame;
    capture.read(frame); // 读取第一帧图像
#v::selectROI函数会显示给定图像,并允许用户通过鼠标交互来选择一个矩形区域。用户可以拖动鼠标来选择矩形的位置和大小。选择完成后,cv::selectROI函数会返回一个cv::Rect2d类型的矩形对象,表示用户选择的感兴趣区域的位置和大小。在给定的代码中,bbox是用来存储用户选择的感兴趣区域的矩形对象。它可以用于后续的操作,比如目标跟踪算法中的初始化,将该矩形作为跟踪目标的初始位置。第二个参数false表示不使用自动调整矩形的大小,用户可以手动拖动鼠标来选择任意大小的矩形区域。
    cv::Rect2d bbox = cv::selectROI(frame, false); // 选择目标对象的初始位置
    cv::Ptr<cv::Tracker> tracker = cv::TrackerKCF::create(); // 创建KCF跟踪器
    tracker->init(frame, bbox); // 初始化跟踪器
    while (capture.read(frame))
    {
    
    
        bool ok = tracker->update(frame, bbox); // 更新跟踪器
        if (ok)
        {
    
    
            cv::rectangle(frame, bbox, cv::Scalar(255, 0, 0), 2, 1); // 绘制跟踪框
        }
        else
        {
    
    
            cv::putText(frame, "跟踪失败", cv::Point(100, 80), cv::FONT_HERSHEY_SIMPLEX, 0.75, cv::Scalar(0, 0, 255), 2);
        }
        cv::imshow("跟踪", frame);
        if (cv::waitKey(1) == 27) // 按下ESC键退出
        {
    
    
            break;
        }
    }
    capture.release();
    cv::destroyAllWindows();
    return 0;
}

This sample program uses the cv::TrackerKCF algorithm for object tracking. It opens the camera or video file, reads the first frame image, and selects the initial position of the target object through the cv::selectROI function. Then, create a cv::Tracker and use the init function to initialize the tracker. In the loop, read new frame images continuously, update the tracker through the update function, and draw the tracking box in the image. Press the ESC key to exit the program.
Note that to compile and run this code, you need to have the OpenCV library installed and linked into your project. Compilation commands can be configured by referring to OpenCV documentation or using CMake.

opencv implementation code python

TrackerKCF_create is a tracker integrated with OpenCV, and the opencv-contrib-python package must be installed before use.

 pip3 install opencv-contrib-python --index-url http://pypi.douban.com/simple/ requests --trusted-host pypi.douban.com
import cv2
# 读取视频帧
cap = cv2.VideoCapture('video.mp4')
ret, frame = cap.read()
# 选择感兴趣区域
bbox = cv2.selectROI(frame, False)
# 初始化跟踪器
tracker = cv2.TrackerKCF_create()
tracker.init(frame, bbox)
# 循环处理视频帧
while True:
    ret, frame = cap.read()
    if not ret:
        break
    # 更新跟踪器
    success, bbox = tracker.update(frame)
    if success:
        # 目标仍然被成功跟踪
        x, y, w, h = [int(v) for v in bbox]
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
    else:
        # 目标丢失
        cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
    # 显示当前帧
    cv2.imshow('Frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# 释放资源
cap.release()
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/hh1357102/article/details/131974618