dis_opt_flow.py 3.5 KB

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