-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathKLT.cpp
279 lines (245 loc) · 6.84 KB
/
KLT.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "KLT.h"
using namespace bgm;
const double KLT::qualityLevel(0.04);
const double KLT::minDistance(10.); //min dist gap to select features
const bool KLT::useHarrisDetector(false);
const double KLT::k(0.04);
const cv::Size KLT::winSize(21,21);
const int KLT::maxLevel(3);
const double KLT::minEigThreshold(5e-4);
KLT::KLT(void):enable_bgm(false),bgm(NULL),maxCorners(-1)
{
}
KLT::~KLT(void)
{
if(bgm!=NULL)
delete bgm;
}
/*Use the background model--init step*/
void KLT::UseBackgroundModel(const cv::Mat &first_frame)
{
assert(!this->enable_bgm && !this->bgm);
enable_bgm = true;//enable bgm
bgm = new BackgroundGMM(first_frame);
}
/*select good features from ROI and store to fl*/
void KLT::SelectGoodFeaturesToTrack(const cv::Mat &frame, KLT_FeatureList fl, const cv::Mat &ROI)
{
assert(ROI.total() == 0 || (ROI.cols == frame.cols && ROI.rows == frame.rows));
this->maxCorners = fl->nFeatures; // full fill size of list
std::vector<cv::Point2f> features; //Feature point location
//Determine the mask
cv::Mat mask;
if(enable_bgm)
{
mask.create(frame.rows,frame.cols,cv::DataType<uchar>::type);
for(int i = 0 ; i< frame.rows; ++i)
{
for(int j = 0; j < frame.cols; ++j)
{
mask.at<uchar>(i,j) = uchar(ROI.at<uchar>(i,j) & bgm->DetermineForeground(i,j));
}
}
}else
{
mask = ROI;
}
cv::Mat fr_gray;
cv::cvtColor(frame,fr_gray,CV_RGB2GRAY);//convert to gray image
cv::goodFeaturesToTrack(fr_gray,features,maxCorners,qualityLevel,minDistance,mask,3,useHarrisDetector,k);//has to be single channel image
//find sub-pixel accurancy
if(!features.empty())
cv::cornerSubPix(fr_gray,features,cv::Size(10,10),cv::Size(-1,-1),cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 30, 0.01));
//store to fl
assert(features.size()<=fl->nFeatures);
for(int i = 0; i<fl->nFeatures; ++i)
{
if(i<features.size())
{
fl->feature[i]->pos_x = features[i].x;
fl->feature[i]->pos_y = features[i].y;
fl->feature[i]->val = 1;
}else //fillin invalid values
{
fl->feature[i]->pos_x = -1;
fl->feature[i]->pos_y = -1;
fl->feature[i]->val = -1;
}
}
}
/*Track features stored in fl*/
void KLT::TrackFeatures(std::vector<cv::Mat> &pre_pyr, std::vector<cv::Mat> &nxt_pyr, KLT_FeatureList fl)
{
assert(!nxt_pyr.empty());
//check if previous frame & pyr have been stored
if(this->pre_pyr.empty())
{
assert(!pre_pyr.empty());
this->pre_pyr.assign(pre_pyr.cbegin(),pre_pyr.cend());
}
//Generate feature point vector
std::vector<cv::Point2f> Pts[2];
Pts[0].reserve(fl->nFeatures);
std::vector<int> index_fl;
index_fl.reserve(fl->nFeatures);
for(int i = 0; i<fl->nFeatures; ++i)
{
if(fl->feature[i]->val>=0)
{
Pts[0].push_back(cv::Point2f(fl->feature[i]->pos_x, fl->feature[i]->pos_y));
index_fl.push_back(i);
}
}
std::vector<uchar> status;
std::vector<float> err;
//Track features
if(Pts[0].size()>0)
{
cv::calcOpticalFlowPyrLK(this->pre_pyr,nxt_pyr,Pts[0],Pts[1],status,err
,KLT::winSize,KLT::maxLevel,
cv::TermCriteria(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 30, 0.01), 0, KLT::minEigThreshold);
}
//Output results to feature list
assert(Pts[1].size() == index_fl.size());
for(size_t i = 0; i<Pts[1].size(); ++i)
{
int fl_inx = index_fl[i];
if(status[i] == 1)
{
fl->feature[fl_inx]->val = 0;
fl->feature[fl_inx]->pos_x = Pts[1][i].x;
fl->feature[fl_inx]->pos_y = Pts[1][i].y;
}else
{
fl->feature[fl_inx]->val = -1; //set to unsuccessfully tracked features
fl->feature[fl_inx]->pos_x = -1;
fl->feature[fl_inx]->pos_y = -1;
}
}
//store for next tracking frame
this->pre_pyr.assign(nxt_pyr.cbegin(), nxt_pyr.cend());
}
/*Process the next image. Returns the pyramid image for feature tracking*/
void KLT::ProcessNxtImg(const cv::Mat &nxt_frame, std::vector<cv::Mat> &img_pry)
{
if(this->enable_bgm)
{
this->bgm->Processing(nxt_frame);
}
//retrieve pyramid image
cv::Mat fr_gray;
fr_gray.create(nxt_frame.rows,nxt_frame.cols,cv::DataType<uchar>::type);
cv::cvtColor(nxt_frame,fr_gray,CV_RGB2GRAY);
cv::buildOpticalFlowPyramid(fr_gray,img_pry,KLT::winSize,KLT::maxLevel);
}
/*Determine whether (i,j) is foreground*/
bool KLT::DetermineForeground(int i, int j)
{
return this->bgm->DetermineForeground(i,j);
}
/*Write Feature List to image*/
void KLT_FeatureListRec::WriteToImage(const std::string imgName, const cv::Mat &img) const
{
cv::Mat img_to_write;
img.copyTo(img_to_write);
const uchar color[3] = {255,0,0};
int dot_size = 1;
for(int i = 0; i<this->nFeatures ; ++i)
{
int x = int(feature[i]->pos_x+0.5f);
int y = int(feature[i]->pos_y+0.5f);
for(int xx = x-dot_size; xx <= x+dot_size; ++xx)
{
if(xx>=0 && xx<img.cols)
{
for(int yy = y-dot_size; yy <=y+dot_size; ++yy)
{
if(yy>=0 && yy<img.rows)
{
cv::Vec3b &val = img_to_write.at<cv::Vec3b>(yy,xx);
val.val[0] = color[2];
val.val[1] = color[1];
val.val[2] = color[0];
}
}
}
}
}
cv::imwrite(imgName,img_to_write);
}
KLT_FeatureList KLTCreateFeatureList(
int nFeatures)
{
KLT_FeatureList fl;
KLT_Feature first;
int nbytes = sizeof(KLT_FeatureListRec) +
nFeatures * sizeof(KLT_Feature) +
nFeatures * sizeof(KLT_FeatureRec);
int i;
/* Allocate memory for feature list */
fl = (KLT_FeatureList)malloc(nbytes);
/* Set parameters */
fl->nFeatures = nFeatures;
/* Set pointers */
fl->feature = (KLT_Feature *) (fl + 1);
first = (KLT_Feature) (fl->feature + nFeatures);
for (i = 0 ; i < nFeatures ; i++) {
fl->feature[i] = first + i;
}
/* Return feature list */
return(fl);
}
void KLTFreeFeatureList(
KLT_FeatureList fl)
{
free(fl);
}
/*Count the # of valid features in feature list*/
int KLTCountRemainingFeatures(const KLT_FeatureList fl)
{
int count = 0;
for(int i = 0; i<fl->nFeatures; ++i)
{
if(fl->feature[i]->val>=0)
++count;
}
return count;
}
void FillMap(cv::Mat &ROI, const KLT_FeatureList fl, int gap)
{
for(int i = 0 ;i<fl->nFeatures; ++i)
{
if(fl->feature[i]->val>=0)
{
int x = int(fl->feature[i]->pos_x);
int y = int(fl->feature[i]->pos_y);
for(int xx = x-gap;xx<=x+gap;++xx)
{
if(xx>=0 && xx<ROI.cols)
{
for(int yy = y-gap; yy<=y+gap; ++yy)
{
if(yy>=0 && yy<ROI.rows)
{
ROI.at<uchar>(yy,xx) = 0;
}
}
}
}
}
}
}
void ShrinkFeatureList(KLT_FeatureList &featurelist)
{
int originalLength=featurelist->nFeatures;
int shrinkablenum=0;
//Count from back until meets a valid feature
for(int i=originalLength-1;featurelist->feature[i]->val<0&&i>=0;i--,shrinkablenum++);//count the shrinkable number
if(shrinkablenum==0)
return;
//fprintf(stderr,"%d shrinkable units!\n",shrinkablenum);
KLT_FeatureList newlist = KLTCreateFeatureList(originalLength-shrinkablenum);
memcpy(newlist->feature[0],featurelist->feature[0],newlist->nFeatures*sizeof(KLT_FeatureRec));//copy the feature data
KLTFreeFeatureList(featurelist);
featurelist=newlist;
}