Video tracking (meanshift and camshift algorithms)

import numpy as np
import cv2 as cv
# opencv实现meanshift的api
# cv.meanShift(probImage,window,criteria)
# 参数一:roi区域,目标直方图的反向投影
# 参数二:初始搜索窗口,就是定义roi的rect
# 参数三: 确定窗口搜索的停止准则,主要有迭代数达到最大值,窗口中心的漂移值大于某个设定的限值
# 实现Meanshift的主要流程是∶
# 1.读取视频文件:cv.videoCapture()
# 2.感兴趣区域设置:获取第一帧图像,q并设置目标区域,即感兴趣区域3.计算直方图:计算感兴趣区域的HSV直方图,并进行归一化
# 4.目标追踪∶设置窗口搜索停止条件,直方图反向投影,进行目标追踪,并在目标位置绘制矩形框。
# 1 获取视频
cap = cv.VideoCapture('./DOG.mp4')
# 2 指定追踪目标
ret,frame = cap.read()
r,h,c,w = 197,141,0,208
win = (c,r,w,h)
roi = frame[r:r+h,c:c+w]
# 3 计算直方图
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)
# 4 目标追踪 ,最大迭代10,漂移1
term = (cv.TERM_CRITERIA_EPS|cv.TERM_CRITERIA_COUNT,10,1)
while(True):
    ret,frame = cap.read()
    if ret==True:
        hst=cv.cvtColor(frame,cv.COLOR_BGR2HSV)
        dst=cv.calcBackProject([hst],[0],roi_hist,[0,180],1)
        ret,win = cv.meanShift(dst,win,term)

        x,y,w,h = win
        img2 = cv.rectangle(frame,(x,y),(x+w,y+h),255,2)
        cv.imshow("frame",img2)
        if cv.waitKey(60)&0xFF == ord('q'):
            break
# 5 释放资源
cap.release()
cv.destroyAllWindows()



The full name of the camshift algorithm is "continuously adaptive mean-shift" . It is an improved algorithm for the meanshift algorithm. It can adjust the size of the search window in real time as the size of the tracking target changes, and has better tracking effects.

The Camshift algorithm first applies meanshift, and once the meanshift converges, it updates the size of the window and also calculates the direction of the best-fit ellipse, thereby updating the search window based on the location and size of the target. As shown below:

When Camshift is implemented in opencv, you only need to change the above meanshift to the camshaft function

It is different when drawing a rectangular frame. Meanshift draws a rectangular frame directly, while camshift draws it based on the obtained points.

Will camshift:

ret, track_window = cv.meanShift(dst, track_window, term_crit)

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

 Change to:

#进行camshift追踪
ret, track_window = cv.CamShift(dst, track_window, term_crit)
#绘制追踪结果
pst = cv.boxPoints(ret)
pts = np.int0(pts)
img2 = cv.ploylines(frame,[pts],True,255,2)

Algorithm summary:
Both the meanshift and camshift algorithms have their own advantages and disadvantages:

  • . Meanshift algorithm: Simple, with few iterations, but it cannot solve the occlusion problem of the target and cannot adapt to the shape and size changes of the moving target.
  • . Camshift algorithm: It can adapt to changes in the size and shape of moving targets and has better tracking effects. However, when the background color and the target color are close, it is easy to make the target area larger, which may eventually lead to the loss of target tracking.

 Summarize


1.Meanshift
        principle: an iterative step, that is, first calculate the offset mean of the current point, move the point to its offset mean, and then use this as a new starting point to continue moving until certain conditions are met.
APl: cv.meanshift()
        advantages and disadvantages: simple, few iterations, but it cannot solve the occlusion problem of the target and cannot adapt to the shape and size changes of the moving target. 2. Camshift principle: To improve the
meanshift
        algorithm, first apply meanshift. When meanshift converges, it updates the size of the window and also calculates the direction of the best-fit ellipse to update the search window based on the location and size of the target.
APl: cv.camshift()
Advantages and Disadvantages: It can adapt to changes in the size and shape of moving targets and has better tracking effects. However, when the background color and target color are close, it is easy to make the target area larger, which may eventually cause the target to Tracking lost

Guess you like

Origin blog.csdn.net/qq_44808827/article/details/123628801