Lucas-Kanade光流算法、基于Kalman滤波的光流算法,基于Horn-Schunck光流算法精确度比较代码
时间: 2023-12-15 20:08:47 浏览: 163
各种光流算法的比较代码
3星 · 编辑精心推荐
以下是基于Lucas-Kanade、基于Kalman滤波的光流算法和基于Horn-Schunck光流算法的精度比较代码:
```python
import cv2
import numpy as np
# Read the video
cap = cv2.VideoCapture("test_video.mp4")
# Lucas-Kanade parameters
lk_params = dict(winSize=(15, 15),
maxLevel=4,
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# Kalman filter parameters
dt = 1. / 30
H = np.array([[1, 0, 0, dt, 0, 0],
[0, 1, 0, 0, dt, 0],
[0, 0, 1, 0, 0, dt],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 1]], dtype=np.float32)
Q = np.array([[0.001, 0, 0, 0, 0, 0],
[0, 0.001, 0, 0, 0, 0],
[0, 0, 0.001, 0, 0, 0],
[0, 0, 0, 0.001, 0, 0],
[0, 0, 0, 0, 0.001, 0],
[0, 0, 0, 0, 0, 0.001]], dtype=np.float32)
R = np.array([[5, 0, 0, 0, 0, 0],
[0, 5, 0, 0, 0, 0],
[0, 0, 5, 0, 0, 0],
[0, 0, 0, 5, 0, 0],
[0, 0, 0, 0, 5, 0],
[0, 0, 0, 0, 0, 5]], dtype=np.float32)
x = np.zeros((6, 1), dtype=np.float32)
P = np.zeros((6, 6), dtype=np.float32)
# Horn-Schunck parameters
alpha = 1
epsilon = 0.01
max_iter = 100
# Iterate through each frame of the video
while True:
ret, frame = cap.read()
if not ret:
break
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Lucas-Kanade optical flow
if 'prev_gray' in locals():
p1, st, err = cv2.calcOpticalFlowPyrLK(prev_gray, gray, p0, None, **lk_params)
good_new = p1[st == 1]
good_old = p0[st == 1]
dx = np.mean(good_new[:, 0] - good_old[:, 0])
dy = np.mean(good_new[:, 1] - good_old[:, 1])
p0 = good_new.reshape(-1, 1, 2)
# Draw the optical flow vectors
for i, (new, old) in enumerate(zip(good_new, good_old)):
a, b = new.ravel()
c, d = old.ravel()
frame = cv2.line(frame, (a, b), (c, d), (0, 255, 0), 2)
frame = cv2.circle(frame, (a, b), 5, (0, 0, 255), -1)
# Print the optical flow displacement
print("Lucas-Kanade displacement: ({}, {})".format(dx, dy))
else:
# Initialize the feature points
p0 = cv2.goodFeaturesToTrack(gray, mask=None, maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7)
# Draw the feature points
for i, pt in enumerate(p0):
x, y = pt.ravel()
frame = cv2.circle(frame, (x, y), 5, (0, 255, 0), -1)
# Kalman filter optical flow
if 'prev_gray' in locals():
z = np.array([[dx], [dy], [0], [0], [0], [0]], dtype=np.float32)
x = np.dot(H, x)
P = np.dot(np.dot(H, P), H.T) + Q
K = np.dot(np.dot(P, np.linalg.inv(P + R)), z - np.dot(H, x))
x = x + K
P = np.dot((np.eye(6) - np.dot(K, H)), P)
dx, dy = x[0], x[1]
# Print the optical flow displacement
print("Kalman filter displacement: ({}, {})".format(dx, dy))
else:
# Initialize the feature points
p0 = cv2.goodFeaturesToTrack(gray, mask=None, maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7)
# Draw the feature points
for i, pt in enumerate(p0):
x, y = pt.ravel()
frame = cv2.circle(frame, (x, y), 5, (0, 255, 0), -1)
x[0], x[1], x[2], x[3], x[4], x[5] = 0, 0, 0, 0, 0, 0
# Horn-Schunck optical flow
if 'prev_gray' in locals():
u = np.zeros_like(gray, dtype=np.float32)
v = np.zeros_like(gray, dtype=np.float32)
Ix = cv2.Sobel(prev_gray, cv2.CV_32F, 1, 0, ksize=3)
Iy = cv2.Sobel(prev_gray, cv2.CV_32F, 0, 1, ksize=3)
for i in range(max_iter):
u_avg = cv2.GaussianBlur(u, (5, 5), 0)
v_avg = cv2.GaussianBlur(v, (5, 5), 0)
u = u_avg + Ix * (Ix * u_avg + Iy * v_avg + gray - prev_gray) / (alpha ** 2 + Ix ** 2 + Iy ** 2 + epsilon)
v = v_avg + Iy * (Ix * u_avg + Iy * v_avg + gray - prev_gray) / (alpha ** 2 + Ix ** 2 + Iy ** 2 + epsilon)
dx = np.mean(u)
dy = np.mean(v)
# Draw the optical flow vectors
for y in range(0, gray.shape[0], 10):
for x in range(0, gray.shape[1], 10):
if np.abs(u[y, x]) > 0.1 or np.abs(v[y, x]) > 0.1:
frame = cv2.circle(frame, (x, y), 1, (0, 255, 0), -1)
frame = cv2.line(frame, (x, y), (int(x + u[y, x]), int(y + v[y, x])), (0, 0, 255), 1)
# Print the optical flow displacement
print("Horn-Schunck displacement: ({}, {})".format(dx, dy))
# Display the resulting frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Save the current frame as the previous frame
prev_gray = gray.copy()
# Release the video capture object and destroy all windows
cap.release()
cv2.destroyAllWindows()
```
该代码使用Lucas-Kanade、Kalman滤波和Horn-Schunck三种光流算法进行光流计算,并比较它们的精度。在每一帧图像中,它绘制了光流向量,并打印了光流位移。注意,在Kalman滤波中,我们使用一个6x1的状态向量来跟踪图像中的运动,其中前两个元素是光流位移的估计值。在Horn-Schunck中,我们使用高斯平滑和迭代来计算光流向量。
阅读全文