OpenCV学习笔记(二十八)——光流法对运动目标跟踪Video

OpenCV配套的教程Tutorials对于Video的部分,没有实例进行说明,我只能摸石头过河啦,之前试过一个camShift做目标检测,这次试一试光流法做运动估计。这里使用的光流法是比较常用的 Lucas-Kanade方法。对于光流法的原理,我就不过多介绍了,主要讲使用OpenCV如何实现。

首先利用goodFeaturesToTrack函数得到图像中的强边界作为跟踪的特征点,接下来要调用calcOpticalFlowPyrLK函数,输入两幅连续的图像,并在第一幅图像里选择一组特征点,输出为这组点在下一幅图像中的位置。再把得到的跟踪结果过滤一下,去掉不好的特征点。再把特征点的跟踪路径标示出来。说着好简单哦~~

程序的效果和代码下载http://download.csdn.net/detail/yang_xian521/3811478



好的,下面是一个简单的C#窗体应用程序,使用OpenCvSharp实现光流法进行视频目标跟踪的示例: ```csharp using System; using System.Windows.Forms; using OpenCvSharp; using OpenCvSharp.Extensions; namespace OpticalFlowTracker { public partial class MainForm : Form { VideoCapture capture; Mat prevFrame, nextFrame; Point2f[] prevPoints, nextPoints; byte[] status; float[] errors; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { capture = new VideoCapture(0); // 打开摄像头 capture.FrameWidth = pictureBox1.Width; capture.FrameHeight = pictureBox1.Height; capture.Open(0); prevFrame = new Mat(); nextFrame = new Mat(); } private void timer1_Tick(object sender, EventArgs e) { if (capture.Read(prevFrame)) { Cv2.CvtColor(prevFrame, prevFrame, ColorConversionCodes.BGR2GRAY); if (nextFrame.Empty()) { prevFrame.CopyTo(nextFrame); } else { Cv2.CvtColor(nextFrame, nextFrame, ColorConversionCodes.BGR2GRAY); prevPoints = Cv2.GoodFeaturesToTrack(prevFrame, 500, 0.01, 10); nextPoints = new Point2f[prevPoints.Length]; status = new byte[prevPoints.Length]; errors = new float[prevPoints.Length]; Cv2.CalcOpticalFlowPyrLK(prevFrame, nextFrame, prevPoints, nextPoints, out status, out errors); for (int i = 0; i < prevPoints.Length; i++) { if (status[i] == 1) { Cv2.Circle(nextFrame, (Point)nextPoints[i], 5, Scalar.Red, 2); Cv2.Line(prevFrame, (Point)prevPoints[i], (Point)nextPoints[i], Scalar.Green, 2); } } pictureBox1.Image = BitmapConverter.ToBitmap(nextFrame); prevFrame.Dispose(); prevFrame = nextFrame.Clone(); } } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (capture != null) { capture.Release(); } prevFrame.Dispose(); nextFrame.Dispose(); } } } ``` 这个程序使用摄像头捕获视频流,并使用光流法进行目标跟踪。每次处理一个帧时,它会将前一帧和当前帧的灰度图像作为输入,并使用`Cv2.GoodFeaturesToTrack`函数在前一帧中检测出一些特征点,然后使用`Cv2.CalcOpticalFlowPyrLK`函数计算这些特征点在当前帧中的位置,并绘制出跟踪结果。最后,它将当前帧作为下一次处理的前一帧,并将结果显示在窗口中。
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值