-
Notifications
You must be signed in to change notification settings - Fork 0
/
optical_flow.py
65 lines (50 loc) · 2.54 KB
/
optical_flow.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
57
58
59
60
61
62
63
64
65
###########################
# #
# Optical Flow #
# (Horn–Schunck method) #
# (Lucas-Kanade method) #
# #
# Author: David #
# Created on May 14, 2023 #
# #
###########################
# reference
# https://github.com/lmiz100/Optical-flow-Horn-Schunck-method
import cv2
from argparse import ArgumentParser
from src.optical_flow_utils import *
from src.HornSchunckOpticalFlow import computeHS
from src.LucasKanadeOpticalFlow import ComputeLK
if __name__ == '__main__':
# translate_image()
parser = ArgumentParser(description = 'Horn Schunck program')
parser.add_argument('-method', type = str, help='Type hs (Horn-Schunck) or lk (Lucas-Kanade) method for optical-flow estimation', default="hs")
parser.add_argument('-img1', type = str, help = 'First image name (include format)', default="data/sphere1.bmp")
parser.add_argument('-img2', type = str, help='Second image name (include format)', default="data/sphere2.bmp")
# parser.add_argument('-img1', type = str, help = 'First image name (include format)', default="data/table1.jpg")
# parser.add_argument('-img2', type = str, help='Second image name (include format)', default="data/table2.jpg")
args = parser.parse_args()
beforeImg = load_image(args.img1, '1st Image', 'results/img1.png')
afterImg = load_image(args.img2, '2nd Image', 'results/img2.png')
# removing noise
kernel_blur_size = 5
beforeImg = cv2.GaussianBlur(beforeImg, (kernel_blur_size, kernel_blur_size), 0)
afterImg = cv2.GaussianBlur(afterImg, (kernel_blur_size, kernel_blur_size), 0)
if args.method == 'hs':
print('\n\tHorn–Schunck Optical Flow')
Lambdas = [0.1, 1, 10]
Iterations = [1, 4, 16, 64]
for lambda_i in Lambdas:
for iteration_number in Iterations:
u, v = computeHS(beforeImg, afterImg, alpha_square = 1.0/lambda_i, max_iterations = iteration_number, delta = 10**-1)
verbose = True
p1 = QuiverPlotter(u, v, beforeImg, verbose, 'HS', lambda_i, iteration_number)
elif args.method == 'lk':
print('\n\tLucas-Kanade Optical Flow')
window_size_set = [1,3,5]
beforeImg = np.uint8(beforeImg)
afterImg = np.uint8(afterImg)
for window_size in window_size_set:
u, v = ComputeLK( beforeImg, afterImg, window_size, min_quality=0.001)
verbose = True
p1 = QuiverPlotter(u, v, beforeImg, verbose, 'LK', window_size)