-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathshapes.cpp
More file actions
393 lines (351 loc) · 13.3 KB
/
shapes.cpp
File metadata and controls
393 lines (351 loc) · 13.3 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <cmath>
#include "picpac-image.h"
namespace picpac {
vector<cv::Scalar> PALETTE{
{180, 119, 31}, {232, 199, 174}, {14, 127, 255}, {120, 187, 255},
{44, 160, 44}, {138, 223, 152}, {40, 39, 214}, {150, 152, 255},
{189, 103, 148}, {213, 176, 197}, {75, 86, 140}, {148, 156, 196},
{194, 119, 227}, {210, 182, 247}, {127, 127, 127}, {199, 199, 199},
{34, 189, 188}, {141, 219, 219}, {207, 190, 23}, {229, 218, 158}};
cv::Scalar Shape::render_color (RenderOptions const &opt) const {
if (opt.use_tag) {
return cv::Scalar(tag, tag, tag);
}
if (opt.use_serial) {
return cv::Scalar(serial, serial, serial);
}
if (opt.use_palette) {
return PALETTE[rand() % PALETTE.size()];
}
else {
return color;
}
}
class Point: public Shape {
public:
Point (json const &geo, cv::Size sz): Shape("point") {
float x = geo.at("x").get<float>() * sz.width;
float y = geo.at("y").get<float>() * sz.height;
controls.emplace_back(x, y);
}
virtual std::unique_ptr<Shape> clone () const {
return std::unique_ptr<Shape>(new Point(*this));
}
virtual void render (cv::Mat *m, RenderOptions const &opt) const {
cv::circle(*m, round(controls[0]), opt.point_radius, render_color(opt), opt.thickness, opt.line_type, opt.shift);
}
};
class Rectangle: public Shape {
public:
Rectangle (json const &geo, cv::Size sz): Shape("rect") {
float x = geo.at("x").get<float>() * sz.width;
float y = geo.at("y").get<float>() * sz.height;
float w = geo.at("width").get<float>() * sz.width;
float h = geo.at("height").get<float>() * sz.height;
controls.emplace_back(x, y);
controls.emplace_back(x+w, y+h);
controls.emplace_back(x+w, y);
controls.emplace_back(x, y + h);
}
virtual std::unique_ptr<Shape> clone () const {
return std::unique_ptr<Shape>(new Rectangle(*this));
}
virtual void render (cv::Mat *m, RenderOptions const &opt) const {
cv::rectangle(*m, round(controls[0]), round(controls[1]), render_color(opt), opt.thickness, opt.line_type, opt.shift);
}
virtual void transform (std::function<void(vector<cv::Point2f> *)> f) {
// some shape might need pre-post processing
f(&controls);
float area = cv::norm(controls[2] - controls[0]) * cv::norm(controls[3] - controls[0]);
float min_x = controls[0].x;
float max_x = controls[0].x;
float min_y = controls[0].y;
float max_y = controls[0].y;
for (int i = 1; i < 4; ++i) {
min_x = std::min(min_x, controls[i].x);
max_x = std::max(max_x, controls[i].x);
min_y = std::min(min_y, controls[i].y);
max_y = std::max(max_y, controls[i].y);
}
float mid_x = (min_x + max_x) / 2;
float span_x = (max_x - min_x);
float mid_y = (min_y + max_y) / 2;
float span_y = (max_y - min_y);
float rate = std::sqrt((area + 1.0) / (span_x * span_y + 1.0));
float d_x = span_x * rate / 2;
float d_y = span_y * rate / 2;
min_x = mid_x - d_x;
max_x = mid_x + d_x;
min_y = mid_y - d_y;
max_y = mid_y + d_y;
controls[0].x = min_x;
controls[0].y = min_y;
controls[1].x = max_x;
controls[1].y = max_y;
controls[2].x = max_x;
controls[2].y = min_y;
controls[3].x = min_x;
controls[3].y = max_y;
}
#if 0
virtual void dump (Json *json) const {
Json::object obj{
{"type", type()},
{"geometry", Json::object{
{"x", rect.x},
{"y", rect.y},
{"width", rect.width},
{"height", rect.height}
}}
};
check_add_label(this, &obj);
*json = obj;
}
virtual void bbox (cv::Rect_<float> *bb) const {
*bb = rect;
}
/*
virtual void zoom (cv::Rect_<float> const &bb) {
rect.x -= bb.x;
rect.y -= bb.y;
rect.x /= bb.width;
rect.width /= bb.width;
rect.y /= bb.height;
rect.height /= bb.height;
}
*/
#endif
};
class Ellipse: public Rectangle {
public:
Ellipse (json const &geo, cv::Size sz): Rectangle(geo, sz) {
type = "ellipse";
}
virtual std::unique_ptr<Shape> clone () const {
return std::unique_ptr<Shape>(new Ellipse(*this));
}
virtual void render (cv::Mat *m, RenderOptions const &opt) const {
CHECK(false); // << "TODO";
/*
cv::Point2f center(m->cols * (rect.x + rect.width/2),
m->rows * (rect.y + rect.height/2));
cv::Size2f size(m->cols * rect.width, m->rows * rect.height);
cv::ellipse(*m, cv::RotatedRect(center, size, 0), v, thickness);
*/
}
};
class Polygon: public Shape {
public:
Polygon (json const &geo, cv::Size sz): Shape("polygon") {
for (auto const &p: geo.at("points")) {
controls.emplace_back(p.at("x").get<float>() * sz.width, p.at("y").get<float>() * sz.height);
}
}
virtual std::unique_ptr<Shape> clone () const {
return std::unique_ptr<Shape>(new Polygon(*this));
}
virtual void render (cv::Mat *m, RenderOptions const &opt) const {
vector<cv::Point> ps;
ps.reserve(controls.size());
for (auto const &p: controls) {
ps.push_back(round(p));
}
cv::Point const *pps = &ps[0];
int const nps = ps.size();
if (opt.thickness == cv::FILLED) {
cv::fillPoly(*m, &pps, &nps, 1, render_color(opt), opt.line_type, opt.shift);
}
else {
cv::polylines(*m, &pps, &nps, 1, true, render_color(opt), opt.thickness, opt.line_type, opt.shift);
}
}
#if 0
virtual void dump (Json *json) const {
vector<Json> pts;
for (auto const &p: points) {
pts.emplace_back(Json::object{{"x", p.x}, {"y", p.y}});
}
Json::object obj{
{"type", type()},
{"geometry", Json::object{{"points", std::move(pts)}}}
};
check_add_label(this, &obj);
*json = obj;
}
virtual void draw (cv::Mat *m, cv::Scalar v, int thickness) const {
}
virtual void bbox (cv::Rect_<float> *bb) const {
float min_x = 1, min_y = 1;
float max_x = 0, max_y = 0;
for (auto const &p: points) {
if (p.x < min_x) min_x = p.x;
if (p.y < min_y) min_y = p.y;
if (p.x > max_x) max_x = p.x;
if (p.y > max_y) max_y = p.y;
}
float area = (max_x - min_x) * (max_y - min_y);
if (area <= 0) {
*bb = cv::Rect_<float>();
}
else {
bb->x = min_x;
bb->y = min_y;
bb->width = max_x - min_x;
bb->height = max_y - min_y;
}
}
virtual void zoom (cv::Rect_<float> const &bb) {
for (auto &p: points) {
p.x -= bb.x;
p.y -= bb.y;
p.x /= bb.width;
p.y /= bb.height;
}
}
#endif
};
class Polygons: public Shape {
vector<unsigned> sizes;
public:
Polygons (json const &geo, cv::Size sz): Shape("polygons") {
for (auto const &polygon: geo.at("points")) {
// array of array
unsigned cc = 0;
for (auto const &p: polygon) {
controls.emplace_back(p.at("x").get<float>() * sz.width, p.at("y").get<float>() * sz.height);
cc += 1;
}
sizes.push_back(cc);
}
}
virtual std::unique_ptr<Shape> clone () const {
return std::unique_ptr<Shape>(new Polygons(*this));
}
virtual void render (cv::Mat *m, RenderOptions const &opt) const {
vector<cv::Point> ps;
ps.reserve(controls.size());
unsigned off = 0;
for (unsigned sz: sizes) {
ps.clear();
for (unsigned i = 0; i < sz; ++i, ++off) {
ps.push_back(round(controls[off]));
}
cv::Point const *pps = &ps[0];
int const nps = ps.size();
if (opt.thickness == cv::FILLED) {
cv::fillPoly(*m, &pps, &nps, 1, render_color(opt), opt.line_type, opt.shift);
}
else {
cv::polylines(*m, &pps, &nps, 1, true, render_color(opt), opt.thickness, opt.line_type, opt.shift);
}
}
}
};
class RotatedRectangle: public Shape {
public:
RotatedRectangle (json const &geo, cv::Size sz): Shape("arect") {
float x = geo.at("x").get<float>();
float y = geo.at("y").get<float>();
float w = geo.at("w").get<float>();
float h = geo.at("h").get<float>();
float a = geo.at("a").get<float>();
cv::Point2f p0(x, y);
cv::Point2f p_x(w/2, 0);
cv::Point2f p_y(0, h/2);
float cosv = std::cos(a);
float sinv = std::sin(a);
cv::Matx<float, 2, 2> rot(cosv, -sinv, sinv, cosv);
p_x = rot * p_x;
p_y = rot * p_y;
controls.emplace_back(p0 - p_x);
controls.emplace_back(p0 + p_x);
controls.emplace_back(p0 - p_y);
controls.emplace_back(p0 + p_y);
}
virtual std::unique_ptr<Shape> clone () const {
return std::unique_ptr<Shape>(new RotatedRectangle(*this));
}
virtual void render (cv::Mat *m, RenderOptions const &opt) const {
/*
cv::Point2f top(controls[0]);
cv::Point2f bottom(controls[1]);
cv::Point2f left(controls[2]);
cv::Point2f right(controls[3]);
*/
cv::Point2f cc = controls[0];
cc += controls[1];
cc += controls[2];
cc += controls[3];
cc *= 1.0f/4.0f;
cv::Point2f d1 = controls[1] - cc;
cv::Point2f d2 = controls[3] - cc;
cv::Point vertices[4] = {
round(cc - d1 - d2),
round(cc - d1 + d2),
round(cc + d1 + d2),
round(cc + d1 - d2)
};
cv::Point const *pps = &vertices[0];
int const nps = 4;
if (opt.thickness == cv::FILLED) {
cv::fillPoly(*m, &pps, &nps, 1, render_color(opt), opt.line_type, opt.shift);
}
else {
cv::polylines(*m, &pps, &nps, 1, true, render_color(opt), opt.thickness, opt.line_type, opt.shift);
}
CHECK(false);
}
virtual void transform (std::function<void(vector<cv::Point2f> *)> f) {
f(&controls);
}
};
std::unique_ptr<Shape> Shape::create (json const &spec, cv::Size sz) {
string type = spec.at("type").get<string>();
auto geo = spec.at("geometry");
std::unique_ptr<Shape> shape;
if (type == "rect") {
shape = std::unique_ptr<Shape>(new Rectangle(geo, sz));
}
else if (type == "polygon") {
shape = std::unique_ptr<Shape>(new Polygon(geo, sz));
}
else if (type == "polygons") {
shape = std::unique_ptr<Shape>(new Polygons(geo, sz));
}
else if (type == "ellipse") {
shape = std::unique_ptr<Shape>(new Ellipse(geo, sz));
}
else if (type == "point") {
shape = std::unique_ptr<Shape>(new Point(geo, sz));
}
else if (type == "arect") {
shape = std::unique_ptr<Shape>(new RotatedRectangle(geo, sz));
}
else {
logging::error("Unknown shape {}.", type);
CHECK(0); // << "unknown shape: " << type;
}
auto plabel = spec.find("label");
if (plabel != spec.end()) { // has label
// might be a single number or an array
if (plabel->is_array()) {
cv::Scalar color(0,0,0,0);
int i = 0;
for (auto it = plabel->begin(); it != plabel->end(); ++it) {
color[i] = it->get<float>();
++i;
}
shape->color = color;
}
else {
float v = plabel->get<float>();
shape->color = cv::Scalar(v, v, v, v);
}
}
auto ptag = spec.find("tag");
if (ptag != spec.end()) { // has label
shape->tag = ptag->get<float>();
}
return shape;
}
}