OpenCV  5.0.0alpha-pre
Open Source Computer Vision
Loading...
Searching...
No Matches
samples/python/snippets/lk_track.py

An example using the Lucas-Kanade optical flow algorithm in python

1#!/usr/bin/env python
2
3'''
4Lucas-Kanade tracker
5====================
6
7Lucas-Kanade sparse optical flow demo. Uses goodFeaturesToTrack
8for track initialization and back-tracking for match verification
9between frames.
10
11Usage
12-----
13lk_track.py [<video_source>]
14
15
16Keys
17----
18ESC - exit
19'''
20
21# Python 2/3 compatibility
22from __future__ import print_function
23
24import numpy as np
25import cv2 as cv
26
27import video
28from common import anorm2, draw_str
29
30lk_params = dict( winSize = (15, 15),
31 maxLevel = 2,
32 criteria = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03))
33
34feature_params = dict( maxCorners = 500,
35 qualityLevel = 0.3,
36 minDistance = 7,
37 blockSize = 7 )
38
39class App:
40 def __init__(self, video_src):
41 self.track_len = 10
42 self.detect_interval = 5
43 self.tracks = []
44 self.cam = video.create_capture(video_src)
45 self.frame_idx = 0
46
47 def run(self):
48 while True:
49 _ret, frame = self.cam.read()
50 frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
51 vis = frame.copy()
52
53 if len(self.tracks) > 0:
54 img0, img1 = self.prev_gray, frame_gray
55 p0 = np.float32([tr[-1] for tr in self.tracks]).reshape(-1, 1, 2)
56 p1, _st, _err = cv.calcOpticalFlowPyrLK(img0, img1, p0, None, **lk_params)
57 p0r, _st, _err = cv.calcOpticalFlowPyrLK(img1, img0, p1, None, **lk_params)
58 d = abs(p0-p0r).reshape(-1, 2).max(-1)
59 good = d < 1
60 new_tracks = []
61 for tr, (x, y), good_flag in zip(self.tracks, p1.reshape(-1, 2), good):
62 if not good_flag:
63 continue
64 tr.append((x, y))
65 if len(tr) > self.track_len:
66 del tr[0]
67 new_tracks.append(tr)
68 cv.circle(vis, (int(x), int(y)), 2, (0, 255, 0), -1)
69 self.tracks = new_tracks
70 cv.polylines(vis, [np.int32(tr) for tr in self.tracks], False, (0, 255, 0))
71 draw_str(vis, (20, 20), 'track count: %d' % len(self.tracks))
72
73 if self.frame_idx % self.detect_interval == 0:
74 mask = np.zeros_like(frame_gray)
75 mask[:] = 255
76 for x, y in [np.int32(tr[-1]) for tr in self.tracks]:
77 cv.circle(mask, (x, y), 5, 0, -1)
78 p = cv.goodFeaturesToTrack(frame_gray, mask = mask, **feature_params)
79 if p is not None:
80 for x, y in np.float32(p).reshape(-1, 2):
81 self.tracks.append([(x, y)])
82
83
84 self.frame_idx += 1
85 self.prev_gray = frame_gray
86 cv.imshow('lk_track', vis)
87
88 ch = cv.waitKey(1)
89 if ch == 27:
90 break
91
92def main():
93 import sys
94 try:
95 video_src = sys.argv[1]
96 except:
97 video_src = 0
98
99 App(video_src).run()
100 print('Done')
101
102
103if __name__ == '__main__':
104 print(__doc__)
105 main()
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void destroyAllWindows()
Destroys all of the HighGUI windows.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws several polygonal curves.
void circle(InputOutputArray img, Point center, int radius, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a circle.
void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners, double qualityLevel, double minDistance, InputArray mask=noArray(), int blockSize=3, bool useHarrisDetector=false, double k=0.04)
Determines strong corners on an image.
void calcOpticalFlowPyrLK(InputArray prevImg, InputArray nextImg, InputArray prevPts, InputOutputArray nextPts, OutputArray status, OutputArray err, Size winSize=Size(21, 21), int maxLevel=3, TermCriteria criteria=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01), int flags=0, double minEigThreshold=1e-4)
Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with pyra...
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3