forked from amintronic/opticalFlowLK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptflow.cpp
94 lines (72 loc) · 2.16 KB
/
optflow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "optflow.h"
#include "ui_optflow.h"
OptFlow::OptFlow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::OptFlow)
{
needToInit = false;
plot_datas.resize(2);
termcrit = TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
cap = new capture_webcam(this);
main_timer = new QTimer(this);
connect(main_timer,SIGNAL(timeout()),this,SLOT(timerEvent()));
main_timer->start(2);
}
void OptFlow::timerEvent()
{
cap->frame.copyTo(colorframe);
cvtColor(colorframe, grayframe, CV_BGR2GRAY);
OptFlow_LK();
}
void OptFlow::OptFlow_LK()
{
if( needToInit )
{
goodFeaturesToTrack(grayframe, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
cornerSubPix(grayframe, points[1], subPixWinSize, Size(-1,-1), termcrit);
}
else if( !points[0].empty() )
{
vector<uchar> status;
vector<float> err;
if(prevGray.empty())
grayframe.copyTo(prevGray);
calcOpticalFlowPyrLK(prevGray, grayframe, points[0], points[1], status, err, winSize,
3, termcrit, 0, 0.001);
Features_counter =0;
for( size_t i = 0 ; i < points[1].size(); i++ )
{
if( status[i] )
{
points[1][Features_counter++] = points[1][i];
line(colorframe, points[0][i], points[1][i], Scalar(0,255,0), 1, 8, 0);
Point2f p1 = points[0][i], p2 = points[1][i];
delta_x += (p1.x - p2.x);
delta_y += (p1.y - p2.y);
}
}
delta_x /= Features_counter;
delta_y /= Features_counter;
points[1].resize(Features_counter);
qDebug() << (int)delta_x << " , "<<(int)delta_y;
}
#ifdef _GRAPH
plot_datas[0] = delta_x;
plot_datas[1] = delta_y;
plot1.Plot("X(t)-Y(t) Velocity", -100, 100, plot_datas);
#endif
if(points[1].size() < (unsigned int)THRESH_REFRESH)
needToInit = true;
else
needToInit = false;
#ifdef _GRAPH
imshow("LK Demo", colorframe);
#endif
waitKey(1);
std::swap(points[1], points[0]);
cv::swap(prevGray, grayframe);
}
OptFlow::~OptFlow()
{
// delete ui;
}