title: Optical Flow using OpenCV - Horizontal and Vertical Components
body: <p>I have the following code that finds the Optical Flow of 2 images (or 2 frames of a video) and it's colour coded.  What I want is the horizontal and vertical components of the optical flow separately (as in separate images)</p>
  
  <p>Here is the code I have so far:</p>
  
  <pre><code>import cv2
  import numpy as np
  frame1 = cv2.imread('my1.bmp')
  frame2 = cv2.imread('my2.bmp')
  prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
  next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
  hsv = np.zeros_like(frame1)
  hsv[...,1] = 255
  
  while(1):
      next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
      flow = cv2.calcOpticalFlowFarneback(prvs, next, 0.5, 3, 15, 3, 5, 1.2, 0)
      mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
      hsv[...,0] = ang*180/np.pi/2
      hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
      rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)
  
      cv2.imshow('frame2',rgb)
      k = cv2.waitKey(30) &amp; 0xff
      if k == 27:
          break
      elif k == ord('s'):
          cv2.imwrite('opticalmyhsv.pgm',rgb)
  
  cap.release()
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>This is what the optical flow looks like given my two images:</p>
  
  <p><img src="https://i.stack.imgur.com/1ENxe.png" alt=""></p>