0

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.

input-images

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 :

flow

I don't understand a few things :

  • First why are the values of flow not integers, but floats. The unique values of flow 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) from raster_disp and flow?

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 !

cthnguyen
  • 53
  • 7
  • Not very helpful right this moment, but the next version of skimage will implement optical flow (it should be out in two weeks or so). – Stefan van der Walt Oct 01 '19 at 20:23
  • Your two images are quite different. Optical flow works best when the objects move around 1px per frame. The results may be easier to interpret if you use images that are more alike. – Stephen Meschke Oct 01 '19 at 21:41
  • @StefanvanderWalt Thank you, I already saw that it was in development. I tried to apply their current function but it's not working great at the moment for my case. – cthnguyen Oct 02 '19 at 08:37
  • @StephenMeschke Thank you for your overview ! I will maybe look into something else for my problem. – cthnguyen Oct 02 '19 at 08:38

0 Answers0