您的位置:首页 > 运维架构

opencv实现简单光流跟踪

2018-01-12 21:32 507 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: