Traditional target tracking - CamShift algorithm (improved MeanShift)

Table of contents

1. CamShift

1.1 Principle

2. Process

3. Code

Four. Summary


1. CamShift

        The result of MeanShift has a problem. The size of the detection window is fixed, and the target is a process that gradually becomes smaller from near to far. A fixed window is not suitable. Therefore, the size and angle of the window need to be corrected according to the size and angle of the target.

        Traditional target tracking - meanshift algorithm - My Blog in Parallel World - CSDN Blog

        CamShift (Continuously Adaptive Mean-Shift algorithm) is an improved algorithm of the MeanShift algorithm, which can solve this problem. It can adjust the size of the search window in real time as the size of the tracking target changes, and has a better tracking effect. The Camshift algorithm first applies MeanShift. Once MeanShift converges, it updates the size of the window and at the same time calculates the orientation of the best-fit ellipse, thereby updating the search window according to the location and size of the target.

        camshift explanation

1.1 Principle

        Camshift uses the color histogram model of the target to convert the image into a color probability distribution map, initializes the size and position of a search window, and adaptively adjusts the position and size of the search window according to the results obtained in the previous frame, thereby locating the current image. The center position of the target.

2. Process

         Extending the meanshift algorithm to continuous image sequences is the camshift algorithm. It performs a meanshift operation on all frames of the video, and uses the result of the previous frame, that is, the size and center of the search window, as the initial value of the search window of the meanshift algorithm in the next frame. By iterating in this way, the tracking of the target can be realized.

        The algorithm process is:

(1). Initialize the search window

(2). Calculate the color probability distribution of the search window (reverse projection)

(3). Run the meanshift algorithm to obtain the new size and position of the search window.

(4). Use the value in (3) to re-initialize the size and position of the search window in the next video frame, and then jump to (2) to continue.

3. Code

import cv2 as cv

# 创建读取视频的对象
cap = cv.VideoCapture("E:\Python-Code/videodataset/enn.mp4")

# 获取第一帧位置,并指定目标位置
ret, frame = cap.read()
c, r, h, w = 530, 160, 300, 320
track_window = (c, r, h, w)
# 指定感兴趣区域
roi = frame[r:r + h, c:c + w]

# 计算直方图
# 转换色彩空间
hsv_roi = cv.cvtColor(roi, cv.COLOR_BGR2HSV)
# 计算直方图
roi_hist = cv.calcHist([hsv_roi], [0], None, [180], [0, 180])
# 归一化
cv.normalize(roi_hist, roi_hist, 0, 255, cv.NORM_MINMAX)

# 目标追踪
# 设置窗口搜索终止条件:最大迭代次数,窗口中心漂移最小值
term_crit = (cv.TermCriteria_EPS | cv.TERM_CRITERIA_COUNT, 10, 1)

while True:
    ret, frame = cap.read()
    if ret:
        # 计算直方图的反向投影
        hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
        dst = cv.calcBackProject([hsv], [0], roi_hist, [0, 180], 1)

        # 进行meanshift追踪
        ret, track_window = cv.meanShift(dst, track_window, term_crit)

        # 将追踪的位置绘制在视频上,并进行显示
        x, y, w, h = track_window
        img = cv.rectangle(frame, (x, y), (x + w, y + h), 255, 2)
        cv.imshow("frame", img)

        if cv.waitKey(20) & 0xFF == ord('q'):
            break

    else:
        break

# 资源释放
cap.release()
cv.destroyAllWindows()

        Then the green rectangular box in the running result can adaptively change the size of the box according to the tracked object (note: this Camshift is easy to detect errors)  

Four. Summary

        Camshift can effectively solve the problem of target deformation and occlusion. It has low requirements on system resources and low time complexity. It can achieve good tracking effect in a simple background. But when the background is complex, or there are many pixels with similar color to the target interference, the tracking will fail. Because it simply considers the color histogram and ignores the spatial distribution characteristics of the target, it is necessary to add a prediction algorithm for the tracking target in this case.

Guess you like

Origin blog.csdn.net/weixin_45823221/article/details/128483827