Spaces:
Runtime error
Runtime error
Commit ·
7200753
1
Parent(s): 2f95a04
Create raft_core_utils_flow_viz.py
Browse files- raft_core_utils_flow_viz.py +125 -0
raft_core_utils_flow_viz.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Flow visualization code used from https://github.com/tomrunia/OpticalFlow_Visualization
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# MIT License
|
| 5 |
+
#
|
| 6 |
+
# Copyright (c) 2018 Tom Runia
|
| 7 |
+
#
|
| 8 |
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 9 |
+
# of this software and associated documentation files (the "Software"), to deal
|
| 10 |
+
# in the Software without restriction, including without limitation the rights
|
| 11 |
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 12 |
+
# copies of the Software, and to permit persons to whom the Software is
|
| 13 |
+
# furnished to do so, subject to conditions.
|
| 14 |
+
#
|
| 15 |
+
# Author: Tom Runia
|
| 16 |
+
# Date Created: 2018-08-03
|
| 17 |
+
|
| 18 |
+
import numpy as np
|
| 19 |
+
|
| 20 |
+
def make_colorwheel():
|
| 21 |
+
"""
|
| 22 |
+
Generates a color wheel for optical flow visualization as presented in:
|
| 23 |
+
Baker et al. "A Database and Evaluation Methodology for Optical Flow" (ICCV, 2007)
|
| 24 |
+
URL: http://vision.middlebury.edu/flow/flowEval-iccv07.pdf
|
| 25 |
+
Code follows the original C++ source code of Daniel Scharstein.
|
| 26 |
+
Code follows the the Matlab source code of Deqing Sun.
|
| 27 |
+
Returns:
|
| 28 |
+
np.ndarray: Color wheel
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
RY = 15
|
| 32 |
+
YG = 6
|
| 33 |
+
GC = 4
|
| 34 |
+
CB = 11
|
| 35 |
+
BM = 13
|
| 36 |
+
MR = 6
|
| 37 |
+
|
| 38 |
+
ncols = RY + YG + GC + CB + BM + MR
|
| 39 |
+
colorwheel = np.zeros((ncols, 3))
|
| 40 |
+
col = 0
|
| 41 |
+
|
| 42 |
+
# RY
|
| 43 |
+
colorwheel[0:RY, 0] = 255
|
| 44 |
+
colorwheel[0:RY, 1] = np.floor(255*np.arange(0,RY)/RY)
|
| 45 |
+
col = col+RY
|
| 46 |
+
# YG
|
| 47 |
+
colorwheel[col:col+YG, 0] = 255 - np.floor(255*np.arange(0,YG)/YG)
|
| 48 |
+
colorwheel[col:col+YG, 1] = 255
|
| 49 |
+
col = col+YG
|
| 50 |
+
# GC
|
| 51 |
+
colorwheel[col:col+GC, 1] = 255
|
| 52 |
+
colorwheel[col:col+GC, 2] = np.floor(255*np.arange(0,GC)/GC)
|
| 53 |
+
col = col+GC
|
| 54 |
+
# CB
|
| 55 |
+
colorwheel[col:col+CB, 1] = 255 - np.floor(255*np.arange(CB)/CB)
|
| 56 |
+
colorwheel[col:col+CB, 2] = 255
|
| 57 |
+
col = col+CB
|
| 58 |
+
# BM
|
| 59 |
+
colorwheel[col:col+BM, 2] = 255
|
| 60 |
+
colorwheel[col:col+BM, 0] = np.floor(255*np.arange(0,BM)/BM)
|
| 61 |
+
col = col+BM
|
| 62 |
+
# MR
|
| 63 |
+
colorwheel[col:col+MR, 2] = 255 - np.floor(255*np.arange(MR)/MR)
|
| 64 |
+
colorwheel[col:col+MR, 0] = 255
|
| 65 |
+
return colorwheel
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def flow_uv_to_colors(u, v, convert_to_bgr=False):
|
| 69 |
+
"""
|
| 70 |
+
Applies the flow color wheel to (possibly clipped) flow components u and v.
|
| 71 |
+
According to the C++ source code of Daniel Scharstein
|
| 72 |
+
According to the Matlab source code of Deqing Sun
|
| 73 |
+
Args:
|
| 74 |
+
u (np.ndarray): Input horizontal flow of shape [H,W]
|
| 75 |
+
v (np.ndarray): Input vertical flow of shape [H,W]
|
| 76 |
+
convert_to_bgr (bool, optional): Convert output image to BGR. Defaults to False.
|
| 77 |
+
Returns:
|
| 78 |
+
np.ndarray: Flow visualization image of shape [H,W,3]
|
| 79 |
+
"""
|
| 80 |
+
flow_image = np.zeros((u.shape[0], u.shape[1], 3), np.uint8)
|
| 81 |
+
colorwheel = make_colorwheel() # shape [55x3]
|
| 82 |
+
ncols = colorwheel.shape[0]
|
| 83 |
+
rad = np.sqrt(np.square(u) + np.square(v))
|
| 84 |
+
a = np.arctan2(-v, -u)/np.pi
|
| 85 |
+
fk = (a+1) / 2*(ncols-1)
|
| 86 |
+
k0 = np.floor(fk).astype(np.int32)
|
| 87 |
+
k1 = k0 + 1
|
| 88 |
+
k1[k1 == ncols] = 0
|
| 89 |
+
f = fk - k0
|
| 90 |
+
for i in range(colorwheel.shape[1]):
|
| 91 |
+
tmp = colorwheel[:,i]
|
| 92 |
+
col0 = tmp[k0] / 255.0
|
| 93 |
+
col1 = tmp[k1] / 255.0
|
| 94 |
+
col = (1-f)*col0 + f*col1
|
| 95 |
+
idx = (rad <= 1)
|
| 96 |
+
col[idx] = 1 - rad[idx] * (1-col[idx])
|
| 97 |
+
col[~idx] = col[~idx] * 0.75 # out of range
|
| 98 |
+
# Note the 2-i => BGR instead of RGB
|
| 99 |
+
ch_idx = 2-i if convert_to_bgr else i
|
| 100 |
+
flow_image[:,:,ch_idx] = np.floor(255 * col)
|
| 101 |
+
return flow_image
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def flow_to_image(flow_uv, clip_flow=None, convert_to_bgr=False):
|
| 105 |
+
"""
|
| 106 |
+
Expects a two dimensional flow image of shape.
|
| 107 |
+
Args:
|
| 108 |
+
flow_uv (np.ndarray): Flow UV image of shape [H,W,2]
|
| 109 |
+
clip_flow (float, optional): Clip maximum of flow values. Defaults to None.
|
| 110 |
+
convert_to_bgr (bool, optional): Convert output image to BGR. Defaults to False.
|
| 111 |
+
Returns:
|
| 112 |
+
np.ndarray: Flow visualization image of shape [H,W,3]
|
| 113 |
+
"""
|
| 114 |
+
assert flow_uv.ndim == 3, 'input flow must have three dimensions'
|
| 115 |
+
assert flow_uv.shape[2] == 2, 'input flow must have shape [H,W,2]'
|
| 116 |
+
if clip_flow is not None:
|
| 117 |
+
flow_uv = np.clip(flow_uv, 0, clip_flow)
|
| 118 |
+
u = flow_uv[:,:,0]
|
| 119 |
+
v = flow_uv[:,:,1]
|
| 120 |
+
rad = np.sqrt(np.square(u) + np.square(v))
|
| 121 |
+
rad_max = np.max(rad)
|
| 122 |
+
epsilon = 1e-5
|
| 123 |
+
u = u / (rad_max + epsilon)
|
| 124 |
+
v = v / (rad_max + epsilon)
|
| 125 |
+
return flow_uv_to_colors(u, v, convert_to_bgr)
|