Skip to content

Instantly share code, notes, and snippets.

@insaneyilin
Created November 15, 2019 02:09
Show Gist options
  • Save insaneyilin/e9120bdcde690ee52c40fa4bdf26ae19 to your computer and use it in GitHub Desktop.
Save insaneyilin/e9120bdcde690ee52c40fa4bdf26ae19 to your computer and use it in GitHub Desktop.
Optical Flow examples
from matplotlib import pyplot as plt
%matplotlib inline
from skimage.color import hsv2rgb
from skimage.color import rgb2gray
from skimage.data import stereo_motorcycle
from skimage.registration import optical_flow_tvl1
import numpy as np
image0, image1, disp = stereo_motorcycle()
# convert the images to gray level: color is not supported.
image0 = rgb2gray(image0)
image1 = rgb2gray(image1)
flow = optical_flow_tvl1(image1, image0)
# display dense optical flow
flow_x = flow[1, :, :]
flow_y = flow[0, :, :]
mag_map = np.hypot(flow_x, flow_y) # magnitude
mag_map *= 255 / mag_map.max()
mag_map = mag_map.astype('uint8')
angle_map = np.arctan2(flow_y, flow_x) + np.pi # direction
angle_map *= (180./np.pi)
angle_map = np.clip(angle_map, 0, 180)
angle_map = angle_map.astype('uint8')
# hsv color space
s_map = (np.ones(mag_map.shape) * 255).astype('uint8')
img_hsv = np.dstack((angle_map, s_map, mag_map))
img_rgb = hsv2rgb(img_hsv)
plt.imshow(img_rgb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment