title: 2D motion estimation using Python, OpenCV & Kalman filtering
body: <p>I have a set of images, and would like to recursively predict where a bunch of pixels will be in the next image. I am using Python, OpenCV, and believe Kalman filtering may be the way forward, but am struggling on the implementation. For simplicity, the code below opens and image and extracts just one colour channel, in this case the red one.</p>
  
  <p>So far, I am using optical flow to determine the motion between images in X and Y for each pixel. After each iteration, I would like to use the last N iterations, and by using the X/Y motions found each time, calculate the velocity of the pixel, and predict where it will end up in the next frame.  The group of pixels I will look at and predict is not specified, but is not relevant for the example.  It would just be a Numpy array of (x,y) values.</p>
  
  <p>Any help would be greatly appreciated.  Simplified code snippet below:</p>
  
  <pre><code>import numpy as np
  import cv2
  from PIL import Image
  
  imageNames = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg"]
  
  for i in range(len(imageNames)):
      # Load images and extract just one colour channel (e.g., red)
      image1 = Image.open(imageNames[i])
      image2 = Image.open(imageNames[i+1])
      image1R = np.asarray(image1[:,:,0]).astype(np.uint8)
      image2R = np.asarray(image2[:,:,0]).astype(np.uint8)
  
      # Get optical flow
      flow = cv2.calcOpticalFlowFarneback(image1R, image2R, 0.5, 1, 5, 15, 10, 5, 1)
      change_in_x = flow[:,:,0]
      change_in_y = flow[:,:,1]
  
      # Use previous flows to obtain velocity in x and y
  
      # For a subset of the image, predict where points will be in the next image
      # Use Kalman filtering?
  
      # Repeat recursively
  </code></pre>