10

I just have two images,one is the current frame,the other is the optical flow image which calculated by other manners.

The current frame is: enter image description here

The optical flow image is: enter image description here

My question is how to calculate the previous frame using the two images?

I have saw one solution,just using bilinear interpolation to warp current frame to last frame with optical flow image.But I didn't know how to do it.

So,could someone give me some advice or ideas? Thanks a lot.

nsknsl
  • 147
  • 2
  • 11
huoyan
  • 141
  • 1
  • 5

1 Answers1

13

You are looking for the opencv function remap. If you have the current image (currImg) and the optical flow mat (flow) than you can predict the previous image by first inverting the optical flow and then apply the function remap. In python the code will be as follows:

import cv2
h, w = flow.shape[:2]
flow = -flow
flow[:,:,0] += np.arange(w)
flow[:,:,1] += np.arange(h)[:,np.newaxis]
prevImg = cv2.remap(curImg, flow, None, cv.INTER_LINEAR)
Tobias Senst
  • 2,665
  • 17
  • 38
  • Hello! Just curious: what's the point of inverting the optical flow here? If the flow is from prev image to current image, according to cv2.remap's formula, dst(x,y) = src(map_x(x,y),map_y(x,y)). Is your code basically adding the grid to the flow? – Shanmy Dec 16 '21 at 18:29
  • Optical flow is usually competed from previous to current image. If you want to get the previous from the current image, you have to invert the flow direction – Tobias Senst Dec 16 '21 at 22:29