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

An example using the dense optical flow and DIS optical flow algorithms in python

1#!/usr/bin/env python
2
3'''
4example to show optical flow estimation using DISOpticalFlow
5
6USAGE: dis_opt_flow.py [<video_source>]
7
8Keys:
9 1 - toggle HSV flow visualization
10 2 - toggle glitch
11 3 - toggle spatial propagation of flow vectors
12 4 - toggle temporal propagation of flow vectors
13ESC - exit
14'''
15
16# Python 2/3 compatibility
17from __future__ import print_function
18
19import numpy as np
20import cv2 as cv
21
22import video
23
24
25def draw_flow(img, flow, step=16):
26 h, w = img.shape[:2]
27 y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1).astype(int)
28 fx, fy = flow[y,x].T
29 lines = np.vstack([x, y, x+fx, y+fy]).T.reshape(-1, 2, 2)
30 lines = np.int32(lines + 0.5)
31 vis = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
32 cv.polylines(vis, lines, 0, (0, 255, 0))
33 for (x1, y1), (_x2, _y2) in lines:
34 cv.circle(vis, (x1, y1), 1, (0, 255, 0), -1)
35 return vis
36
37
38def draw_hsv(flow):
39 h, w = flow.shape[:2]
40 fx, fy = flow[:,:,0], flow[:,:,1]
41 ang = np.arctan2(fy, fx) + np.pi
42 v = np.sqrt(fx*fx+fy*fy)
43 hsv = np.zeros((h, w, 3), np.uint8)
44 hsv[...,0] = ang*(180/np.pi/2)
45 hsv[...,1] = 255
46 hsv[...,2] = np.minimum(v*4, 255)
47 bgr = cv.cvtColor(hsv, cv.COLOR_HSV2BGR)
48 return bgr
49
50
51def warp_flow(img, flow):
52 h, w = flow.shape[:2]
53 flow = -flow
54 flow[:,:,0] += np.arange(w)
55 flow[:,:,1] += np.arange(h)[:,np.newaxis]
56 res = cv.remap(img, flow, None, cv.INTER_LINEAR)
57 return res
58
59
60def main():
61 import sys
62 print(__doc__)
63 try:
64 fn = sys.argv[1]
65 except IndexError:
66 fn = 0
67
68 cam = video.create_capture(fn)
69 _ret, prev = cam.read()
70 prevgray = cv.cvtColor(prev, cv.COLOR_BGR2GRAY)
71 show_hsv = False
72 show_glitch = False
73 use_spatial_propagation = False
74 use_temporal_propagation = True
75 cur_glitch = prev.copy()
76 inst = cv.DISOpticalFlow.create(cv.DISOPTICAL_FLOW_PRESET_MEDIUM)
77 inst.setUseSpatialPropagation(use_spatial_propagation)
78
79 flow = None
80 while True:
81 _ret, img = cam.read()
82 gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
83 if flow is not None and use_temporal_propagation:
84 #warp previous flow to get an initial approximation for the current flow:
85 flow = inst.calc(prevgray, gray, warp_flow(flow,flow))
86 else:
87 flow = inst.calc(prevgray, gray, None)
88 prevgray = gray
89
90 cv.imshow('flow', draw_flow(gray, flow))
91 if show_hsv:
92 cv.imshow('flow HSV', draw_hsv(flow))
93 if show_glitch:
94 cur_glitch = warp_flow(cur_glitch, flow)
95 cv.imshow('glitch', cur_glitch)
96
97 ch = 0xFF & cv.waitKey(5)
98 if ch == 27:
99 break
100 if ch == ord('1'):
101 show_hsv = not show_hsv
102 print('HSV flow visualization is', ['off', 'on'][show_hsv])
103 if ch == ord('2'):
104 show_glitch = not show_glitch
105 if show_glitch:
106 cur_glitch = img.copy()
107 print('glitch is', ['off', 'on'][show_glitch])
108 if ch == ord('3'):
109 use_spatial_propagation = not use_spatial_propagation
110 inst.setUseSpatialPropagation(use_spatial_propagation)
111 print('spatial propagation is', ['off', 'on'][use_spatial_propagation])
112 if ch == ord('4'):
113 use_temporal_propagation = not use_temporal_propagation
114 print('temporal propagation is', ['off', 'on'][use_temporal_propagation])
115
116 print('Done')
117
118
119if __name__ == '__main__':
120 print(__doc__)
121 main()
static Ptr< DISOpticalFlow > create(int preset=DISOpticalFlow::PRESET_FAST)
Creates an instance of DISOpticalFlow.
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 remap(InputArray src, OutputArray dst, InputArray map1, InputArray map2, int interpolation, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar(), AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Applies a generic geometrical transformation to an image.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3