Opencv稠密光流跟踪

编程入门 行业动态 更新时间:2024-10-06 10:39:04

Opencv<a href=https://www.elefans.com/category/jswz/34/1762952.html style=稠密光流跟踪"/>

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稠密光流跟踪

本文发布于:2024-02-14 08:16:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1762681.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:稠密   Opencv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!