Repository navigation
Expand file tree
/
Copy pathLKT_Tracker.cpp
More file actions
139 lines (121 loc) · 3.33 KB
/
Copy pathLKT_Tracker.cpp
File metadata and controls
139 lines (121 loc) · 3.33 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "LKT_Tracker.h"
LKT_Tracker::LKT_Tracker(int featureCount)
{
this->featureCount = featureCount;
detectionCounter = 0;
contArea = 0;
locked = false;
}
void LKT_Tracker::updateTracker(Mat &img, Rect pos)
{
locked = true;
detectionCounter = 0;
// Convert image to grayscale
cvtColor(img, prevGray, COLOR_BGR2GRAY);
// Find good features to track
Mat mask = Mat::zeros(480,640, CV_8U);
mask(pos) = 1;
/*const TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
const Size subPixWinSize(5,5),winSize(31,31);*/
goodFeaturesToTrack(prevGray, points[0], featureCount, 0.01, 5, mask,
3, 0, 0.04);
cornerSubPix(prevGray, points[0], subPixWinSize, Size(-1,-1),
termcrit);
// Extract points from rectangle
bb[0] = Point(pos.x, pos.y);
bb[1] = Point(pos.x+pos.width, pos.y);
bb[3] = Point(pos.x, pos.y+pos.height);
bb[2] = Point(pos.x+pos.width, pos.y + pos.height);
vector<Point2f> bbb;
bbb.push_back(bb[0]);bbb.push_back(bb[1]);bbb.push_back(bb[2]);bbb.push_back(bb[3]);
contArea = contourArea(bbb);
cout << "Area is: " << contArea << endl;
rectangle(img, pos, cv::Scalar(0,0,255), 2);
}
void LKT_Tracker::track(Mat &img)
{
if(locked)
{
/*if(detectionCounter++ > MAX_DETECTION_COUNTER)
{
locked = false;
cout << "Unlocking of tracker" << endl;
}*/
// Convert image to grayscale
cvtColor(img, gray, COLOR_BGR2GRAY);
// In case prevGray is not locked
if(prevGray.empty())
gray.copyTo(prevGray);
vector<uchar> status;
vector<float> err;
/*const TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
const Size subPixWinSize(5,5),winSize(31,31);*/
calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status,
err, winSize,3, termcrit, 0, 0.001);
int i, k;
for(i = k = 0; i< points[1].size(); i++)
{
if(!status[i])
continue;
points[0][k] = points[0][i];
points[1][k++] = points[1][i];
circle(img, points[1][i], 3, Scalar(0,0,255), -1, 1);
}
points[0].resize(k);
points[1].resize(k);
if(k < 3)
{
locked = false;
}
vector<Point2f> bbb;
bbb.push_back(bb[0]);bbb.push_back(bb[1]);bbb.push_back(bb[2]);bbb.push_back(bb[3]);
//find transformation between features
try
{
if(points[0].size() > 2)
{
Mat transf = estimateRigidTransform(points[0], points[1], false);
transform(bbb, bbb, transf);
bb[0] = bbb.at(0);bb[1] = bbb.at(1);bb[2] = bbb.at(2);bb[3] = bbb.at(3);
line(img, bb[0], bb[1], Scalar(0,0,255), 1,8);
line(img, bb[1], bb[2], Scalar(0,0,255), 1,8);
line(img, bb[2], bb[3], Scalar(0,0,255), 1,8);
line(img, bb[3], bb[0], Scalar(0,0,255), 1,8);
}
}
catch( cv::Exception &e){};
contArea = contourArea(bbb);
cout << "Area is: " << contArea << endl;
std::swap(points[1], points[0]);
cv::swap(prevGray, gray);
}
}
Rect LKT_Tracker :: getRect()
{
//find min x, max x, min y and max y;
int minx = 1000; int miny = 1000; int maxx = 0; int maxy = 0;
for(int i = 0; i< 4; i++)
{
if(bb[i].x > maxx)
maxx = bb[i].x;
if(bb[i].x < minx)
minx = bb[i].x;
if(bb[i].y > maxy)
maxy = bb[i].y;
if(bb[i].y < miny)
miny = bb[i].y;
}
Rect rect(minx, miny, maxy - miny, maxx-minx);
return rect;
}
bool LKT_Tracker :: isLocked()
{
return locked;
}
Point* LKT_Tracker :: getPosition()
{
return bb;
}
LKT_Tracker:: ~LKT_Tracker()
{
}