opencv实现简单光流跟踪

#include "highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/tracking.hpp"  
#include <iostream>  

using namespace cv;
using namespace std;

Mat image1, image2;
vector<Point2f> point1, point2, pointCopy;
vector<uchar> status;
vector<float> err;

int main(void)
{
	VideoCapture cap;
	Mat frame, frame2;
	cap.open(0);
	cap >> frame;
	Mat image1Gray, image2Gray;
	cvtColor(frame, image1Gray, CV_RGB2GRAY);
	goodFeaturesToTrack(image1Gray, point1, 100, 0.01, 10, Mat());
	pointCopy = point1;
	for (int i = 0; i<point1.size(); i++)    //绘制特征点位  
	{
		circle(frame, point1[i], 1, Scalar(0, 0, 255), 2);
	}
	namedWindow("dd", 0);
	imshow("dd", frame);
	while (true)
	{
		cap >> frame2;
		cvtColor(frame2, image2Gray, CV_RGB2GRAY);
		calcOpticalFlowPyrLK(image1Gray, image2Gray, point1, point2, status, err, Size(20, 20), 3); //LK金字塔       
		for (int i = 0; i<point2.size(); i++)
		{
			circle(frame2, point2[i], 1, Scalar(0, 0, 255), 2);
			line(frame2, pointCopy[i], point2[i], Scalar(255, 0, 0), 2);
		}
		imshow("xx", frame2);
		waitKey(10);
		swap(point1, point2);
		image1Gray = image2Gray.clone();
	}
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值