-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlvarObjectTracker.cpp
313 lines (292 loc) · 11.9 KB
/
AlvarObjectTracker.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* AlvarObjectTracker.cpp
*
* Created on: 5 Oct 2014
* Author: mgw
*/
#include "AlvarObjectTracker.h"
#include "Camera.h"
using namespace cv;
using namespace alvar;
using namespace TracingFramework;
AlvarObjectTracker::AlvarObjectTracker(Configuration conf, String calibfn) {
cout << "AlvarObjectTracker tracker"<< endl;
config = &conf;
marker_size = atof(config->getValueByKey("markerSize").c_str());
double width = atof(config->getValueByKey("resolution_y").c_str());
double height = atof(config->getValueByKey("resolution_x").c_str());
cout << "readCameraParams"<< endl;
bool data_read = readCameraParams(calibfn, cameraMatrix, distCoeffs);
if (!data_read){
cout << "Calib file didn't read!"<< endl;
}
homographyFound = false;
//cam.SetCalib(calibfn.c_str(), width, height);
}
AlvarObjectTracker::AlvarObjectTracker(Configuration conf, String M, String D){
cout << "AlvarObjectTracker tracker"<< endl;
config = &conf;
marker_size = atof(config->getValueByKey("markerSize").c_str());
double width = atof(config->getValueByKey("resolution_y").c_str());
double height = atof(config->getValueByKey("resolution_x").c_str());
cout << "readCameraParams"<< endl;
bool data_read = readCameraParams(M, D, cameraMatrix, distCoeffs);
if (!data_read){
cout << "Calib file didn't read!"<< endl;
}
homographyFound = false;
}
bool AlvarObjectTracker::readCameraParams( string calib_file, Mat& cameraMatrix, Mat& distCoeffs ) {
cv::FileStorage fs;
cout << "fs" << endl;
fs.open( calib_file, FileStorage::READ );
cout << "fs open" << endl;
bool fsIsOpened = fs.isOpened();
if(fsIsOpened)
{
fs["Camera_Matrix"] >> cameraMatrix;
fs["Distortion_Coefficients"] >> distCoeffs;
}
return fsIsOpened;
}
bool AlvarObjectTracker::readCameraParams( string M, string D, Mat& cameraMatrix, Mat& distCoeffs ) {
cv::FileStorage fsM, fsD;
cout << "fs" << endl;
fsM.open( M, FileStorage::READ );
fsD.open( D, FileStorage::READ );
cout << "fs open" << endl;
bool fsMIsOpened = fsM.isOpened();
bool fsDIsOpened = fsD.isOpened();
if(fsMIsOpened)
{
fsM["Camera_Matrix"] >> cameraMatrix;
}
if (fsDIsOpened){
fsD["Distortion_Coefficients"] >> distCoeffs;
}
return fsDIsOpened && fsMIsOpened;
}
AlvarObjectTracker::~AlvarObjectTracker() {
// TODO Auto-generated destructor stub
}
String AlvarObjectTracker::trackInPicture(Mat picture, String time){
cout << "track0" << endl;
IplImage image = picture;
IplImage *image_tmp = ℑ
String result;
cout << "track2" << endl;
Mat imageUndistortedMat;
vector<Point2f> pointBuf;
undistort(picture, imageUndistortedMat, cameraMatrix, distCoeffs);
Mat homography;
if (!this->homographyFound){
homography = findHomography(imageUndistortedMat, Size(9,6), float(2.5), cameraMatrix, distCoeffs, pointBuf);
homographyInternal = homography.clone();
} else {
homography = homographyInternal.clone();
}
static alvar::MarkerDetector<alvar::MarkerData> marker_detector;
cout << "track3" << endl;
marker_detector.SetMarkerSize(marker_size);
marker_detector.Detect(image_tmp, &cam, true, true);
cout << "track4" << endl;
cout << "Markers: " << marker_detector.markers->size() << endl;
if(homography.data != NULL && marker_detector.markers->size() > 0){
cout << "Found sth" << endl;
int markerIndx = 0;
int cornerIndx = 0;
float markerPosX0 = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].x;
float markerPosY0 = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].y;
cornerIndx = 1;
float markerPosXX = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].x;
float markerPosYX = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].y;
cornerIndx = 2;
float markerPosXY = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].x;
float markerPosYY = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].y;
vector<Point2f> orgPoint;
orgPoint.push_back(Point2f(markerPosX0, markerPosY0));
orgPoint.push_back(Point2f(markerPosXX, markerPosYX));
orgPoint.push_back(Point2f(markerPosXY, markerPosYY));
vector<Point2f> udorgPoint(orgPoint);
cout << "try undistort" << endl;
undistortPoints(orgPoint, udorgPoint, cameraMatrix, distCoeffs);
vector<Point2f> rorgPoint(udorgPoint);
cout << "try perspectiveTransform" << endl;
perspectiveTransform( udorgPoint, rorgPoint, homography);
cout << "try output" << endl;
for (int i = 0; i < 3; ++i) {
result += boost::lexical_cast<std::string>(i) + " " +
boost::lexical_cast<std::string>(rorgPoint[i].x) + " " +
boost::lexical_cast<std::string>(rorgPoint[i].y) + " " +
time + "\n";
}
result += "\n";
}
cout << "track5" << endl;
return result;
};
vector<std::pair<Point2f,String>> AlvarObjectTracker::trackInPictureV(Mat picture, String time){
cout << "track0" << endl;
IplImage image = picture;
IplImage *image_tmp = ℑ
vector<std::pair<Point2f,String>> result;
cout << "track2" << endl;
Mat imageUndistortedMat;
vector<Point2f> pointBuf;
undistort(picture, imageUndistortedMat, cameraMatrix, distCoeffs);
Mat homography;
if (!this->homographyFound){
homography = findHomography(imageUndistortedMat, Size(9,6), float(2.5), cameraMatrix, distCoeffs, pointBuf);
homographyInternal = homography.clone();
} else {
homography = homographyInternal.clone();
}
static alvar::MarkerDetector<alvar::MarkerData> marker_detector;
cout << "track3" << endl;
marker_detector.SetMarkerSize(marker_size);
marker_detector.Detect(image_tmp, &cam, true, true);
cout << "track4" << endl;
cout << "Markers: " << marker_detector.markers->size() << endl;
if(homography.data != NULL && marker_detector.markers->size() > 0){
cout << "Found sth" << endl;
int markerIndx = 0;
int cornerIndx = 0;
float markerPosX0 = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].x;
float markerPosY0 = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].y;
cornerIndx = 1;
float markerPosXX = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].x;
float markerPosYX = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].y;
cornerIndx = 2;
float markerPosXY = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].x;
float markerPosYY = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].y;
vector<Point2f> orgPoint;
orgPoint.push_back(Point2f(markerPosX0, markerPosY0));
orgPoint.push_back(Point2f(markerPosXX, markerPosYX));
orgPoint.push_back(Point2f(markerPosXY, markerPosYY));
vector<Point2f> udorgPoint(orgPoint);
cout << "try undistort" << endl;
undistortPoints(orgPoint, udorgPoint, cameraMatrix, distCoeffs);
vector<Point2f> rorgPoint(udorgPoint);
cout << "try perspectiveTransform" << endl;
perspectiveTransform( udorgPoint, rorgPoint, homography);
cout << "try output" << endl;
Point2f partial;
for (int i = 0; i < 3; ++i) {
partial.x = rorgPoint[i].x;
partial.y = rorgPoint[i].y;
result.push_back(std::make_pair(partial,time));
}
}
cout << "track5" << endl;
return result;
};
vector<String> AlvarObjectTracker::trackInPictures(vector<std::pair<Mat,String>> pictures){
vector<String> result;
String partial;
for (std::vector<std::pair<Mat,String>>::iterator it = pictures.begin(); it != pictures.end(); ++it){
cout << "Tracking in picture" << endl;
partial = trackInPicture((*it).first,(*it).second);
result.push_back(partial);
}
return result;
};
vector<vector<std::pair<Point2f,String>>> AlvarObjectTracker::trackInPicturesV(vector<std::pair<Mat,String>> pictures){
vector<vector<std::pair<Point2f,String>>> result;
vector<std::pair<Point2f,String>> partial;
for (std::vector<std::pair<Mat,String>>::iterator it = pictures.begin(); it != pictures.end(); ++it){
cout << "Tracking in picture" << endl;
partial = trackInPictureV((*it).first,(*it).second);
result.push_back(partial);
}
return result;
};
vector<String> AlvarObjectTracker::trackInVideo(String filename){
vector<String> result;
return result;
};
void AlvarObjectTracker::saveTrackToFile(vector<String> pos, String filename){
};
Mat AlvarObjectTracker::findHomography(Mat img, Size pattern_size, float squareSize,
Mat intrinsic_matrix, Mat distortion_coeffs, vector<Point2f> &pointBuf){
bool found = false;
flip( img, img, 0 );
found = findChessboardCorners( img, pattern_size, pointBuf,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
if (found){
cout << "findChessboardCorners found sth" << endl;
} else {
cout << "findChessboardCorners didn't find anth" << endl;
}
vector<Point2f> objectPoints;
calcBoardCornerPositions(pattern_size, squareSize, objectPoints);
Mat homography;
cout << "pointBuf.size(): " << pointBuf.size() << endl;
cout << "objectPoints.size(): " << objectPoints.size() << endl;
if(pointBuf.size() > 0 && pointBuf.size() == objectPoints.size())
{
cout << "try homography" << endl;
vector<Point2f> cpointBuf(pointBuf);
undistortPoints(pointBuf, cpointBuf, intrinsic_matrix, distortion_coeffs);//TODO spr
homography = cv::findHomography(cpointBuf, objectPoints, homography);
cout << "homography success" << endl;
}
return homography;
}
void AlvarObjectTracker::calcBoardCornerPositions(Size boardSize, float squareSize, vector<Point2f>& corners)
{
corners.clear();
for( int i = 0; i < boardSize.height; ++i )
for( int j = 0; j < boardSize.width; ++j )
corners.push_back(Point2f(float( j*squareSize ), float( i*squareSize )));
}
vector<Point2f> AlvarObjectTracker::trackInPicturePixels(Mat picture){
vector<Point2f> res;
cout << "track0" << endl;
IplImage image = picture;
IplImage *image_tmp = ℑ
String result;
cout << "track2" << endl;
Mat imageUndistortedMat;
vector<Point2f> pointBuf;
//undistort(picture, imageUndistortedMat, cameraMatrix, distCoeffs);
static alvar::MarkerDetector<alvar::MarkerData> marker_detector;
cout << "track3" << endl;
marker_detector.SetMarkerSize(marker_size);
marker_detector.Detect(image_tmp, &cam, true, true);
cout << "track4" << endl;
if(marker_detector.markers->size() > 0){
cout << "Found sth" << endl;
int markerIndx = 0;
int cornerIndx = 0;
float markerPosX0 = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].x;
float markerPosY0 = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].y;
cornerIndx = 1;
float markerPosXX = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].x;
float markerPosYX = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].y;
cornerIndx = 2;
float markerPosXY = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].x;
float markerPosYY = (*(marker_detector.markers))[markerIndx].marker_corners_img[cornerIndx].y;
vector<Point2f> orgPoint;
orgPoint.push_back(Point2f(markerPosX0, markerPosY0));
orgPoint.push_back(Point2f(markerPosXX, markerPosYX));
orgPoint.push_back(Point2f(markerPosXY, markerPosYY));
vector<Point2f> udorgPoint(orgPoint.size());
cout << "try undistort" << endl;
/*undistortPoints(orgPoint, udorgPoint, cameraMatrix, distCoeffs);
res.push_back(Point2f(udorgPoint[0].x,udorgPoint[0].y));
res.push_back(Point2f(udorgPoint[1].x,udorgPoint[1].y));
res.push_back(Point2f(udorgPoint[2].x,udorgPoint[2].y));*/
res.push_back(Point2f(orgPoint[0].x,orgPoint[0].y));
res.push_back(Point2f(orgPoint[1].x,orgPoint[1].y));
res.push_back(Point2f(orgPoint[2].x,orgPoint[2].y));
}
return res;
}
vector<pair<Point2f,String>> AlvarObjectTracker::trackInPicturePixelsV(Mat picture, String time){
vector<Point2f> res_no_time = trackInPicturePixels(picture);
vector<pair<Point2f,String>> res;
for (int i = 0; i< res_no_time.size(); i++){
res.push_back(make_pair(res_no_time[i],time));
}
return res;
}