-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrid.cpp
More file actions
236 lines (207 loc) · 6.23 KB
/
grid.cpp
File metadata and controls
236 lines (207 loc) · 6.23 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
#include "grid.hpp"
// TODO: Replace
void undoScale2(Matx33f H, float s) {
H(0,0) = H(0,0) / s;
H(0,1) = H(0,1) / s;
H(1,0) = H(1,0) / s;
H(1,1) = H(1,1) / s;
}
Grid::Grid(int _grid_cols, int _grid_rows,
float _cell_width, float _cell_height,
float _cell_project_width, float _cell_project_height,
float _cols_per_inch, float _rows_per_inch) {
int n = 1;
cols = _grid_cols;
rows = _grid_rows;
cell_width = _cell_width;
cell_height = _cell_height;
cell_project_width = _cell_project_width;
cell_project_height = _cell_project_height;
cols_per_inch = _cols_per_inch;
rows_per_inch = _rows_per_inch;
for (int r=0; r<_grid_rows; r++) {
for (int c=0; c<_grid_cols; c++) {
Point2f p(c*cols_per_inch*cell_project_width,
r*-1*rows_per_inch*cell_project_height);
cells.push_back(p);
names.push_back("G" + to_string(n++));
}
}
}
Point2f Grid::getCell() {
return getCell(selected);
}
Point2f Grid::getCell(int c) {
return Point2f(cells[c].x+gx-(cell_width-cell_project_width)*cols_per_inch/2,
cells[c].y+gy-(cell_height-cell_project_height)*rows_per_inch/2);
}
Point2f Grid::getCellProject() {
return getCellProject(selected);
}
Point2f Grid::getCellProject(int c) {
return Point2f(cells[c].x+gx, cells[c].y+gy);
}
void Grid::drawRectProject(UMat img, int c) {
Rect roi = getRoiProject(c);
Mat rimg = img.getMat(ACCESS_RW);
rectangle(rimg, roi, Scalar(128,128,128), 5);
drawGridText(img, roi, names[c], 20.0);
}
void Grid::drawRect(UMat img, int c) {
Rect roi = getRoi(c);
Mat rimg = img.getMat(ACCESS_RW);
rectangle(rimg, roi, Scalar(0,0,255), 5);
}
void Grid::drawGrid(UMat img) {
for (int i=0; i<cells.size(); i++) {
drawRectProject(img, i);
drawRect(img, i);
}
}
void Grid::drawMetrics(UMat img, Matx33f warp,
float x, float y, float rx, float ry, float rr, int mode) {
double t = atan(warp(1,0) / warp(0,0));
double deg = t * (180/3.1415926535897) * -1;
char dbuf [7];
sprintf(dbuf, "%.2f", deg);
char xbuf [7];
sprintf(xbuf, "%.3f", x);
char ybuf [7];
sprintf(ybuf, "%.3f", y);
char rxbuf [7];
sprintf(rxbuf, "%.3f", rx);
char rybuf [7];
sprintf(rybuf, "%.3f", ry);
char rrbuf [7];
sprintf(rrbuf, "%.3f", rr);
Rect roi = getRoi();
drawGridTextSmall(img, roi, "Y: " + string(ybuf) + "in +/- " +
string(rybuf) + "in", 1.0, mode);
drawGridTextSmall(img, roi, "X: " + string(xbuf) + "in +/- " +
string(rxbuf) + "in", 3.0, mode);
drawGridTextSmall(img, roi, "Rotation: " + string(dbuf) + "d +/- " +
string(rrbuf) + "d", 5.0, mode);
}
Rect Grid::getGridRoi() {
return Rect(Point2f(gx, gy-(rows-1)*cell_project_height*rows_per_inch),
Size(cols*cell_project_width*cols_per_inch,
rows*cell_project_height*rows_per_inch));
}
Rect Grid::getRoi() {
return getRoi(selected);
}
Rect Grid::getRoi(int c) {
Point2f p = getCell(c);
Rect r = Rect(p, Size(cell_width*cols_per_inch, cell_height*rows_per_inch));
return r;
}
Rect Grid::getRoiProject(int c) {
Point2f p = getCellProject(c);
Rect r = Rect(p, Size(cell_project_width*cols_per_inch,
cell_project_height*rows_per_inch));
return r;
}
void Grid::prev() {
selected--;
if (selected < 0) {
selected = cells.size() - 1;
}
}
void Grid::next() {
selected++;
if (selected >= cells.size()) {
selected = 0;
}
}
void Grid::logStats(Matx33f aH) {
ax.insert(ax.begin(), aH(0,2));
ay.insert(ay.begin(), aH(1,2));
ar.insert(ar.begin(), getAngle(aH));
as.insert(as.begin(), getScale(aH));
while (ax.size() > MAX) ax.pop_back();
while (ay.size() > MAX) ay.pop_back();
while (ar.size() > MAX) ar.pop_back();
while (as.size() > MAX) as.pop_back();
}
void Grid::avg(vector<float> list, float& mean, float& range) {
int count = list.size();
float sum=0;
mean=0;
float min = 1000000;
float max = -1000000;
for (int i=0; i<count; i++) {
sum += list[i];
if (list[i] < min) min = list[i];
if (list[i] > max) max = list[i];
}
mean = sum / (float)count;
range = max - min;
}
void Grid::store(Matx33f warp) {
logStats(warp);
undoScale2(warp, (getScale(warp))); // FYI: destructive
warps.insert(warps.begin(), warp);
while (warps.size() > MAX) warps.pop_back();
}
Matx33f Grid::avgWarp() {
Matx33f aWarp;
aWarp(2,2) = 1.0f;
int count = warps.size();
for (int i=0; i<count; i++) {
aWarp(0,0) += warps[i](0,0);
aWarp(0,1) += warps[i](0,1);
aWarp(0,2) += warps[i](0,2);
aWarp(1,0) += warps[i](1,0);
aWarp(1,1) += warps[i](1,1);
aWarp(1,2) += warps[i](1,2);
}
aWarp(0,0) /= count;
aWarp(0,1) /= count;
aWarp(0,2) /= count;
aWarp(1,0) /= count;
aWarp(1,1) /= count;
aWarp(1,2) /= count;
return aWarp;
}
// In pixels...
void Grid::showStats(vector<float> list, string name) {
float mean, range;
avg(list, mean, range);
cout << name << ": " << mean << " " << range << endl;
}
void Grid::showStats() {
showStats(ax, "ax");
showStats(ay, "ay");
showStats(ar, "ar");
showStats(as, "as");
}
// Scale image to accomodate grid.
void Grid::handleGridChange(UMat& stitchedImg) {
Rect roi = getGridRoi();
// Handle <0 exceeds-bounds case.
if (roi.x < 0 || roi.y < 0) {
float dx = roi.x < 0 ? roi.x * -1 : 0;
float dy = roi.y < 0 ? roi.y * -1 : 0;
LOG(INFO) << "dx: " << dx << " dy:" << dy << endl;
UMat bufImg = UMat::zeros(stitchedImg.rows+dy, stitchedImg.cols+dx,
stitchedImg.type());
Rect croi(Point2f(dx, dy), Size(stitchedImg.cols, stitchedImg.rows));
stitchedImg.copyTo(bufImg(croi));
bufImg.copyTo(stitchedImg);
gx += dx;
gy += dy;
roi = getGridRoi(); // update roi
// Save corresponding changes to image if they impact relative offsets.
imwrite("stitched.jpeg", stitchedImg);
saveOffsets(gx, gy);
}
// Handle >cols or >rows exceeds case.
if (roi.x + roi.width > stitchedImg.cols || roi.y + roi.height > stitchedImg.rows) {
float w = roi.x + roi.width > stitchedImg.cols ? roi.x + roi.width : stitchedImg.cols;
float h = roi.y + roi.height > stitchedImg.rows ? roi.y + roi.height : stitchedImg.rows;
UMat bufImg = UMat::zeros(h, w, stitchedImg.type());
Rect croi(Point2f(0,0), Size(stitchedImg.cols, stitchedImg.rows));
stitchedImg.copyTo(bufImg(croi));
bufImg.copyTo(stitchedImg);
}
}