【OpenCV】 中使用 Lucas-Kanade 光流进行对象跟踪和路径映射

2024-08-23 04:52

本文主要是介绍【OpenCV】 中使用 Lucas-Kanade 光流进行对象跟踪和路径映射,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、说明
  • 二、什么是Lucas-Kanade 方法
  • 三、Lucas-Kanade 原理
  • 四、代码实现
    • 4.1 第 1 步:用户在第一帧绘制一个矩形
    • 4.2 第 2 步:从图像中提取关键点
    • 4.3 第 3 步:跟踪每一帧的关键点

一、说明

本文针对基于光流法的目标追踪进行叙述,首先介绍Lucas-Kanade 方法的引进,以及基本推导,然后演示如何实现光流法的运动跟踪。并以OpenCV实现一个基本项目。

二、什么是Lucas-Kanade 方法

在计算机视觉领域,Lucas-Kanade 方法是 Bruce D. Lucas 和Takeo Kanade开发的一种广泛使用的光流估计差分方法。该方法假设所考虑像素局部邻域中的光流基本恒定,并根据最小二乘准则求解该邻域中所有像素的基本光流方程。

通过结合来自多个邻近像素的信息,Lucas-Kanade 方法通常可以解决光流方程固有的模糊性。与逐点方法相比,该方法对图像噪声的敏感度也较低。另一方面,由于它是一种纯局部方法,因此无法提供图像均匀区域内部的流信息。

三、Lucas-Kanade 原理

在理论上,初始时间为 t 0 t_0 t0 时刻,经历过 Δ t \Delta t Δt时段后,点p会移动到另一个位置 p ′ p′ p ,并且 p ′ p′ p 本身和周围都有着与p相似的亮度值。朴素的LK光流法是直接用灰度值代替RGB作为亮度。根据上面的描述,对于点p而言,假设p 的坐标值是( x , y ),有
I ( x , y , t ) = I ( x + Δ x , y + Δ y , t + Δ t ) I(x, y, t) = I(x+\Delta x,y+\Delta y, t+\Delta t) I(x,y,t)=I(x+Δx,y+Δy,t+Δt)

根据泰勒公式:在这里把x 、y 看做是t 的函数,把公式(1)看做单变量t 的等式,只需对t进行展开)
I ( x , y , t ) = I ( x , y , t ) + ∂ I ∂ x ∂ x ∂ t + ∂ I ∂ y ∂ y ∂ t + ∂ I ∂ t + o ( Δ t ) I(x,y,t)=I(x,y,t)+\frac{∂I} {∂x}\frac{∂x}{∂t}+\frac{∂I} {∂y}\frac{∂y}{∂t}+\frac{∂I} {∂t}+o(Δt) I(x,y,t)=I(x,y,t)+xItx+yIty+tI+o(Δt)
对于一个像素区域:
I x ( q 1 ) V x + I y ( q 1 ) V x = − I t ( q 1 ) I x ( q 2 ) V x + I y ( q 2 ) V x = − I t ( q 2 ) . . . I x ( q n ) V x + I y ( q n ) V x = − I t ( q n ) I_x(q_1)V_x+I_y(q_1)V_x=-I_t(q_1)\\I_x(q_2)V_x+I_y(q_2)V_x=-I_t(q_2)\\...\\I_x(q_n)V_x+I_y(q_n)V_x=-I_t(q_n) Ix(q1)Vx+Iy(q1)Vx=It(q1)Ix(q2)Vx+Iy(q2)Vx=It(q2)...Ix(qn)Vx+Iy(qn)Vx=It(qn)

在这里: q 1 , q 2 , . . . q n q_1,q_2,...q_n q1,q2,...qn是窗口内点的标号, I x ( q i ) I_x(q_i) Ix(qi), I y ( q i ) I_y(q_i) Iy(qi), I t ( q i ) I_t(q_i) It(qi)是图像的灰度偏导数,
这些方程可以写成矩阵形式:
A v = b Av=b Av=b
在这里插入图片描述
这个系统的方程多于未知数,因此它通常是过度确定的。Lucas-Kanade方法通过最小二乘原理得到折衷解。也就是说,它解决了2×2系统:
在这里插入图片描述

在这里插入图片描述
因此
在这里插入图片描述

四、代码实现

4.1 第 1 步:用户在第一帧绘制一个矩形

# Path to video  
video_path="videos/bicycle1.mp4" 
video = cv2.VideoCapture(video_path)# read only the first frame for drawing a rectangle for the desired object
ret,frame = video.read()# I am giving  big random numbers for x_min and y_min because if you initialize them as zeros whatever coordinate you go minimum will be zero 
x_min,y_min,x_max,y_max=36000,36000,0,0def coordinat_chooser(event,x,y,flags,param):global go , x_min , y_min, x_max , y_max# when you click the right button, it will provide coordinates for variablesif event==cv2.EVENT_RBUTTONDOWN:# if current coordinate of x lower than the x_min it will be new x_min , same rules apply for y_min x_min=min(x,x_min) y_min=min(y,y_min)# if current coordinate of x higher than the x_max it will be new x_max , same rules apply for y_maxx_max=max(x,x_max)y_max=max(y,y_max)# draw rectanglecv2.rectangle(frame,(x_min,y_min),(x_max,y_max),(0,255,0),1)"""if you didn't like your rectangle (maybe if you made some misclicks),  reset the coordinates with the middle button of your mouseif you press the middle button of your mouse coordinates will reset and you can give a new 2-point pair for your rectangle"""if event==cv2.EVENT_MBUTTONDOWN:print("reset coordinate  data")x_min,y_min,x_max,y_max=36000,36000,0,0cv2.namedWindow('coordinate_screen')
# Set mouse handler for the specified window, in this case, "coordinate_screen" window
cv2.setMouseCallback('coordinate_screen',coordinat_chooser)while True:cv2.imshow("coordinate_screen",frame) # show only first frame k = cv2.waitKey(5) & 0xFF # after drawing rectangle press ESC   if k == 27:cv2.destroyAllWindows()breakcv2.destroyAllWindows()

4.2 第 2 步:从图像中提取关键点

# take region of interest ( take inside of rectangle )
roi_image=frame[y_min:y_max,x_min:x_max]# convert roi to grayscale
roi_gray=cv2.cvtColor(roi_image,cv2.COLOR_BGR2GRAY) # Params for corner detection
feature_params = dict(maxCorners=20,  # We want only one featurequalityLevel=0.2,  # Quality threshold minDistance=7,  # Max distance between corners, not important in this case because we only use 1 cornerblockSize=7)first_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)# Harris Corner detection
points = cv2.goodFeaturesToTrack(first_gray, mask=None, **feature_params)# Filter the detected points to find one within the bounding box
for point in points:x, y = point.ravel()if y_min <= y <= y_max and x_min <= x <= x_max:selected_point = pointbreak# If a point is found, convert it to the correct shape
if selected_point is not None:p0 = np.array([selected_point], dtype=np.float32)plt.imshow(roi_gray,cmap="gray")

将从此图像中提取关键点

4.3 第 3 步:跟踪每一帧的关键点

############################ Parameters ####################################""" 
winSize --> size of the search window at each pyramid level
Smaller windows can more precisely track small, detailed features -->   slow or subtle movements and where fine detail tracking is crucial.
Larger windows is better for larger displacements between frames ,  more robust to noise and small variations in pixel intensity --> require more computations
"""# Parameters for Lucas-Kanade optical flow
lk_params = dict(winSize=(7, 7),  # Window sizemaxLevel=2,  # Number of pyramid levelscriteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))############################ Algorithm ##################################### Read video
cap = cv2.VideoCapture(video_path)# Take first frame and find corners in it
ret, old_frame = cap.read()width = old_frame.shape[1]
height = old_frame.shape[0]# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)frame_count = 0
start_time = time.time()old_gray = first_graywhile True:ret, frame = cap.read()if not ret:breakframe_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)if p0 is not None:# Calculate optical flowp1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)  good_new = p1[st == 1]  # st==1 means found pointgood_old = p0[st == 1]if len(good_new) > 0:# Calculate movementa, b = good_new[0].ravel()c, d = good_old[0].ravel()# Draw the tracksmask = cv2.line(mask, (int(a), int(b)), (int(c), int(d)), (0, 255, 0), 2)frame = cv2.circle(frame, (int(a), int(b)), 5, (0, 255, 0), -1)img = cv2.add(frame, mask)# Calculate and display FPSelapsed_time = time.time() - start_timefps = frame_count / elapsed_time if elapsed_time > 0 else 0cv2.putText(img, f"FPS: {fps:.2f}", (width - 200, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)cv2.imshow('frame', img)# Update previous frame and pointsold_gray = frame_gray.copy()p0 = good_new.reshape(-1, 1, 2)else:p0 = None# Check if the tracked point is out of frameif not (25 <= a < width):p0 = None  # Reset p0 to None to detect new feature in the next iterationselected_point_distance = 0  # Reset selected point distance when new point is detected# Redetect features if necessaryif p0 is None:p0 = cv2.goodFeaturesToTrack(frame_gray, mask=None, **feature_params)mask = np.zeros_like(frame)selected_point_distance=0frame_count += 1k = cv2.waitKey(25)if k == 27:breakcv2.destroyAllWindows()
cap.release()

结果

这篇关于【OpenCV】 中使用 Lucas-Kanade 光流进行对象跟踪和路径映射的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/1098405

相关文章

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或

Python使用pynput模拟实现键盘自动输入工具

《Python使用pynput模拟实现键盘自动输入工具》在日常办公和软件开发中,我们经常需要处理大量重复的文本输入工作,所以本文就来和大家介绍一款使用Python的PyQt5库结合pynput键盘控制... 目录概述:当自动化遇上可视化功能全景图核心功能矩阵技术栈深度效果展示使用教程四步操作指南核心代码解析

使用Python获取JS加载的数据的多种实现方法

《使用Python获取JS加载的数据的多种实现方法》在当今的互联网时代,网页数据的动态加载已经成为一种常见的技术手段,许多现代网站通过JavaScript(JS)动态加载内容,这使得传统的静态网页爬取... 目录引言一、动态 网页与js加载数据的原理二、python爬取JS加载数据的方法(一)分析网络请求1

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur

Mac备忘录怎么导出/备份和云同步? Mac备忘录使用技巧

《Mac备忘录怎么导出/备份和云同步?Mac备忘录使用技巧》备忘录作为iOS里简单而又不可或缺的一个系统应用,上手容易,可以满足我们日常生活中各种记录的需求,今天我们就来看看Mac备忘录的导出、... 「备忘录」是 MAC 上的一款常用应用,它可以帮助我们捕捉灵感、记录待办事项或保存重要信息。为了便于在不同

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

qtcreater配置opencv遇到的坑及实践记录

《qtcreater配置opencv遇到的坑及实践记录》我配置opencv不管是按照网上的教程还是deepseek发现都有些问题,下面是我的配置方法以及实践成功的心得,感兴趣的朋友跟随小编一起看看吧... 目录电脑环境下载环境变量配置qmake加入外部库测试配置我配置opencv不管是按照网上的教程还是de

如何Python使用设置word的页边距

《如何Python使用设置word的页边距》在编写或处理Word文档的过程中,页边距是一个不可忽视的排版要素,本文将介绍如何使用Python设置Word文档中各个节的页边距,需要的可以参考下... 目录操作步骤代码示例页边距单位说明应用场景与高级用China编程途小结在编写或处理Word文档的过程中,页边距是一个

SpringBoot项目Web拦截器使用的多种方式

《SpringBoot项目Web拦截器使用的多种方式》在SpringBoot应用中,Web拦截器(Interceptor)是一种用于在请求处理的不同阶段执行自定义逻辑的机制,下面给大家介绍Sprin... 目录一、实现 HandlerInterceptor 接口1、创建HandlerInterceptor实

使用JavaConfig配置Spring的流程步骤

《使用JavaConfig配置Spring的流程步骤》JavaConfig是Spring框架提供的一种基于Java的配置方式,它通过使用@Configuration注解标记的类来替代传统的XML配置文... 目录一、什么是 JavaConfig?1. 核心注解2. 与 XML 配置的对比二、JavaConf