-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1_1.py
31 lines (25 loc) · 940 Bytes
/
q1_1.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
import cv2
import numpy as np
def optical_flow_GF(addr):
cap = cv2.VideoCapture(addr)
ret, frame1 = cap.read()
prvsImg = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
hsv_mask = np.zeros_like(frame1)
hsv_mask[:, :, 1] = 255
while True:
ret, frame2 = cap.read()
nextImg = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(prvsImg, nextImg, None, 0.5, 3, 15, 3, 5, 1.2, 0)
mag, ang = cv2.cartToPolar(flow[:, :, 0], flow[:, :, 1], angleInDegrees=True)
hsv_mask[:, :, 0] = ang / 2
hsv_mask[:, :, 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
bgr = cv2.cvtColor(hsv_mask, cv2.COLOR_HSV2BGR)
cv2.imshow('frame2', bgr)
k = cv2.waitKey(10) & 0xff
if k == 27:
break
prvsImg = nextImg
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
optical_flow_GF('cars.mp4')