-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracking_Farenback.py
56 lines (43 loc) · 2.83 KB
/
tracking_Farenback.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
54
55
56
# computing dense optical flow using the Farneback method.
# Dense optical flow computes the motion vector for every pixel in the image.
# Here's an overview of how the Farneback method works:
# Image Pyramids:
# The algorithm begins by constructing Gaussian image pyramids for the input frames. Image pyramids are multi-scale representations of images, where each level of the pyramid represents the image at a different scale.
# By building pyramids, the algorithm can capture both large-scale and small-scale motion information efficiently.
# Dense Optical Flow Estimation:
# The Farneback method aims to estimate dense optical flow, meaning it computes flow vectors for every pixel in the image.
# It computes the optical flow field by performing polynomial expansion and local matching between corresponding regions in the input frames.
# Polynomial Expansion:
# In this step, the algorithm fits a polynomial function to small image patches around each pixel.
# By fitting polynomials, the algorithm models the local motion between corresponding patches in the two frames.
# Local Matching:
# The algorithm uses the polynomial coefficients obtained from the polynomial expansion to estimate the local motion between corresponding image patches.
# It computes the motion vectors by comparing the polynomial coefficients between patches in the two frames.
# Gaussian Smoothing:
# To improve the robustness of the flow estimation, the algorithm applies Gaussian smoothing to the gradients of the input frames.
# Smoothing helps to reduce noise and errors in the gradient computation, resulting in more accurate flow estimation.
# Optical Flow Computation:
# Finally, the algorithm computes the optical flow vectors based on the local matching results and the polynomial coefficients.
# These flow vectors represent the motion displacement of each pixel between the two frames.
import cv2
import numpy as np
cap = cv2.VideoCapture(r'I:\downloadly\Udemy – Python for Computer Vision with OpenCV and Deep Learning 2021-3\Udemy - Python for Computer Vision with OpenCV and Deep Learning 2021-3\1 - Course Overview and Introduction\Computer-Vision-with-Python\DATA\video_capture.mp4')
ret , frame1 = cap.read()
prevImg = 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(prevImg,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(frame1,cv2.COLOR_HSV2BGR)
cv2.imshow('frame',bgr)
k = cv2.waitKey(1) & 0xFF
if k==27:
break
prevImg = nextImg
cv2.destroyAllWindows()
cap.release()