-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhorn_schunck.py
52 lines (42 loc) · 1.38 KB
/
horn_schunck.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
import hindemith as hm
from hindemith.types import hmarray
from hindemith.core import compose
from hindemith.operations.array import Square
from hindemith.operations.convolve import Convolve2D
import numpy as np
import cv2
alpha = 15.0
jacobi = np.array([
[1.0/12.0, 1.0/6.0, 1.0/12.0],
[1.0/6.0, 0.0, 1.0/6.0],
[1.0/12.0, 1.0/6.0, 1.0/12.0]
])
dx = np.array([[-1.0/12.0, -2.0/3.0, 0.0, 2.0/3.0, 1.0/12.0]])
dy = np.array([[-1.0/12.0], [-2.0/3.0], [0.0], [2.0/3.0], [1.0/12.0]])
alpha2 = alpha ** 2
epsilon = .01
@compose
def hs_jacobi(im0, im1, u, v):
It = im1 - im0
Iy = Convolve2D(im1, dy)
Ix = Convolve2D(im1, dx)
denom = Square(Ix) + Square(Iy) + alpha2
for _ in range(100):
ubar = Convolve2D(u, jacobi)
vbar = Convolve2D(v, jacobi)
t = (Ix * ubar + Iy * vbar + It) / denom
u_new = ubar - Ix * t
v_new = vbar - Iy * t
u, v = u_new, v_new
return u, v
frame0 = cv2.imread('images/frame0.png')
frame1 = cv2.imread('images/frame1.png')
# cap = cv2.VideoCapture('ir.mp4')
# ret, frame0 = cap.read()
hsv = np.zeros_like(frame0)
im0 = cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY).astype(np.float32).view(hmarray)
hm_u = hm.zeros_like(im0)
hm_v = hm.zeros_like(im0)
im1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY).astype(np.float32).view(hmarray)
hm_u, hm_v = hs_jacobi(im0, im1, hm_u, hm_v)
hs_jacobi.composed.dump_kernels()