-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
284 lines (238 loc) · 8.04 KB
/
main.cpp
File metadata and controls
284 lines (238 loc) · 8.04 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
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
//
// Created by hegedus on 2017.11.26..
//
#include <opencv2/opencv.hpp>
#include <opencv2/tracking/tracker.hpp>
#include <opencv2/tracking/tldDataset.hpp>
#include <opencv2/core/ocl.hpp>
#include <iostream>
#include "detector.h"
using namespace std;
using namespace cv;
void imageDetection(char* path);
void videoDetection(char* path);
void calculationsOnFrame(Mat &frame, Detection &detection, std::vector<cv::Vec3f> circles, Mat &mask);
void runTest();
void saveTimeline();
void savePoints();
float focalLength = 3272.61360667f;
float HFOV = 1.32963f; // 76.18°
float VFOV = 0.68511f; // 39,25°
static float KNOWN_DISTANCE = 30.0f;
static float KNOWN_WIDTH = 7.5f;
std::vector<float> timeline;
std::vector<Point3f> points;
int main(int argc, char** argv) {
if(argc != 3 && argc != 4) {
if(argc == 1) {
runTest();
return 0;
}
cout << "Missing parameter";
return -1;
}
switch (argv[1][1]) {
case 'i' :
imageDetection(argv[2]);
break;
case 'v' :
videoDetection(argv[2]);
break;
case 'c' :
//-c /home/hegedus/Videos/IMG_20171208_235426.jpg /home/hegedus/Videos/ball_camera_120.mp4
if(argc == 3) {
cout << "Missing parameter";
return -1;
}
std::cout << argv[2] << "\n" << argv[3];
//imageDetection(argv[2]);
videoDetection(argv[3]);
break;
case 'f':
imageDetection((char*)"/home/hegedus/Videos/red_ball_30cm.jpg");
KNOWN_DISTANCE = 60.0f;
imageDetection((char*)"/home/hegedus/Videos/red_ball_60cm.jpg");
KNOWN_DISTANCE = 90.0f;
imageDetection((char*)"/home/hegedus/Videos/red_ball_90cm.jpg");
KNOWN_DISTANCE = 120.0f;
imageDetection((char*)"/home/hegedus/Videos/red_ball_120cm.jpg");
default:
std::cout << "Invalid parameters\n";
}
return 0;
}
void runTest() {
Mat frame, mask;
std::vector<Vec3f> circles;
Detection detection;
int i = 30;
bool calculated = false;
bool loop = true;
while(loop) {
int key = waitKey(30);
switch(key) {
case 'n': {
if(i < 200) {
i += 5;
calculated = false;
} else {
saveTimeline();
savePoints();
return;
}
break;
}
case (27):
return;
default:
break;
}
string path = "/home/hegedus/Videos/test_engineer/Photos/";
string name = to_string(i) + ".jpg";
string full_path = path + name;
frame = imread(full_path, 1);
if(!frame.data) {
std::cout << "no image data";
return;
}
if(!calculated) {
rectangle(frame, Rect(0, 0, 2400, 450), Scalar(255,255,255), CV_FILLED, 8, 0);
calculationsOnFrame(frame, detection, circles, mask);
calculated = true;
double scale = 4;
Point org_irl = cvPoint(10,100);
putText(frame, "Distance irl: " + to_string(i) + " cm", org_irl, FONT_HERSHEY_SIMPLEX, scale, Scalar(0, 0, 255, 255), 8);
resize(frame, frame, Size(1920, 1080), 0, 0, INTER_LANCZOS4);
imshow("test", frame);
}
}
}
void calculationsOnFrame(Mat &frame, Detection &detection, std::vector<cv::Vec3f> circles, Mat &mask) {
detection.detect(frame, circles);
detection.drawCircles(frame, circles, Scalar(255, 0, 0));
/*mask = detection.getMask(frame);
detection.drawCircles(mask, circles, Scalar(255, 0, 0));*/
if(!circles.empty() && circles.size() == 1) {
char fps_str[50];
char coordinate_str[1000];
float distance = detection.distanceToCamera(KNOWN_WIDTH, focalLength, circles[0][2] * 2);
Point3f point = detection.calculateCoordinates(focalLength, distance, frame.rows, frame.cols, circles[0][0], circles[0][1]);
timeline.push_back(distance);
points.push_back(point);
sprintf(fps_str, "Distance: %f cm ", distance);
sprintf(coordinate_str, "Point( x = %.lf, y = %.lf, z = %.lf ) ", point.x, point.y, point.z);
double scale = 4;
//Point org = cvPoint(frame.cols / 7, frame.rows / 14);
Point org = cvPoint(10, 250);
//Point org_point = cvPoint(frame.cols / 7, frame.rows / 4);
Point org_point = cvPoint(10, 400);
putText(frame, fps_str, org, FONT_HERSHEY_SIMPLEX, scale, Scalar(0, 0, 255, 255), 8);
putText(frame, coordinate_str, org_point, FONT_HERSHEY_SIMPLEX, scale, Scalar(0, 0, 255, 255), 8);
}
}
void saveTimeline() {
/*ofstream file;
file.open("TIMELINE.txt");
for(size_t i = 0; i < timeline.size(); i++) {
file << timeline[i] << "\n";
}
file.close();
std::cout << "file(timeline) has been written";*/
}
void savePoints() {
/*ofstream file;
file.open("POINTS.txt");
for(size_t i = 0; i < points.size(); i++) {
file << points[i] << "\n";
}
file.close();
std::cout << "file(points) has been written";*/
}
void videoDetection(char* path) {
std::cout << "video\n";
VideoCapture video(path);
if(!video.isOpened()) {
cout << "Could not read video file\n";
return;
}
Mat frame;
vector<Vec3f> circles;
Detection detection;
video.read(frame);
resize(frame, frame, Size(1024, 576), 0, 0, INTER_LANCZOS4);
imshow("default window", frame);
Mat mask;
bool state = true;
bool cont = true;
while (state) {
int key = waitKey(30);
switch(key) {
case (27):
state = false;
case (97): {
if(!video.read(frame))
state = false;
else
calculationsOnFrame(frame, detection, circles, mask);
break;
}
case (98):
cont = false;
break;
case (99):
cont = true;
break;
case (100):
saveTimeline();
savePoints();
break;
default:
break;
}
resize(frame, frame, Size(1920, 1080), 0, 0, INTER_LANCZOS4);
//resize(mask, mask, Size(1024, 576), 0, 0, INTER_LANCZOS4);
imshow("default window", frame);
//imshow("not def window", mask);
if(cont) {
if(!video.read(frame)) {
state = false;
saveTimeline();
savePoints();
} else {
calculationsOnFrame(frame, detection, circles, mask);
}
}
}
}
void imageDetection(char* path) {
Mat image;
Detection detection;
image = imread(path, 1);
if (!image.data) {
std::cout << "No image data \n";
return;
}
Mat frame = image;
std::vector<cv::Vec3f> circles;
frame = imread(path, 1);
detection.detect(frame, circles);
detection.drawCircles(frame, circles, Scalar(255, 0, 0));
imshow("def", frame);
while (true) {
if(waitKey(0) == 27) {
break;
}
imshow("def", frame);
}
focalLength = (circles[0][2] * 2 * KNOWN_DISTANCE) / KNOWN_WIDTH;
char buff[50];
sprintf(buff, "%f = (%f * 2 * %f) / %f\n", focalLength, circles[0][2], KNOWN_DISTANCE, KNOWN_WIDTH);
std::cout << buff;
float distance = detection.distanceToCamera(KNOWN_WIDTH, focalLength, circles[0][2] * 2);
//std::cout << "frame width: " << frame.cols << "\t frame height: " << frame.rows << "\n";
float HFOV = detection.calculateFOV(focalLength, frame.cols);
float VFOV = detection.calculateFOV(focalLength, frame.rows);
Point3f coord = detection.calculateCoordinates(focalLength, distance, frame.cols, frame.rows, circles[0][0], circles[0][1]);
std::cout << HFOV << "\t" << VFOV << "\n";
std::cout << "distance = " << distance << "\n";
}