This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DenseOpticalFlow.py
53 lines (33 loc) · 1.69 KB
/
DenseOpticalFlow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
##############################################################
########## Object Tracking using OpenCV and Python ##########
##############################################################
#############################
##### Optical Flow #####
#############################
##### Importing Libraries #####
import cv2
import numpy as np
##### Pre-Processing #####
cap = cv2.VideoCapture("Videos/chaplin.mp4") #Video Capture
ret, first_frame = cap.read() #Get the first frame
prev_gray = cv2.cvtColor(first_frame,cv2.COLOR_BGR2GRAY) #Conver to Grayscale
mask = np.zeros_like(first_frame) #Create a Mask
mask[..., 1] = 255 #Setting saturation to max
##### Optical Dense Flow #####
while(cap.isOpened()): #While Loop
ret, frame = cap.read() #Get Video
cv2.imshow('imput', frame) #Display the output
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #Conver to Grayscale
flow = cv2.calcOpticalFlowFarneback(prev_gray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0) #Farneback Optical FLow Calculation
magn, angle = cv2.cartToPolar(flow[..., 0], flow[..., 1]) #Calculate Mag and Angle
mask[..., 0] = angle*180/np.pi/2 #Set the Optical Flow Direction
mask[..., 2] = cv2.normalize(magn, None, 0, 255, cv2.NORM_MINMAX) #Normalise The Magnitude
rgb = cv2.cvtColor(mask, cv2.COLOR_HSV2RGB) #Convert the HSB to RGB
cv2.imshow("Dense Optical Flow", rgb)
##### UPDATION #####
prev_gray = gray
##### END #####
if cv2.waitKey(300) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()