稠密光流跟踪"/>
Opencv稠密光流跟踪
Opencv稠密光流跟踪详细代码
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <math.h>
#include <stdio.h>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <chrono>
#include <fstream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>#include "opencv2/calib3d/calib3d.hpp"
#include <cctype>
#include <string.h>
#include <time.h>#include <windows.h>
#include <cstdio>
#include<windows.h>
#include"glew.h"
#include<GL/GL.h>
#include<GL/GLU.h>//#include <sl/Camera.hpp>using namespace std;
using namespace cv;Mat frame, gray;
Mat pre_frame, pre_gray;
//存储光流结果以及光流数据
Mat flowresult, flowdata;//绘画函数
void drawOpticalFlowHF(Mat &flowdata, Mat &flowresult,int step);
int main(int argc, char**argv)
{VideoCapture capture;capture.open("E:/图片/data/2.avi");if (!capture.isOpened()){printf("could not load video data.....\n");return -1;}//读取第一帧图片capture.read(frame);cvtColor(frame, pre_gray, COLOR_BGR2GRAY);namedWindow("input viedo", CV_WINDOW_AUTOSIZE);namedWindow("flow tracking", CV_WINDOW_AUTOSIZE);//从第二帧数据开始while (capture.read(frame)){cvtColor(frame, gray, COLOR_BGR2GRAY);if (!pre_gray.empty()){//计算光流数据calcOpticalFlowFarneback(pre_gray, gray, flowdata, 0.5, 3, 15, 3, 5, 1.2, 0);//将第一帧数据画到计算结果上面//灰度图转原图cvtColor(pre_gray, flowresult, COLOR_GRAY2BGR);drawOpticalFlowHF(flowdata, flowresult,1);imshow("input viedo", frame);imshow("flow tracking", flowresult);}char c = waitKey(1);if (c == 27){break;}}capture.release();waitKey(0);return 0;
}//绘画函数
void drawOpticalFlowHF(Mat &flowdata, Mat &flowresult, int step)
{for (int row = 0; row < flowresult.rows; row++){for (int col = 0; col < flowresult.cols; col++){//把产生位移的点读出来const Point2f fxy = flowdata.at<Point2f>(row, col);//如果移动两个像素之上,就认为是动的,否则就是静止的if (fxy.x > 2 || fxy.y > 2){//将动点画出来 cvRound是基准点的意思line(flowresult, Point(col, row), Point(cvRound(col + fxy.x), cvRound(row + fxy.y)),Scalar(156,0,0),2);//定义点的形状circle(flowresult, Point(col, row), 2, Scalar(0, 255, 0), -1);}}}
}
更多推荐
Opencv稠密光流跟踪
发布评论