Created
April 6, 2018 18:19
-
-
Save mpeven/7df30dda0b780a0cfe7f664e5a4d8301 to your computer and use it in GitHub Desktop.
Put optical flow arrows on an image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
def put_optical_flow_arrows_on_image(image, optical_flow_image, threshold=2.0, skip_amount=30): | |
# Don't affect original image | |
image = image.copy() | |
# Turn grayscale to rgb if needed | |
if len(image.shape) == 2: | |
image = np.stack((image,)*3, axis=2) | |
# Get start and end coordinates of the optical flow | |
flow_start = np.stack(np.meshgrid(range(optical_flow_image.shape[1]), range(optical_flow_image.shape[0])), 2) | |
flow_end = (optical_flow_image[flow_start[:,:,1],flow_start[:,:,0],:1]*3 + flow_start).astype(np.int32) | |
# Threshold values | |
norm = np.linalg.norm(flow_end - flow_start, axis=2) | |
norm[norm < threshold] = 0 | |
# Draw all the nonzero values | |
nz = np.nonzero(norm) | |
for i in range(0, len(nz[0]), skip_amount): | |
y, x = nz[0][i], nz[1][i] | |
cv2.arrowedLine(image, | |
pt1=tuple(flow_start[y,x]), | |
pt2=tuple(flow_end[y,x]), | |
color=(0, 255, 0), | |
thickness=1, | |
tipLength=.2) | |
return image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 15,
flow_end = (optical_flow_image[flow_start[:,:,1],flow_start[:,:,0],:1]*3 + flow_start).astype(np.int32)
, why there is a :1 here? And why *3?