Skip to content
Snippets Groups Projects
Commit d9d93c70 authored by Lukas Riedersberger's avatar Lukas Riedersberger
Browse files

opencv full optical flow

parent 703f4117
No related branches found
No related tags found
No related merge requests found
Pipeline #
#pragma once
#include <iostream>
#include <utility>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
namespace OF {
enum Sparse {};
enum Full {};
}
template<typename T>
class OpticalFlow;
template<>
class OpticalFlow<OF::Sparse> {
private:
Mat image_t1;
Mat image_t2;
public:
OpticalFlow(const Mat &t1,const Mat &t2)
{
image_t1 = t1.clone();
image_t2 = t2.clone();
}
void calc()
{
}
};
template<>
class OpticalFlow<OF::Full> {
private:
Mat image_t1;
Mat image_t2;
Mat image_t1_GRAY;
Mat image_t2_GRAY;
Mat flow;
void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, const Scalar& color) {
for(int y = 0; y < cflowmap.rows; y += step)
for(int x = 0; x < cflowmap.cols; x += step)
{
const Point2f& fxy = flow.at< Point2f>(y, x);
line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),
color);
circle(cflowmap, Point(cvRound(x+fxy.x), cvRound(y+fxy.y)), 1, color, -1);
}
}
public:
OpticalFlow(const Mat &t1,const Mat &t2)
{
image_t1 = t1.clone();
image_t2 = t2.clone();
cvtColor(image_t1,image_t1_GRAY,COLOR_BGR2GRAY);
cvtColor(image_t2,image_t2_GRAY,COLOR_BGR2GRAY);
}
void calc()
{
Mat &t1=image_t1_GRAY;
Mat &t2=image_t2_GRAY;
//calculate dense flow
calcOpticalFlowFarneback(t1,t2, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
}
void draw()
{
Mat cflow = image_t1.clone();
//draw ever tenth row and col in green
drawOptFlowMap(flow, cflow, 10, CV_RGB(0, 255, 0));
imshow("OpticalFlowFarneback", cflow);
}
};
OFCV/images/example_car/car_t0.png

556 KiB

OFCV/images/example_car/car_t1.png

554 KiB

#include <iostream>
#include <chrono>
#include <thread>
#include <opencv2/opencv.hpp>
#include "opticalflow.hpp"
using namespace std;
int main(int argc, char **argv)
{
cout << "Hello World" << endl;
cv::Mat image;
image = cv::imread( argv[1], 1 );
cv::Mat image_t1, image_t2;
image_t1 = cv::imread( argv[1], 1 );
image_t2 = cv::imread( argv[2], 1 );
cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE );
cv::imshow("Display Image", image);
Mat &t0 = image_t1;
Mat &t1 = image_t2;
/*while(1)
{
OpticalFlow<OF::Full> test(image_t1,image_t2);
test.calc();
test.draw();
cv::waitKey(10);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::swap(t0,t1);
}*/
OpticalFlow<OF::Full> test(image_t1,image_t2);
test.calc();
test.draw();
cv::waitKey(0);
return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment