异常检测中常用的光流法对比(Horn-Schunck / Lucas–Kanade / Farneback)

三种光流法的OpenCV-API

(一) Horn-Schunck光流法

CalcOpticalFlowHS

Horn–Schunck光流算法用一种全局方法估计图像的稠密光流场(即对图像中的每个像素计算光流)

算法原理参考论文:Determining Optical Flow

(二) Lucas-Kanade光流法

calcOpticalFlowPyrLK

Lucas-Kanada最初于1981年提出,该算法假设在一个小的空间邻域内运动矢量保持恒定,使用加权最小二乘法估计光流。由于该算法应用于输入图像的一组点上时比较方便,因此被广泛应用于稀疏光流场。

算法原理参考论文:Pyramidal Implementation of the Lucas Kanade Feature TrackerDescription of the algorithm

(三) Farneback光流法

calcOpticalFlowFarneback

Farneback是一种基于梯度的方法,假设图像梯度恒定且假设局部光流恒定,计算出图像上所有像素点的光流。

算法原理参考论文:Polynomial Expansion for Orientation and Motion Estimation

三种光流法的实验效果对比

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于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,我们使用高斯平滑和迭代来计算光流向量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值