I applied a function from open CV to have the optical flow between two images. Here is the two images, which I want to have their flow.
This is the code (extracted from opencv doc) I used to extract an optical flow
import cv2
import numpy as np
import matplotlib.pyplot as plt
raster = plt.imread(get_y_fn(fnames[0]))
raster_disp = plt.imread(get_disp_fn(fnames[0]))
prvs = cv2.cvtColor(raster, cv2.COLOR_BGR2GRAY)
next = cv2.cvtColor(raster_disp, cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(raster)
hsv[...,1] = 255
flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 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)
rgb
gives this as output :
I don't understand a few things :
First why are the values of
flow
not integers, but floats. The unique values offlow
are :array([-1.580338e-12, -1.544204e-12, -1.544158e-12, -1.524542e-12, ..., 1.641482e-12, 1.645247e-12, 1.659195e-12, 1.688041e-12], dtype=float32)
Then how to apply the flow that I predicted to get back the source image (here
raster
) fromraster_disp
andflow
?
Or do you have any suggestions to have a matrix (or something else ...) which could give me the "flow" from the two images, and have a function to return to the source image ?
Thank you !