forked from lucasw/vimjay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.cpp
More file actions
632 lines (492 loc) · 15.7 KB
/
Copy pathfilter.cpp
File metadata and controls
632 lines (492 loc) · 15.7 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
Copyright 2012 Lucas Walter
This file is part of Vimjay.
Vimjay is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Vimjay is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Vimjay. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filter.h"
#include <boost/lexical_cast.hpp>
#include <opencv2/video/tracking.hpp>
#include <glog/logging.h>
#if 0
// TDB need CV_MAJOR_VERSION to establish this is available
#include <opencv2/videostab/videostab.hpp>
#endif
// filter Node objects
namespace bm {
FilterFIR::FilterFIR(const std::string name) : Buffer(name)
{
}
void FilterFIR::setup(const std::vector<float> new_xi)
{
xi = new_xi;
setSignal("max_size", new_xi.size());
}
bool FilterFIR::update()
{
bool rv = Node::update();
if (!rv) return false;
if (!isDirty(this,22)) { return true;}
manualUpdate();
cv::Mat out;
int addnum = 0;
for (int i = 0; i < ports.size(); i++) {
if (ports[i]->type != SIGNAL) continue;
const std::string port = ports[i]->name;
if (port.substr(0,2) != "xi") continue;
float val = getSignal(port);
cv::Mat tmp = frames[frames.size() - addnum - 1];
if (addnum == 0)
out = tmp * val;
else {
if (val > 0) {
out += tmp * val;
} else
out -= tmp * -val;
}
addnum++;
if (addnum >= frames.size()) break;
}
setImage("out", out);
return true;
}
bool FilterFIR::handleKey(int key)
{
bool valid_key = Buffer::handleKey(key);
if (valid_key) return true;
valid_key = true;
if (key == '[') {
// add an input addition port, TBD move to function
int add_num = 0;
for (int i = 0; i < ports.size(); i++) {
if (ports[i]->type != SIGNAL) continue;
const std::string port = ports[i]->name;
if (port.substr(0,2) != "xi") {
VLOG(1) << name << " : " << port.substr(0,2) << " " << port;
continue;
}
add_num++;
}
setSignal("max_size", add_num+1);
// add a new addition port
const std::string port = "xi" + boost::lexical_cast<std::string>(add_num);
setInputPort(SIGNAL, port, NULL, "value"); // this allows other signals to connect to replace nf
// TBD make a way to delete a port
} else {
valid_key = false;
}
// TBD
if (valid_key) setDirty();
return valid_key;
}
Sobel::Sobel(const std::string name) : ImageNode(name)
{
cv::Mat tmp;
setImage("in", tmp);
setSignal("xorder",1);
setSignal("yorder",0);
setSignal("ksize",3);
setSignal("scale",0.8);
setSignal("delta",128);
}
bool Sobel::update()
{
//if (!ImageNode::update()) return false;
if (!Node::update()) return false;
cv::Mat in = getImage("in");
if (in.empty()) {
VLOG(2) << name << " in is empty";
return false;
}
int ksize = getSignal("ksize");
int xorder = getSignal("xorder");
int yorder = getSignal("yorder");
float scale = getSignal("scale");
if (ksize < 1) ksize = 1;
if (ksize > 4) ksize = 4;
if (xorder < 0) xorder = 0;
if (xorder > 4) xorder = 4;
if (yorder < 0) yorder = 0;
if (yorder > 4) yorder = 4;
if (scale < 0.001) scale = 0.001;
if (xorder + yorder < 1) xorder = 1;
setSignal("ksize", ksize);
setSignal("xorder", xorder);
setSignal("yorder", yorder);
setSignal("scale", scale);
int real_ksize = 1;
if (ksize == 2) real_ksize = 3;
if (ksize == 3) real_ksize = 5;
if (ksize == 4) real_ksize = 7;
cv::Mat out;
cv::Sobel(in, out, in.depth(),
xorder,
yorder,
real_ksize,
scale,
getSignal("delta")
);
setImage("out", out);
return true;
}
Laplacian::Laplacian(const std::string name) : ImageNode(name)
{
cv::Mat tmp;
setImage("in", tmp);
// TBD Signals should allo min max parameters to be set
setSignal("ksize", 1);
// scale and offset
setSignal("scale",1.0);
setSignal("delta",128);
}
bool Laplacian::update()
{
const bool rv = Node::update();
if (!rv) return false;
if (!isDirty(this, 5)) {
VLOG(1) << name << " not dirty ";
return true;
}
cv::Mat in = getImage("in");
if (in.empty()) return true;
int ksize = getSignal("ksize");
if (ksize < 0) { ksize = 0; setSignal("ksize", ksize); }
if (ksize > 15) { ksize = 15; setSignal("ksize", ksize); }
int real_ksize = ksize*2 + 1;
cv::Mat out;
cv::Laplacian(in, out, in.depth(),
real_ksize,
getSignal("scale"),
getSignal("delta"),
getBorderType(true)
);
setImage("out", out);
return true;
}
////////////////////////////////////////
GaussianBlur::GaussianBlur(const std::string name) : ImageNode(name)
{
cv::Mat tmp;
setImage("in", tmp);
setSignal("k_width",2);
setSignal("k_height",2);
vcol = cv::Scalar(200, 200, 50);
}
bool GaussianBlur::update()
{
if (!Node::update()) return false;
if (!isDirty(this, 5)) {
VLOG(1) << name << " not dirty ";
return true;
}
cv::Mat in = getImage("in");
if (in.empty()) return false;
int k_width = abs(getSignal("k_width"));
int k_height = abs(getSignal("k_height"));
cv::Size ksize = cv::Size(k_width*2 + 1, k_height*2+1);
cv::Mat out = cv::Mat(in.size(), in.type());
cv::GaussianBlur(in, out, ksize, 0, 0, cv::BORDER_REFLECT);
setImage("out", out);
return true;
}
////////////////////////////////////////
MedianBlur::MedianBlur(const std::string name) : ImageNode(name)
{
cv::Mat tmp;
setImage("in", tmp);
setSignal("k_size", 2, false, SATURATE, 1, 10);
vcol = cv::Scalar(200, 200, 50);
}
bool MedianBlur::update()
{
if (!Node::update()) return false;
if (!isDirty(this, 5)) {
VLOG(1) << name << " not dirty ";
return true;
}
cv::Mat in = getImage("in");
if (in.empty()) return false;
int k = getSignal("k_size");
int ksize = k * 2 + 1;
cv::Mat out = cv::Mat(in.size(), in.type());
cv::medianBlur(in, out, ksize);
setImage("out", out);
return true;
}
BilateralFilter::BilateralFilter(const std::string name) : ImageNode(name)
{
cv::Mat tmp;
setImage("in", tmp);
setSignal("d", 2, false, SATURATE, -1, 10);
setSignal("sigma_color", 10);
setSignal("sigma_space", 10);
setSignal("border", 0, false, ROLL, 0, 4);
vcol = cv::Scalar(200, 200, 50);
}
bool BilateralFilter::update()
{
if (!Node::update()) return false;
if (!isDirty(this, 5)) {
VLOG(1) << name << " not dirty ";
return true;
}
cv::Mat in = getImage("in");
if (in.empty()) return false;
cv::Mat in_3 = cv::Mat(in.size(), CV_8UC3, cv::Scalar(0));
// just calling reshape(4) doesn't do the channel reassignment like this does
int ch[] = {0,0, 1,1, 2,2};
cv::mixChannels(&in, 1, &in_3, 1, ch, 3 );
const float sigma_color = getSignal("sigma_color");
const float sigma_space = getSignal("sigma_space");
const int d = getSignal("d");
cv::Mat out_3;
cv::bilateralFilter(in_3, out_3, d, sigma_color, sigma_space, getBorderType());
cv::Mat out = cv::Mat(out_3.size(), CV_8UC4, cv::Scalar(0));
cv::mixChannels(&out_3, 1, &out, 1, ch, 3 );
setImage("out", out);
return true;
}
#if 0
InPaint::InPaint(const std::string name) : ImageNode(name)
{
cv::Mat tmp;
setImage("in", tmp);
setImage("mask", tmp);
setSignal("radius", 10);
setSignal("mode", 0, false, ROLL, 0, 1);
}
bool InPaint::update()
{
if (!Node::update()) return false;
if (!isDirty(this, 5)) {
VLOG(1) << name << " not dirty ";
return true;
}
cv::Mat in = getImage("in");
if (in.empty()) return false;
cv::Mat in_3 = cv::Mat(in.size(), CV_8UC3, cv::Scalar(0));
// just calling reshape(4) doesn't do the channel reassignment like this does
int ch[] = {0,0, 1,1, 2,2};
cv::mixChannels(&in, 1, &in_3, 1, ch, 3 );
cv::Mat mask = getImage("mask");
if (mask.empty()) return false;
cv::Mat mask_1 = cv::Mat(in.size(), CV_8UC1, cv::Scalar(0));
int ch1[] = {0, 0};
mixChannels(&mask, 1, &mask_1, 1, ch1, 1);
int mode_ind = getSignal("mode");
int mode = cv::INPAINT_NS;
if (mode_ind == 1) {
mode = cv::INPAINT_TELEA;
}
cv::Mat out_3;
cv::inpaint(in_3, mask_1, out_3, getSignal("radius"), mode);
{
cv::Mat out = cv::Mat(out_3.size(), CV_8UC4, cv::Scalar(0,0,0,0));
int ch[] = {0,0, 1,1, 2,2};
mixChannels(&out_3, 1, &out, 1, ch, 3);
setImage("out", out);
}
return true;
}
#endif
/////////////////////////////////////////////////////////////////////////////
MorphologyEx::MorphologyEx(const std::string name) : ImageNode(name)
{
cv::Mat in;
setImage("in", in);
setSignal("element", 0, false, ROLL, 0, 2);
setSignal("element_size_x", 3, false, SATURATE, 1, 10);
setSignal("element_size_y", 3, false, SATURATE, 1, 10);
setSignal("op", 0, false, ROLL, 0, 6);
setSignal("iterations", 1, false, SATURATE, 1, 10);
// TBD could allow element image input, which would be shrunk
// down to element size to be used as the morph element
}
bool MorphologyEx::update()
{
if (!Node::update()) return false;
if (!isDirty(this, 5)) {
VLOG(4) << name << " not dirty ";
return true;
}
cv::Mat in = getImage("in");
if (in.empty()) return false;
int element_shape_ind = getSignal("element");
int element_shape = cv::MORPH_RECT;
if (element_shape_ind == 1) element_shape = cv::MORPH_CROSS;
if (element_shape_ind == 2) element_shape = cv::MORPH_ELLIPSE;
int element_size_x = getSignal("element_size_x");
int element_size_y = getSignal("element_size_y");
int op_ind = getSignal("op");
// TBD should the default do nothing?
int op = cv::MORPH_OPEN;
if (op_ind == 1) op = cv::MORPH_CLOSE;
else if (op_ind == 2) op = cv::MORPH_GRADIENT;
else if (op_ind == 3) op = cv::MORPH_BLACKHAT;
else if (op_ind == 4) op = cv::MORPH_TOPHAT;
else if (op_ind == 5) op = cv::MORPH_DILATE;
else if (op_ind == 6) op = cv::MORPH_ERODE;
int iterations = getSignal("iterations");
cv::Mat kernel = cv::getStructuringElement(
element_shape,
cv::Size(element_size_x*2+1, element_size_y*2+1),
cv::Point(element_size_x, element_size_y) );
cv::Mat out;
//if (op_ind <= 6)
{
cv::morphologyEx(in, out,
op,
kernel,
cv::Point(-1, -1),
iterations,
getBorderType()
);
}
#if 0
// these are identical to passing in MORPH_DILATE/ERODE
else if (op_ind == 7) {
cv::dilate(in, out, kernel, cv::Point(-1,-1), iterations, getBorderType());
} else if (op_ind == 8) {
cv::erode(in, out, kernel, cv::Point(-1,-1), iterations, getBorderType());
}
#endif
setImage("out", out);
}
/////////////////////////////////////////////////////////////////////////////
OpticalFlow::OpticalFlow(const std::string name) : Remap(name)
{
cv::Mat tmp, tmp2, tmp3, tmp4;
//setImage("prev", tmp);
setImage("next", tmp2);
//setImage("flowx", tmp3);
//setImage("flowy", tmp4);
setSignal("pyr_scale",0.5);
setSignal("levels",1);
setSignal("winsize",16);
setSignal("iterations",2);
setSignal("poly_n",5);
setSignal("poly_sigma",1.1);
setSignal("mode",0, false, ROLL, 0, 3);
setSignal("scale",1.0);
setSignal("offset",128);
setSignal("interp", 0.5);
setSignal("interp_mode",0, false, ROLL, 0, 2);
}
bool OpticalFlow::update()
{
if (!Node::update()) return false;
if (!isDirty(this, 5)) {
VLOG(5) << name << " not dirty ";
return true;
}
float pyr_scale = getSignal("pyr_scale");
// TBD add min/max and type to setSignal()
if (pyr_scale < 0.1) pyr_scale = 0.1;
if (pyr_scale > 0.9) pyr_scale = 0.9;
setSignal("pyr_scale", pyr_scale);
int levels = getSignal("levels");
if (levels < 1) levels = 1;
setSignal("levels", levels);
int winsize = getSignal("winsize");
if (winsize < 4) winsize = 4;
setSignal("winsize", winsize);
int iterations = getSignal("iterations");
if (iterations < 1) iterations = 1;
setSignal("iterations", iterations);
int poly_n = getSignal("poly_n");
if (poly_n < 2) poly_n = 2;
setSignal("poly_n", poly_n);
float poly_sigma = getSignal("poly_sigma");
int mode = getSignal("mode");
int flow_mode = 0;
if (mode == 1) flow_mode = cv::OPTFLOW_USE_INITIAL_FLOW;
if (mode == 2) flow_mode = cv::OPTFLOW_USE_INITIAL_FLOW & cv::OPTFLOW_FARNEBACK_GAUSSIAN;
if (mode == 3) flow_mode = cv::OPTFLOW_FARNEBACK_GAUSSIAN;
// clear previous setSignal dirtiness
isDirty(this, 5);
cv::Mat prev = getImage("in");
cv::Mat next = getImage("next");
if (prev.empty() || next.empty()) return true;
cv::Mat prevm, nextm;
cv::cvtColor(prev, prevm, CV_BGR2GRAY);
cv::cvtColor(next, nextm, CV_BGR2GRAY);
cv::calcOpticalFlowFarneback(prevm, nextm, flow,
pyr_scale, levels, winsize,
iterations, poly_n, poly_sigma, flow_mode);
cv::Mat flow8_2;
flow.convertTo(flow8_2, CV_8UC2, getSignal("scale"), getSignal("offset"));
// TBD use scalex and scaley to separately scale those?
{
cv::Mat offx = cv::Mat(flow8_2.size(), CV_8UC4, cv::Scalar(0));
int ch[] = {0,0, 0,1, 0,2};
mixChannels(&flow8_2, 1, &offx, 1, ch, 3);
setImage("offx", offx);
}
{
cv::Mat offy = cv::Mat(flow8_2.size(), CV_8UC4, cv::Scalar(0));
int ch[] = {1,0, 1,1, 1,2};
mixChannels(&flow8_2, 1, &offy, 1, ch, 3);
setImage("offy", offy);
}
/// Do interpolation, should make optional
{
const float interp = getSignal("interp");
const int interp_mode = getSignal("interp_mode");
cv::Mat out_forward, out_reverse, out;
/*
oflow
prev(x,y) ~= next(x + flow_x(x,y), y + flow_y(x,y))
remap
i dst(x,y) = src(map_x(x,y), map_y(x,y))
*/
if ((interp_mode == 0) || (interp_mode == 2)) {
cv::remap(prev, out_forward, base_xy - flow*interp, cv::Mat(), cv::INTER_NEAREST, cv::BORDER_REPLICATE);
}
if ((interp_mode == 1) || (interp_mode == 2)) {
//cv::Mat flow_reverse;
cv::calcOpticalFlowFarneback(nextm, prevm, flow_reverse,
pyr_scale, levels, winsize,
iterations, poly_n, poly_sigma, flow_mode);
cv::remap(next, out_reverse, base_xy - flow_reverse*(1.0 - interp), cv::Mat(), cv::INTER_NEAREST, cv::BORDER_REPLICATE);
}
cv::Mat test;
if (interp_mode == 0) {
out = out_forward;
test = prev.clone();
}
else if (interp_mode == 1) {
out = out_reverse;
test = next.clone();
}
else if (interp_mode == 2) {
out = out_forward * (1.0 - interp) + out_reverse * interp;
test = prev/2 + next/2;
}
setImage("out", out);
// TBD temp/optional
// make test image
for (int i = 0; i < flow.rows; i += 14) {
for (int j = 0; j < flow.cols; j += 14) {
float dx = flow.at<cv::Point2f>(i,j).x;
float dy = flow.at<cv::Point2f>(i,j).y;
if ((dx > 2.0) || (dy > 2.0)) {
cv::Point2f p1 = cv::Point2f(j, i);
cv::Point2f p2 = cv::Point2f((float)j + dx, (float)i + dy);
cv::line(test, p1, p2, cv::Scalar(0, 0, 0, 0), 1);
cv::circle(test, p1, 2, cv::Scalar(0,0,255,0));
cv::circle(test, p2, 2, cv::Scalar(0,255,0,0));
}
}}
setImage("test", test);
}
return true;
}
} // namespace bm