Skip to content

Instantly share code, notes, and snippets.

@eeddaann
Created April 16, 2023 04:10
Show Gist options
  • Save eeddaann/f7b4de48cbfc407a63722685d2cd0349 to your computer and use it in GitHub Desktop.
Save eeddaann/f7b4de48cbfc407a63722685d2cd0349 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
import matplotlib.pyplot as plt
a = []
cap = cv2.VideoCapture('clip.mp4')
ok, frame = cap.read()
parameters_shitomasi = dict(maxCorners=100, qualityLevel=0.3, minDistance=7)
frame_gray_init = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Use Shi-Tomasi to detect object corners / edges from initial frame
edges = cv2.goodFeaturesToTrack(frame_gray_init, mask = None, **parameters_shitomasi)
# create a black canvas the size of the initial frame
canvas = np.zeros_like(frame)
# create random colours for visualization for all 100 max corners for RGB channels
colours = np.random.randint(0, 255, (100, 3))
parameter_lucas_kanade = dict(winSize=(15, 15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
def select_point(event, x, y, flags, params):
global point, selected_point, old_points
# record coordinates of mouse click
if event == cv2.EVENT_LBUTTONDOWN:
point = (x, y)
selected_point = True
old_points = np.array([[x, y]], dtype=np.float32)
# associate select function with window Selector
cv2.namedWindow('Optical Flow')
cv2.setMouseCallback('Optical Flow', select_point)
# initialize variables updated by function
selected_point = False
point = ()
old_points = ([[]])
while True:
# get next frame
ok, frame = cap.read()
if ok:
# covert to grayscale
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if selected_point is True:
cv2.circle(frame, point, 5, (0, 0, 255), 2)
# update object corners by comparing with found edges in initial frame
new_points, status, errors = cv2.calcOpticalFlowPyrLK(frame_gray_init, frame_gray, old_points, None,
**parameter_lucas_kanade)
# overwrite initial frame with current before restarting the loop
frame_gray_init = frame_gray.copy()
# update to new edges before restarting the loop
old_points = new_points
x, y = new_points.ravel()
j, k = old_points.ravel()
# draw line between old and new corner point with random colour
canvas = cv2.line(canvas, (int(x), int(y)), (int(j), int(k)), (0, 255, 0), 3)
# draw circle around new position
frame = cv2.circle(frame, (int(x), int(y)), 5, (0, 255, 0), -1)
a.append(y)
result = cv2.add(frame, canvas)
cv2.imshow('Optical Flow', result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
plt.plot(a)
plt.title("point location")
plt.xlabel("frame")
plt.ylabel("y-location")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment