-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatch_stream.cpp
More file actions
249 lines (219 loc) · 7.06 KB
/
match_stream.cpp
File metadata and controls
249 lines (219 loc) · 7.06 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
#include <math.h>
#include "util.hpp"
#include "markers.hpp"
#include "config.hpp"
#include "stitcher.hpp"
#include "source.hpp"
#include "grid.hpp"
using namespace std;
using namespace cv;
void showError(string error, UMat img) {
LOG(ERROR) << error << endl;
drawText(img, error, 2);
}
class Projector {
public:
Projector(Matx33f _warp, float _borderx, float _bordery,
float _cpi, float _rpi) {
warp = _warp;
borderx = _borderx;
bordery = _bordery;
cpi = _cpi;
rpi = _rpi;
}
Point2f pt(float x, float y, float bx=0.0f, float by=0.0f) {
Point2f pt = Point2f(x+bx*borderx*cpi,
y+by*bordery*rpi);
Point3f hi = warp.inv() * pt;
Point2f po(hi.x, hi.y);
return po;
}
Matx33f warp;
float borderx;
float bordery;
float cpi;
float rpi;
};
int main( int argc, char** argv ) {
Config config;
Markers markers(config);
char key;
namedWindow("Stitched Image", WINDOW_NORMAL);
resizeWindow("Stitched Image", 800, 800);
moveWindow("Stitched Image", 20, 20);
namedWindow("Cell", WINDOW_AUTOSIZE);
moveWindow("Cell", 600, 20);
namedWindow("Camera", WINDOW_AUTOSIZE);
moveWindow("Camera", 800, 20);
string filename = "stitched.jpeg";
Source *source;
if (argc==1) {
VideoCapture cap;
if(!cap.open(config.video_source)) {
LOG(ERROR) << "Could not open camera." << endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, config.image_width);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, config.image_height);
source = new VideoSource(cap);
// TODO: Do in Source
Mat junk;
for (int i=0; i<30; i++) cap >> junk;
config.setv4l(); // Exposure settings don't take unless we read some frames first.
} else {
VideoCapture cap;
if(!cap.open(argv[1])) {
LOG(ERROR) << "Could not open file." << endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, config.image_width);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, config.image_height);
source = new VideoSource(cap);
}
UMat stitchedImg = imread(filename).getUMat(ACCESS_READ);
imshow("Stitched Image", imscale(800, stitchedImg));
float matchScale = 1.0;
IncrementalStitcher stitcher(matchScale,
IncrementalStitcher::MatchMode::AGGREGATE,
IncrementalStitcher::DetectMethod::DETECT_SURF,
IncrementalStitcher::ExtractMethod::EXTRACT_FREAK);
const float markerboard_width_actual = (config.markerboard_width -
config.markerboard_offset*2.0f);
const float markerboard_height_actual = (config.markerboard_height -
config.markerboard_offset*2.0f);
float cols_per_inch = 186.0;
float rows_per_inch = 186.0;
// Burn a frame to set cpi & rpi.
UMat img_base;
source->nextImage(markers, img_base);
cols_per_inch = (float)img_base.cols / markerboard_width_actual;
rows_per_inch = (float)img_base.rows / markerboard_height_actual;
LOG(INFO) << "cpi: " << cols_per_inch << endl;
LOG(INFO) << "rpi: " << rows_per_inch << endl;
Grid grid(3, 3,
markerboard_width_actual,
markerboard_height_actual,
config.markerboard_project_width,
config.markerboard_project_height,
cols_per_inch, rows_per_inch);
grid.gx = 100.0;
grid.gy = img_base.rows-100;
getOffsets(grid.gx, grid.gy); // Read saved offsets from file.
grid.handleGridChange(stitchedImg);
bool gridMode = true;
bool nudgeMode = false;
bool moveMode = false;
while (!source->done()) {
UMat img2, cell, stitchedCopy;
stitchedImg.copyTo(stitchedCopy);
if (gridMode) {
// For grid mode, gray out base image and replace roi with color.
UMat stitchedGray;
cvtColor(stitchedCopy, stitchedGray, CV_RGB2GRAY);
cvtColor(stitchedGray, stitchedCopy, CV_GRAY2RGB);
Rect roi = grid.getRoi();
stitchedImg(roi).copyTo(stitchedCopy(roi));
}
grid.drawGrid(stitchedCopy);
// If we're in move mode, skip.
if (!moveMode) {
Markers::Status istatus = source->nextImage(markers, img2);
if (istatus == 0) {
Mat R;
IncrementalStitcher::Status status;
if (!gridMode) {
status = stitcher.detectAndMatch(stitchedCopy, img2, R);
} else {
status = stitcher.detectAndMatch(stitchedImg(grid.getRoi()), img2, R);
}
if (status == IncrementalStitcher::Status::OK) {
Matx33f warp = R;
Matx33f useWarp = warp;
if (gridMode) {
// Offset by grid offset.
warp(0,2) -= grid.getCell().x;
warp(1,2) -= grid.getCell().y;
grid.store(warp);
if (nudgeMode) {
useWarp = grid.avgWarp();
} else {
useWarp = warp;
}
float x = R.at<float>(0,2);
float y = R.at<float>(1,2);
float mx, my, mr, rx, ry, rr;
grid.avg(grid.ax, mx, rx);
grid.avg(grid.ay, my, ry);
grid.avg(grid.ar, mr, rr);
grid.drawMetrics(stitchedCopy, useWarp, x/cols_per_inch, y/cols_per_inch,
rx/cols_per_inch, ry/rows_per_inch, rr, nudgeMode);
imshow("Cell", imscale(600, stitchedCopy(grid.getRoi())));
}
imshow("Camera", imscale(600, img2));
// Draw markerboard projections.
const float borderx = (config.markerboard_project_width -
(markerboard_width_actual))/2.0f;
const float bordery = (config.markerboard_project_height -
(markerboard_height_actual))/2.0f;
Projector p(useWarp, borderx, bordery, cols_per_inch, rows_per_inch);
Point2f p0 = p.pt(0.0f, 0.0f);
Point2f p1 = p.pt(img2.cols, 0.0f);
Point2f p2 = p.pt(img2.cols, img2.rows);
Point2f p3 = p.pt(0.0f, img2.rows);
Point2f p0b = p.pt(0.0f, 0.0f, -1.0, -1.0);
Point2f p1b = p.pt(img2.cols, 0.0f, 1.0, -1.0);
Point2f p2b = p.pt(img2.cols, img2.rows, 1.0, 1.0);
Point2f p3b = p.pt(0.0f, img2.rows, -1.0, 1.0);
line(stitchedCopy, p0, p1, Scalar(255, 0, 0), 3, CV_AA);
line(stitchedCopy, p1, p2, Scalar(255, 0, 0), 3, CV_AA);
line(stitchedCopy, p2, p3, Scalar(255, 0, 0), 3, CV_AA);
line(stitchedCopy, p3, p0, Scalar(255, 0, 0), 3, CV_AA);
line(stitchedCopy, p0b, p1b, Scalar(255, 0, 0), 3, CV_AA);
line(stitchedCopy, p1b, p2b, Scalar(255, 0, 0), 3, CV_AA);
line(stitchedCopy, p2b, p3b, Scalar(255, 0, 0), 3, CV_AA);
line(stitchedCopy, p3b, p0b, Scalar(255, 0, 0), 3, CV_AA);
} else {
showError(stitcher.getError(status), stitchedCopy);
}
} else {
showError(markers.getError(istatus), stitchedCopy);
}
}
UMat scaleCopy = imscale(800, stitchedCopy);
if (stitchedCopy.cols > 0) {
drawText(scaleCopy, "(G)rid Next (N)udge (M)ove Mode");
imshow("Stitched Image", scaleCopy);
}
// Include a short pause for any drawing to catch up.
key = waitKey(10);
if (key == 27) break;
if (key == 'g') grid.next();
if (key == 'm') moveMode=!moveMode;
if (key == 'n') nudgeMode = !nudgeMode;
if (moveMode) {
bool changed = false;
if (key == 'Q') {
grid.gx--;
changed = true;
}
if (key == 'S') {
grid.gx++;
changed = true;
}
if (key == 'T') {
grid.gy++;
changed = true;
}
if (key == 'R') {
grid.gy--;
changed = true;
}
if (changed) {
saveOffsets(grid.gx, grid.gy);
grid.handleGridChange(stitchedImg);
}
}
}
key = (char) waitKey(0);
return 0;
}