# -*- coding: utf-8 -*- # @Time : 2022/7/21 # @Author : Rui Zhao # @File : optical_flow_visualization.py import torch import numpy as np import cv2 import math #################### Interface #################### def flow_visualization(flow, mode='normal', use_cv2=True): if mode == 'normal': flow_vis = flow_to_image(flow_uv=flow, convert_to_bgr=use_cv2) elif mode == 'scflow': flow_vis = flow_to_img_scflow(flow_uv=flow) if not use_cv2: flow_vis = cv2.cvtColor(flow_vis, cv2.COLOR_BGR2RGB) elif mode == 'evflow': flow_vis = flow_viz_np(flow_x=flow[:,:,0], flow_y=flow[:,:,1]) return flow_vis def vis_color_map(use_cv2=True): u = np.linspace(-100, 99, 200) v = np.linspace(-100, 99, 200) xx, yy = np.meshgrid(u, v) flow = np.concatenate((xx[:,:,None], yy[:,:,None]), axis=2) map_normal = flow_visualization(flow=flow, mode='normal', use_cv2=use_cv2) map_scflow = flow_visualization(flow=flow, mode='scflow', use_cv2=use_cv2) map_evflow = flow_visualization(flow=flow, mode='evflow', use_cv2=use_cv2) return [map_normal, map_scflow, map_evflow] def make_colorwheel(): """ Generates a color wheel for optical flow visualization as presented in: Baker et al. "A Database and Evaluation Methodology for Optical Flow" (ICCV, 2007) URL: http://vision.middlebury.edu/flow/flowEval-iccv07.pdf Code follows the original C++ source code of Daniel Scharstein. Code follows the the Matlab source code of Deqing Sun. Returns: np.ndarray: Color wheel """ RY = 15 YG = 6 GC = 4 CB = 11 BM = 13 MR = 6 ncols = RY + YG + GC + CB + BM + MR colorwheel = np.zeros((ncols, 3)) col = 0 # RY colorwheel[0:RY, 0] = 255 colorwheel[0:RY, 1] = np.floor(255*np.arange(0,RY)/RY) col = col+RY # YG colorwheel[col:col+YG, 0] = 255 - np.floor(255*np.arange(0,YG)/YG) colorwheel[col:col+YG, 1] = 255 col = col+YG # GC colorwheel[col:col+GC, 1] = 255 colorwheel[col:col+GC, 2] = np.floor(255*np.arange(0,GC)/GC) col = col+GC # CB colorwheel[col:col+CB, 1] = 255 - np.floor(255*np.arange(CB)/CB) colorwheel[col:col+CB, 2] = 255 col = col+CB # BM colorwheel[col:col+BM, 2] = 255 colorwheel[col:col+BM, 0] = np.floor(255*np.arange(0,BM)/BM) col = col+BM # MR colorwheel[col:col+MR, 2] = 255 - np.floor(255*np.arange(MR)/MR) colorwheel[col:col+MR, 0] = 255 return colorwheel #################### Normal Version #################### """ From https://github.com/princeton-vl/RAFT/blob/master/core/utils/flow_viz.py """ def flow_uv_to_colors(u, v, convert_to_bgr=False): """ Applies the flow color wheel to (possibly clipped) flow components u and v. According to the C++ source code of Daniel Scharstein According to the Matlab source code of Deqing Sun Args: u (np.ndarray): Input horizontal flow of shape [H,W] v (np.ndarray): Input vertical flow of shape [H,W] convert_to_bgr (bool, optional): Convert output image to BGR. Defaults to False. Returns: np.ndarray: Flow visualization image of shape [H,W,3] """ flow_image = np.zeros((u.shape[0], u.shape[1], 3), np.uint8) colorwheel = make_colorwheel() # shape [55x3] ncols = colorwheel.shape[0] rad = np.sqrt(np.square(u) + np.square(v)) a = np.arctan2(-v, -u)/np.pi fk = (a+1) / 2*(ncols-1) k0 = np.floor(fk).astype(np.int32) k1 = k0 + 1 k1[k1 == ncols] = 0 f = fk - k0 for i in range(colorwheel.shape[1]): tmp = colorwheel[:,i] col0 = tmp[k0] / 255.0 col1 = tmp[k1] / 255.0 col = (1-f)*col0 + f*col1 idx = (rad <= 1) col[idx] = 1 - rad[idx] * (1-col[idx]) col[~idx] = col[~idx] * 0.75 # out of range # Note the 2-i => BGR instead of RGB ch_idx = 2-i if convert_to_bgr else i flow_image[:,:,ch_idx] = np.floor(255 * col) return flow_image def flow_to_image(flow_uv, clip_flow=None, convert_to_bgr=False): """ Expects a two dimensional flow image of shape. Args: flow_uv (np.ndarray): Flow UV image of shape [H,W,2] clip_flow (float, optional): Clip maximum of flow values. Defaults to None. convert_to_bgr (bool, optional): Convert output image to BGR. Defaults to False. Returns: np.ndarray: Flow visualization image of shape [H,W,3] """ assert flow_uv.ndim == 3, 'input flow must have three dimensions' assert flow_uv.shape[2] == 2, 'input flow must have shape [H,W,2]' if clip_flow is not None: flow_uv = np.clip(flow_uv, 0, clip_flow) u = flow_uv[:,:,0] v = flow_uv[:,:,1] rad = np.sqrt(np.square(u) + np.square(v)) rad_max = np.max(rad) epsilon = 1e-5 u = u / (rad_max + epsilon) v = v / (rad_max + epsilon) return flow_uv_to_colors(u, v, convert_to_bgr) #################### SCFlow Version #################### def flow_uv_to_colors_scflow(u, v, convert_to_bgr=False): """ Applies the flow color wheel to (possibly clipped) flow components u and v. According to the C++ source code of Daniel Scharstein According to the Matlab source code of Deqing Sun Args: u (np.ndarray): Input horizontal flow of shape [H,W] v (np.ndarray): Input vertical flow of shape [H,W] convert_to_bgr (bool, optional): Convert output image to BGR. Defaults to False. Returns: np.ndarray: Flow visualization image of shape [H,W,3] """ flow_image = np.zeros((u.shape[0], u.shape[1], 3), np.uint8) colorwheel = make_colorwheel() # shape [55x3] ncols = colorwheel.shape[0] rad = np.sqrt(np.square(u) + np.square(v)) a = np.arctan2(-v, u)/np.pi fk = (a+1) / 2*(ncols-1) k0 = np.floor(fk).astype(np.int32) k1 = k0 + 1 k1[k1 == ncols] = 0 f = fk - k0 for i in range(colorwheel.shape[1]): tmp = colorwheel[:,i] col0 = tmp[k0] / 255.0 col1 = tmp[k1] / 255.0 col = (1-f)*col0 + f*col1 idx = (rad <= 1) col[idx] = 1 - rad[idx] * (1-col[idx]) col[~idx] = col[~idx] * 0.75 # out of range # Note the 2-i => BGR instead of RGB ch_idx = 2-i if convert_to_bgr else i flow_image[:,:,ch_idx] = np.floor(255 * col) return flow_image def flow_to_img_scflow(flow_uv, clip_flow=None): """ Expects a two dimensional flow image of shape. Args: flow_uv (np.ndarray): Flow UV image of shape [H,W,2] clip_flow (float, optional): Clip maximum of flow values. Defaults to None. Returns: np.ndarray: Flow visualization image of shape [H,W,3] """ convert_to_bgr = False assert flow_uv.ndim == 3, 'input flow must have three dimensions' assert flow_uv.shape[2] == 2, 'input flow must have shape [H,W,2]' if clip_flow is not None: flow_uv = np.clip(flow_uv, 0, clip_flow) u = flow_uv[:,:,0] v = flow_uv[:,:,1] rad = np.sqrt(np.square(u) + np.square(v)) rad_max = np.max(rad) epsilon = 1e-5 u = u / (rad_max + epsilon) v = v / (rad_max + epsilon) return flow_uv_to_colors_scflow(u, v, convert_to_bgr) #################### EVFlow Version #################### """ From https://github.com/chan8972/Spike-FlowNet/blob/master/vis_utils.py """ """ Generates an RGB image where each point corresponds to flow in that direction from the center, as visualized by flow_viz_tf. Output: color_wheel_rgb: [1, width, height, 3] """ def draw_color_wheel_np(width, height): color_wheel_x = np.linspace(-width / 2.,width / 2.,width) color_wheel_y = np.linspace(-height / 2.,height / 2.,height) color_wheel_X, color_wheel_Y = np.meshgrid(color_wheel_x, color_wheel_y) color_wheel_rgb = flow_viz_np(color_wheel_X, color_wheel_Y) return color_wheel_rgb """ Visualizes optical flow in HSV space using TensorFlow, with orientation as H, magnitude as V. Returned as RGB. Input: flow: [batch_size, width, height, 2] Output: flow_rgb: [batch_size, width, height, 3] """ def flow_viz_np(flow_x, flow_y): import cv2 flows = np.stack((flow_x, flow_y), axis=2) mag = np.linalg.norm(flows, axis=2) ang = np.arctan2(flow_y, flow_x) ang += np.pi ang *= 180. / np.pi / 2. ang = ang.astype(np.uint8) hsv = np.zeros([flow_x.shape[0], flow_x.shape[1], 3], dtype=np.uint8) hsv[:, :, 0] = ang hsv[:, :, 1] = 255 hsv[:, :, 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) flow_rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) return flow_rgb #################### Visualization tools when training SCFlow #################### def outflow_img(flow_list, vis_path, name_prefix='flow', max_batch=4): flow = flow_list[0] batch_size, c, h, w = flow.shape for batch in range(batch_size): if batch > max_batch: break flow_current = flow[batch,:,:,:].permute(1,2,0).detach().cpu().numpy() flow_img = flow_visualization(flow_current, mode='scflow', use_cv2=True) cv2.imwrite(vis_path + '/{:s}_batch_id={:02d}.png'.format(name_prefix, batch), flow_img) return