-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_OLD.cpp
281 lines (230 loc) · 9.14 KB
/
main_OLD.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
#include <csignal>
// #include <iostream>
#include "./headers/converter.hpp"
#include "./headers/settings.hpp"
#include "./impl/object_recognition.cpp"
#include "./impl/templategen.cpp"
#include "./impl/utils.cpp"
using namespace cv;
using namespace std;
using namespace dm;
// TODO check if an image exists in the location
// TODO frontend
// TODO show log statistics
// TODO
// TODO backend
// TODO save test image in a "database"
// TODO
// TODO
// TODO free cam on process kill
// TODO use zed.setRegionOfInterest to discard all area beyond the projector
struct InteractiveState {
char key;
bool pause = false;
bool next = false;
int idx = 0;
void printHelp() {
cout << " Press 'q' to exit..." << endl;
cout << " Press 'p' to exit..." << endl;
}
void pauseApp() {
if (key == 'p') pause = !pause;
while (pause && !next) {
key = cv::waitKey(0);
if (key == 'p') pause = !pause;
if (key == ' ' || key == 'q') next = true;
}
}
};
void signalHandler(int signalNumber);
void process2(Logger &logger, CameraManager &camMan, Settings &settings,
ImageProcessor &image);
void process(Logger &logger, CameraManager &camMan, Settings &settings,
ImageProcessor &image);
void applyTemplates();
void createVideo();
int main(int argc, char **argv) {
// TODO add more signals, work on handling
signal(SIGTERM, signalHandler);
signal(SIGHUP, signalHandler);
Settings settings;
Printer printer;
auto Result = settings.Init(argc, argv);
if (Result == Printer::ERROR::ARGS_FAILURE) {
printer.log_message({Printer::ERROR::ARGS_FAILURE, {}, argv[0]});
return EXIT_FAILURE;
}
try {
settings.Parse();
} catch (const std::exception &x) {
printer.log_message({Printer::ERROR::FLAGS_FAILURE, {}, argv[0]});
printer.log_message(x);
return EXIT_FAILURE;
}
printer.setDebugLevel(
static_cast<Printer::DEBUG_LVL>(settings.debug_level));
Logger logger("log", 0, 0, settings.save_logs, settings.measure_time,
settings.debug_level);
ImageProcessor image(settings.outputLocation.value, logger, printer);
if (settings.type == Settings::SOURCE_TYPE::IMAGE) {
// image = ImageProcessor(settings.FilePath,
// settings.outputLocation.value,
// logger, printer);
image.getImage(settings.FilePath);
} else {
CameraManager camMan;
if (settings.type == Settings::SOURCE_TYPE::SVO)
// does it even work?
camMan.initSVO((settings.FilePath).c_str());
CameraManager::Params params{
.depth_mode =
static_cast<sl::DEPTH_MODE>(settings.depth_mode.value),
.max_distance = settings.camera_distance,
.threshold = settings.threshold,
.texture_threshold = settings.texture_threshold,
.fill_mode = settings.fill_mode};
// CameraManager::Params params(
// static_cast<sl::DEPTH_MODE>(settings.depth_mode.value),
camMan.setParams(params);
camMan.openCamera();
//
//
// Main loop: show frame by frame generated video images (via
// templategen function)
// But start with a calibration: show an image of checkerboard
// Then read it with camera and understand an area of operations from
// that
// -- get an image of the board and form all the neccacery spatial
// transitions for a resulting vido to be projected (fish-eye, spatial
// rotation)
//
//
InteractiveState state;
state.printHelp();
string window_name = "Projection";
cv::namedWindow(window_name, cv::WindowFlags::WINDOW_NORMAL);
// cv::namedWindow(window_name, cv::WindowFlags::WINDOW_FULLSCREEN);
cv::setWindowProperty(window_name,
cv::WindowPropertyFlags::WND_PROP_FULLSCREEN,
cv::WindowFlags::WINDOW_FULLSCREEN);
Templates templates(image.image);
vector<cv::Mat> &masks = image.mask_mats;
vector<cv::Mat> results;
while (state.key != 'q') {
// ? multithreaded? One video creation, one showing and one server
state.next = 0;
state.idx = 0;
vector<cv::Mat> &masks = image.mask_mats;
vector<cv::Mat> results;
// masks = image.mask_mats;
// results.clear();
process2(logger, camMan, settings, image);
try {
for (auto mask : masks) {
results.push_back(templates.solidColor(
mask,
{(double)(240 - state.idx), (double)(220 + (state.idx)),
(double)(210 + (state.idx * 3))}));
state.idx++;
}
for (int i = 0; i < results.size() - 1; i++) {
cv::add(results.at(i), results.at(i + 1),
results.at(i + 1));
}
// cv::imshow(window_name, image.m_objects);
cv::imshow(window_name, results.at(results.size() - 1));
} catch (const std::exception &e) {
std::cerr << e.what() << '\n';
}
state.key = cv::waitKey(10);
// TODO color coding for objects via tamplates
state.pauseApp();
// ! calibration
}
camMan.~CameraManager();
}
return EXIT_SUCCESS;
}
void process2(Logger &logger, CameraManager &camMan, Settings &settings,
ImageProcessor &image) {
logger.start();
auto returned_state = camMan.grab();
if (returned_state <= ERROR_CODE::SUCCESS) {
std::pair<sl::Mat &, sl::Mat &> result = camMan.imageProcessing();
// image.write(settings.outputLocation.value + "init_image.png");
sl::Mat texture = result.first;
sl::Mat depth = result.second;
cv::Mat img = slMat2cvMat(depth);
cv::imwrite(settings.outputLocation.value + "testy.png", img);
// auto state = result.second.write(
// (settings.outputLocation.value + "init_image.png").c_str());
// image.getImage(img);
image.getImage(img);
} else {
throw runtime_error("Coudn't grab a frame");
}
logger.stop("Zed grab");
// TODO 5544332244
image.dilate(5, 5);
image.erode(5, 5);
image.write(settings.outputLocation.value + "modified_image.png");
image.findObjects(settings.zlimit, settings.minDistance,
settings.medium_limit, settings.minArea,
settings.maxObjects, settings.recurse);
}
void process(Logger &logger, CameraManager &camMan, Settings &settings,
ImageProcessor &image) {
logger.start();
auto returned_state = camMan.grab();
if (returned_state <= ERROR_CODE::SUCCESS) {
std::pair<sl::Mat &, sl::Mat &> result = camMan.imageProcessing();
// TODO romeve this workaround. It currently saves an image
// TODO and then reads it.
// TODO it seems, slMat2cvMat has some kind of a bug
image.write(settings.outputLocation.value + "init_image.png");
// cv::Mat img = slMat2cvMat(result.second);
auto state = result.second.write(
(settings.outputLocation.value + "init_image.png").c_str());
// image.getImage(img);
image.getImage(settings.outputLocation.value + "init_image.png");
} else {
throw runtime_error("Coudn't grab a frame");
}
logger.stop("Zed grab");
// TODO 5544332244
image.dilate(5, 5);
image.erode(5, 5);
image.write(settings.outputLocation.value + "modified_image.png");
image.findObjects(settings.zlimit, settings.minDistance,
settings.medium_limit, settings.minArea,
settings.maxObjects, settings.recurse);
}
void applyTemplates() {}
void createVideo() {
// VideoWriter video(outputLocation + "color_gradient.avi",
// VideoWriter::fourcc('M', 'J', 'P', 'G'), 60, size);
// if (!video.isOpened()) {
// return -1;
// }
// vector<cv::Mat> masks = image.mask_mats;
// Templates templates(image.image);
// for (int i = 0; i < 10 * 60; i++) { // 10 seconds at 60 fps
// vector<cv::Mat> results;
// results.push_back(templates.chessBoard(i, masks.at(1), 2, 1));
// results.push_back(templates.chessBoard(i, masks.at(2), 1, 3));
// results.push_back(templates.gradient(i, masks.at(3), 1));
// results.push_back(templates.gradient(i, masks.at(4), 5));
// cv::add(results.at(0), results.at(1), results.at(1));
// cv::add(results.at(1), results.at(2), results.at(2));
// cv::add(results.at(2), results.at(3), results.at(3));
// video.write(results.at(3));
// }
// video.release();
}
void signalHandler(int signalNumber) {
if (signalNumber == SIGTERM || signalNumber == SIGHUP) {
std::cout << "Recieved interupt signal: " << signalNumber
<< "\nExiting..." << std::endl;
exit(signalNumber);
}
}