参考:https://blog.csdn.net/gjy095/article/details/9226883?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0.no_search_link&spm=1001.2101.3001.4242.1
# encoding:utf-8
'''
Lucas-Kanade tracker
====================
Lucas-Kanade sparse optical flow demo. Uses goodFeaturesToTrack
for track initialization and back-tracking for match verification
between frames.
Usage
-----
lk_track.py [<video_source>]
Keys
----
ESC - exit
'''
import numpy as np
import cv2
# from common import anorm2, draw_str
from time import clock
lk_params = dict(winSize=(15, 15),
maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
feature_params = dict(maxCorners=500,
qualityLevel=0.3,
minDistance=7,
blockSize=7)
class App:
def __init__(self, video_src): # 构造方法,初始化一些参数和视频路径
self.track_len = 10
self.detect_interval = 5
self.tracks = []
self.cam = cv2.VideoCapture(video_src)
self.frame_idx = 0
def run(self): # 光流运行方法
while True:
ret, frame = self.cam.read() # 读取视频帧
if ret == True:
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 转化为灰度虚图像
vis = frame.copy()
if len(self.tracks) > 0: # 检测到角点后进行光流跟踪
img0, img1 = self.prev_gray, frame_gray
p0 = np.float32([tr[-1] for tr in self.tracks]).reshape(-1, 1, 2)
p1, st, err = cv2.calcOpticalFlowPyrLK(img0, img1, p0, None,
**lk_params) # 前一帧的角点和当前帧的图像作为输入来得到角点在当前帧的位置
p0r, st, err = cv2.calcOpticalFlowPyrLK(img1, img0, p1, None,
**lk_params) # 当前帧跟踪到的角点及图像和前一帧的图像作为输入来找到前一帧的角点位置
d = abs(p0 - p0r).reshape(-1, 2).max(-1) # 得到角点回溯与前一帧实际角点的位置变化关系
good = d < 1 # 判断d内的值是否小于1,大于1跟踪被认为是错误的跟踪点
new_tracks = []
for tr, (x, y), good_flag in zip(self.tracks, p1.reshape(-1, 2), good): # 将跟踪正确的点列入成功跟踪点
if not good_flag:
continue
tr.append((x, y))
if len(tr) > self.track_len:
del tr[0]
new_tracks.append(tr)
cv2.circle(vis, (x, y), 2, (0, 255, 0), -1)
self.tracks = new_tracks
cv2.polylines(vis, [np.int32(tr) for tr in self.tracks], False,
(0, 255, 0)) # 以上一振角点为初始点,当前帧跟踪到的点为终点划线
# draw_str(vis, (20, 20), 'track count: %d' % len(self.tracks))
if self.frame_idx % self.detect_interval == 0: # 每5帧检测一次特征点
mask = np.zeros_like(frame_gray) # 初始化和视频大小相同的图像
mask[:] = 255 # 将mask赋值255也就是算全部图像的角点
for x, y in [np.int32(tr[-1]) for tr in self.tracks]: # 跟踪的角点画圆
cv2.circle(mask, (x, y), 5, 0, -1)
p = cv2.goodFeaturesToTrack(frame_gray, mask=mask, **feature_params) # 像素级别角点检测
if p is not None:
for x, y in np.float32(p).reshape(-1, 2):
self.tracks.append([(x, y)]) # 将检测到的角点放在待跟踪序列中
self.frame_idx += 1
self.prev_gray = frame_gray
cv2.imshow('lk_track', vis)
ch = 0xFF & cv2.waitKey(1)
if ch == 27:
break
def main():
import sys
try:
video_src = sys.argv[1]
except:
video_src = "/Users/wangyitian/OneDrive/论文方向/举重/视频资料(1)/右机位/石智勇z-3-168kg成功 破世纪.avi"
print(__doc__)
App(video_src).run()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
参考:https://blog.csdn.net/FireMicrocosm/article/details/48496697?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link
# -*- coding:utf-8 -*-
__author__ = 'Microcosm'
import cv2
import numpy as np
cap = cv2.VideoCapture("E:/python/Python Project/opencv_showimage/videos/visionface.avi")
# 设置 ShiTomasi 角点检测的参数
feature_params = dict( maxCorners=100,
qualityLevel=0.3,
minDistance=7,
blockSize=7 )
# 设置 lucas kanade 光流场的参数
# maxLevel 为使用图像金字塔的层数
lk_params = dict( winSize=(15,15),
maxLevel=2,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# 产生随机的颜色值
color = np.random.randint(0,255,(100,3))
# 获取第一帧,并寻找其中的角点
ret, old_frame = cap.read()
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params)
# 创建一个掩膜为了后面绘制角点的光流轨迹
mask = np.zeros_like(old_frame)
while(1):
ret, frame = cap.read()
if ret:
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 计算能够获取到的角点的新位置
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
# 选取好的角点,并筛选出旧的角点对应的新的角点
good_new = p1[st == 1]
good_old = p0[st == 1]
# 绘制角点的轨迹
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel()
c,d = old.ravel()
cv2.line(mask, (a,b), (c,d), color[i].tolist(), 2)
cv2.circle(frame, (a,b), 5, color[i].tolist(), -1)
img = cv2.add(frame, mask)
cv2.imshow("frame", img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
# 更新当前帧和当前角点的位置
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1,1,2)
else:
break
cv2.destroyAllWindows()
cap.release()
matlab版本
参考:https://blog.csdn.net/sgfmby1994/article/details/68489944?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2defaultCTRLISTdefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2defaultCTRLISTdefault-1.no_search_link
参考:https://blog.csdn.net/SatisfyMyCuriousity/article/details/104703312
matlab代码在链接:https://pan.baidu.com/s/1mlmoPuIDXJS9uP2iSQBT1w 提取码:fvxt