-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdemo_opencv.cpp
2279 lines (1953 loc) · 65.7 KB
/
demo_opencv.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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************
This file is part of the C++ library "homog2d", dedicated to
handle 2D lines and points, see https://github.com/skramm/homog2d
Author & Copyright 2019-2025 Sebastien Kramm
Contact: [email protected]
Licence: MPL v2
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
**************************************************************************/
/**
\file demo_opencv.cpp
\brief demo of interfacing with Opencv.
Try it with <code>$ make demo</code>.
(requires OpenCv)
To run a specific demo, give its number as first argument, i.e.:
<code>$ BUILD/demo_opencv 4</code>.
Each demo is defined by three code blocks:
- a class definition, that inherits from class \c Data (usually named \c Params_something), and that may hold some specific data for the demo.
- a function \c demo_something( int ), that will be run by this program, it must be added in the v_demo vector in main() function.
- a callback function \c action_something( void* ), that may/will be called either by the mouse callback, either after each (valid) keyboard hit.
Its argument will be
To enable the mouse callback, the function \c demo_something( int ) must first instanciate the \c Params_something class
then assign the \c action_something() function to the mouse callback:
\code
void demo_something( int demo_index)
{
Param_something data( demo_index, "what-is-this-about" );
action_something( &data ); // initial call of the "action" stuff
data.setMouseCB( action_something ) // assign to mouse callback
...
}
\endcode
*/
//#define HOMOG2D_PRELIMINAR
#define HOMOG2D_USE_OPENCV
#define HOMOG2D_ENABLE_VRTP
//#define HOMOG2D_DEBUGMODE
#include "homog2d.hpp"
// additional Opencv header, needed for GUI stuff
#include "opencv2/highgui.hpp"
using namespace h2d;
//using namespace h2d::img;
// forward declaration
void myMouseCB( int event, int x, int y, int, void* param );
//------------------------------------------------------------------
/// General data struct used in demos, is inherited in each demo
struct Data
{
friend void myMouseCB( int, int, int, int, void* );
img::Image<cv::Mat> img;
int _imWidth = 700;
int _imHeight = 500;
std::string win1; ///< Window name (appears in window title bar)
int _selected = -1; ///< Mouse selected point
std::vector<Point2d> vpt; ///< some points used in demo
bool leftClicAddPoint = false;
int _lineIndex = 0; ///< used to draw some lines of text inside window
int _demo_idx = -1; ///< index of current demo
Point2d _pt_mouse; ///< Mouse coordinates
CPolyline _cpoly; ///< will be saved in SVG file
private:
std::function<void(void*)> _mouseCB = nullptr;
public:
explicit Data( int demidx, std::string wname, std::string helpText=std::string() )
: _demo_idx( demidx )
{
if( helpText.empty() )
helpText = wname;
std::cout << "Demo " << demidx << ": " << helpText << "\n press 'h' anytime for available keys\n";
win1 = std::string("Demo ") + std::to_string(demidx) + ": " + wname;
cv::destroyAllWindows();
cv::namedWindow( win1 );
img.setSize( _imWidth, _imHeight );
img.clear(255);
vpt.resize(4);
_pt_mouse.set(10,10); // just to avoid it being 0,0
reset();
}
void reset()
{
vpt[0] = Point2d(100,200);
vpt[1] = Point2d(200,300);
vpt[2] = Point2d(150,50);
vpt[3] = Point2d(300,250);
}
void setMousePos(int x, int y)
{
_pt_mouse.set(x,y);
}
void setMouseCB( std::function<void(void*)> cb )
{
_mouseCB = cb;
cv::setMouseCallback( win1, myMouseCB, this );
}
void addMousePoint()
{
vpt.push_back( _pt_mouse );
}
int nbPts() const
{
return (int)vpt.size();
}
void clearImage()
{
img.clear();
_lineIndex = 0;
}
void showImage()
{
// cv::Mat dst;
// cv::flip(img.getReal(), dst, 0 );
img.show( win1 );
// cv::imshow( win1, dst );
}
void putTextLine( std::string msg, int lineindex=-1 )
{
int lineSize = 22;
if( lineindex == 0 )
_lineIndex = 0;
cv::putText( img.getReal(), msg, cv::Point2i( 20, lineSize * ++_lineIndex), 0, 0.6, cv::Scalar( 150,0,0 ), 1 );
}
std::string save_SVG( int idx ) const;
void drawLines()
{
for( int i=0; i<nbPts(); i++ )
{
if( _selected == i )
vpt[i].draw( img, img::DrawParams().setColor( 250, 0, 150).setPointStyle( (img::PtStyle)i).selectPoint() );
else
vpt[i].draw( img, img::DrawParams().setPointStyle((img::PtStyle)i) );
}
Line2d lA( vpt[0], vpt[2] );
Line2d lB( vpt[0], vpt[3] );
Line2d lC( vpt[1], vpt[2] );
Line2d lD( vpt[1], vpt[3] );
img::DrawParams dp;
dp.setColor( 250, 50, 100) ;
lA.draw( img, dp );
lB.draw( img, dp );
lC.draw( img, dp );
lD.draw( img, dp );
auto rect = getBB( vpt );
rect.draw( img, img::DrawParams().setColor(0,250,0) );
auto cbb = rect.getBoundingCircle();
cbb.draw( img, img::DrawParams().setColor( 0,0,250) );
}
};
/// Saves the \c CPolyline in a file
/**
\return file name
*/
std::string Data::save_SVG( int demo_idx ) const
{
static int num;
static int previous_demo_idx;
if( previous_demo_idx != demo_idx )
num = 0;
previous_demo_idx = demo_idx;
img::Image<img::SvgImage> im( _imWidth, _imHeight );
_cpoly.draw( im );
std::ostringstream oss;
oss << "BUILD/demo_pol_" << demo_idx << "_" << num++ << ".svg";
im.write( oss.str() );
return oss.str();
}
//------------------------------------------------------------------
/// Mouse callback functions, checks if one of the points is selected.
/**
- If so, that point gets moved by the mouse, and the function \c action is called (if it has been assigned)
*/
void
myMouseCB( int event, int x, int y, int, void* param )
{
auto& data = *reinterpret_cast<Data*>(param);
data.setMousePos(x,y);
bool doSomething = true;
switch( event )
{
case cv::EVENT_LBUTTONUP:
data._selected = -1;
break;
case cv::EVENT_LBUTTONDOWN:
data._selected = -1;
for( int i=0; i<data.nbPts(); i++ )
if( data._pt_mouse.distTo( data.vpt[i]) < 10 ) // if mouse is less than 10 pixel away
{
data._selected = i;
data.vpt[i].draw( data.img, img::DrawParams().setPointStyle(img::PtStyle::Diam) );
}
if( data._selected == -1 )
if( data.leftClicAddPoint )
data.addMousePoint();
break;
case cv::EVENT_MOUSEMOVE:
if( data._selected != -1 )
{
data.vpt[data._selected] = data._pt_mouse;
data.vpt[data._selected].draw( data.img, img::DrawParams().selectPoint() );
}
break;
case cv::EVENT_RBUTTONDOWN:
data._selected = -1;
for( int i=0; i<data.nbPts(); i++ )
if( data._pt_mouse.distTo( data.vpt[i]) < 10 ) // if mouse is less than 10 pixel away
data._selected = i;
if( data._selected != -1 )
{
data.vpt.erase( std::begin(data.vpt) + data._selected );
data._selected = -1;
}
break;
default: doSomething = false; break;
}
if( doSomething )
{
data.clearImage();
if( data._mouseCB )
{
data._mouseCB( param );
}
data.showImage();
}
}
//------------------------------------------------------------------
/// Generic Keyboard loop, build on top of Opencv's \c cv::waitKey(0)
struct KeyboardLoop
{
using FuncType = void(void*);
/// Holds link between key, function, and description
struct KbLoopAction
{
char _key; ///< what key
std::function<FuncType> _action; ///< what do do
std::string _msg; ///< message describing key action
friend std::ostream& operator << ( std::ostream& f, const KbLoopAction& action )
{
if( !action._msg.empty() )
f << " -" << action._key << ": " << action._msg << '\n';
return f;
}
};
private:
std::vector<KbLoopAction> _actions;
std::function<FuncType> _common = nullptr;
int _index = 0;
public:
/// Call this to add a new action to a given key
void addKeyAction(
char key, ///< the key
const std::function<FuncType>& action, ///< the callback function, called on key hit
std::string text=std::string() ///< the message that will be printed
)
{
auto it = std::find_if(
_actions.begin(),
_actions.end(),
[&]
( const KbLoopAction& elem )
{
return key == elem._key;
}
);
if( it != _actions.end() )
throw std::runtime_error( std::string("Error, key '") + key + "' already registered" );
_actions.push_back( KbLoopAction{ key, action, text } );
}
/// Add common \c action when a valid key is hit
void addCommonAction( const std::function<FuncType>& action )
{
_common = action;
}
void showAvailableKeys() const
{
if( !_actions.empty() )
{
std::cout << "Available keys: " << _actions.size() << '\n';
for( const auto& elem: _actions )
std::cout << elem;
}
else
std::cout << "No user keys defined, but 'h' (help), ESC (quit) and SPC (switch to next) available\n";
}
void start( Data& data );
};
void KeyboardLoop::start( Data& data )
{
showAvailableKeys();
char key=0;
do
{
switch( key = cv::waitKey(0) )
{
case 27:
std::cout << "ESC => terminate\n";
std::exit(0);
case 32:
std::cout << "SPC: switch to next\n";
return;
case 'H':
case 'h':
showAvailableKeys();
break;
case 's':
{
auto fn = data.save_SVG( data._demo_idx );
std::cout << "Saved Polyline to file '" << fn << "'\n";
break;
}
default:
{
auto it = std::find_if(
_actions.begin(),
_actions.end(),
[&]
( const KbLoopAction& elem )
{
return key == elem._key;
}
);
if( it != _actions.end() )
{
std::cout << "Action " << _index++;
if( !it->_msg.empty() )
std::cout << ": " << it->_msg;
std::cout << '\n';
it->_action(&data);
if( _common )
_common(&data);
data.showImage();
}
}
}
}
while( key != 32 ); // SPC
}
//------------------------------------------------------------------
void toggle( bool& b, std::string msg )
{
b = !b;
std::cout << "toogle " << msg << "=" << b << '\n';
}
//------------------------------------------------------------------
void action_1( void* param )
{
auto& data = *reinterpret_cast<Data*>(param);
data.drawLines();
Line2d l_mouse = data._pt_mouse * Point2d();
auto p_lines = l_mouse.getParallelLines( 30 );
auto ppts = l_mouse.getPoints( Point2d(), 200 );
Line2d l_mouse_o = l_mouse.getOrthogLine( ppts.second );
l_mouse.draw( data.img );
l_mouse_o.draw( data.img );
auto p_lines_o = l_mouse_o.getParallelLines( 10 );
img::DrawParams dp;
dp.setColor(100,250,100);
p_lines.first.draw( data.img, dp );
p_lines.second.draw( data.img, dp );
p_lines_o.first.draw( data.img, dp );
p_lines_o.second.draw( data.img, dp );
}
void demo_1( int demidx )
{
Data data( demidx, "lines" );
std::cout << "Demo " << demidx << ": click on points and move them\n";
data.setMouseCB( action_1 );
int n=5;
data.vpt[0].set( data._imWidth/2, data._imHeight/n );
data.vpt[1].set( data._imWidth/2, data._imHeight*(n-1)/n );
data.vpt[2].set( data._imWidth/n, data._imHeight/2 );
data.vpt[3].set( data._imWidth*(n-1)/n, data._imHeight/2 );
data.clearImage();
action_1( &data );
data.showImage();
auto k = cv::waitKey(0);
if( k == 27 )
std::exit(0);
}
//------------------------------------------------------------------
struct Param_B: public Data
{
explicit Param_B( int demidx, std::string wname, std::string text ): Data( demidx, wname, text )
{}
void initPts()
{
int a=50;
int b=150;
vpt[0].set( a, a );
vpt[1].set( b, b );
vpt[2].set( b, a );
vpt[3].set( a, b );
}
};
/// Build H from R,T,S (no mouse)
void demo_B( int demidx )
{
Param_B data( demidx, "build Homography", "build Homography from Rotation, Translation, Scale \
Hit a key: scale:[op], angle:[lm], translation:[gh,yb], reset: r" );
double angle = 0.;
double angle_delta = 5.;
double scale = 1.;
double scale_delta = 1.2;
double tx = 0;
double ty = 0;
double trans_delta = 20;
double K = M_PI/180.;
data.clearImage();
data.initPts();
data.drawLines();
data.showImage();
KeyboardLoop kbloop;
kbloop.addKeyAction( 'r', [&](void*){ scale = 1.; angle = tx = ty = 0.; }, "reset" );
kbloop.addKeyAction( 'm', [&](void*){ angle += angle_delta; }, "increment angle" );
kbloop.addKeyAction( 'l', [&](void*){ angle -= angle_delta; }, "decrement angle" );
kbloop.addKeyAction( 'z', [&](void*){ tx += trans_delta; }, "increment tx" );
kbloop.addKeyAction( 'a', [&](void*){ tx -= trans_delta; }, "decrement tx" );
kbloop.addKeyAction( 'b', [&](void*){ ty += trans_delta; }, "increment ty" );
kbloop.addKeyAction( 'y', [&](void*){ ty -= trans_delta; }, "decrement ty" );
kbloop.addKeyAction( 'p', [&](void*){ scale *= scale_delta; }, "increment scale" );
kbloop.addKeyAction( 'o', [&](void*){ scale /= scale_delta; }, "reduce scale" );
kbloop.addCommonAction( [&](void*)
{
data.clearImage();
Homogr H;
H.addRotation( angle*K ).addTranslation( tx, ty ).addScale( scale );
data.initPts();
H.applyTo(data.vpt);
data.drawLines();
data.showImage();
}
);
kbloop.start( data );
}
//------------------------------------------------------------------
struct Param_C: public Data
{
explicit Param_C( int demidx, std::string wname, std::string txt ): Data( demidx, wname, txt )
{
rect.set( Point2d( 180,120), Point2d( 380,280) );
vpt[0] = Point2d( 70,70 );
vpt[1] = Point2d( 480,380 );
}
int radius = 50;
std::array<Line2d,3> li;
FRect rect;
void drawLines()
{
for( size_t i=0; i<li.size(); i++ )
li[i].draw( img );
}
};
void action_C( void* param )
{
auto& data = *reinterpret_cast<Param_C*>(param);
data.clearImage();
data.drawLines();
Circle c1( data.vpt[0], data.radius );
Circle c2( data.vpt[1], 100 );
img::DrawParams dpc2;
dpc2.setColor(150,0,150);
if( c2.isInside( c1 ) )
dpc2.setColor(250,100,0);
c2.draw( data.img, dpc2 );
img::DrawParams dp;
dp.setColor(150,0,150);
if( data.rect.isInside( c1 ) )
dp.setColor(250,100,0);
data.rect.draw( data.img, dp );
img::DrawParams dpc1;
dpc1.setColor(0,250,0);
if( c1.isInside( data.rect ) )
dpc1.setColor(250,100,0);
if( c1.isInside( c2 ) )
dpc1.setColor(250,100,0);
c1.draw( data.img, dpc1 );
data._pt_mouse.draw( data.img, img::DrawParams().setColor(250,50,20) );
// circle - circle intersections
auto cci = c1.intersects( c2 );
if( cci() )
draw( data.img, cci.get(), img::DrawParams().setColor(0,150,0).setPointStyle(img::PtStyle::Diam) );
// circle - rectangle intersections
auto cr1 = c1.intersects( data.rect );
auto cr2 = c2.intersects( data.rect );
if( cr1() )
draw( data.img, cr1.get(), img::DrawParams().setColor(0,20,220).setPointStyle(img::PtStyle::Diam) );
if( cr2() )
draw( data.img, cr2.get(), img::DrawParams().setColor(0,20,220).setPointStyle(img::PtStyle::Diam) );
// circle - lines intersections
for( size_t i=0; i<data.li.size(); i++ )
{
auto ri = data.li[i].intersects( c1 );
if( ri() )
{
auto inter = ri.get();
inter.first.draw( data.img, img::DrawParams().setColor(250, 0, 0) );
inter.second.draw( data.img, img::DrawParams().setColor(250, 0, 0) );
}
}
auto seg = getSegment( c1, c2 );
seg.draw( data.img, img::DrawParams().setColor(250, 0, 0) );
auto pseg = getTanSegs( c1, c2 );
pseg.first.draw( data.img, img::DrawParams().setColor(250, 250, 0) );
pseg.second.draw( data.img, img::DrawParams().setColor(0, 250, 250) );
data.showImage();
}
void demo_C( int demidx )
{
Param_C data( demidx, "circle demo", "move circle over line, hit [lm] to change circle radius" );
data.li[0] = Point2d() * Point2d(200,100);
data.li[1] = Point2d(200,0) * Point2d(200,200);
data.li[2] = Point2d(0,200) * Point2d(200,200);
data.clearImage();
data.drawLines();
action_C( &data );
data.showImage();
data.setMouseCB( action_C );
KeyboardLoop kbloop;
kbloop.addKeyAction( 'l', [&](void*){ data.radius += 10; }, "increment radius" );
kbloop.addKeyAction( 'm', [&](void*){ data.radius -= 10; }, "decrement radius" );
kbloop.addKeyAction( 'r', [&](void*){ data.radius = 80; }, "reset radius" );
kbloop.start( data );
}
//------------------------------------------------------------------
struct Param_SI: Data
{
explicit Param_SI( int demidx, std::string wname, std::string txt ): Data( demidx, wname, txt )
{}
Segment seg1,seg2;
};
double action_SI_drawDist( const Segment& seg, void* param )
{
auto& data = *reinterpret_cast<Param_SI*>(param);
int segDistCase;
auto segDist = seg.distTo( data._pt_mouse, &segDistCase );
auto col_A = img::DrawParams().setColor( 0,200,200 );
auto col_B = img::DrawParams().setColor( 200,200,0 );
switch( segDistCase )
{
case -1:
draw( data.img, Segment( data._pt_mouse, seg.getPts().first ), col_B );
break;
case +1:
draw( data.img, Segment( data._pt_mouse, seg.getPts().second ), col_B );
break;
default:
if( data._pt_mouse.distTo( seg.getLine() ) > 3. )
{
auto orthog_seg = seg.getLine().getOrthogSegment( data._pt_mouse );
orthog_seg.draw( data.img, col_A );
}
}
return segDist;
}
void action_SI( void* param )
{
auto& data = *reinterpret_cast<Param_SI*>(param);
data.clearImage();
data.seg1.set( data.vpt[0], data.vpt[1] );
data.seg2.set( data.vpt[2], data.vpt[3] );
data.seg1.draw( data.img, img::DrawParams().setColor( 0,0,250).setThickness(2) );
data.seg2.draw( data.img, img::DrawParams().setColor( 250,0,0).setThickness(2) );
data.seg1.getLine().draw( data.img, img::DrawParams().setColor( 200,200,200) );
data.seg2.getLine().draw( data.img, img::DrawParams().setColor( 200,200,200) );
draw( data.img, data.vpt );
auto psegs = data.seg1.getParallelSegs( 40 );
draw( data.img, psegs.first, img::DrawParams().setColor( 0,250,200) );
draw( data.img, psegs.second, img::DrawParams().setColor( 200,0,250) );
if( data._selected != -1 )
data.vpt[data._selected].draw( data.img, img::DrawParams().selectPoint() );
auto inters = data.seg1.intersects( data.seg2 );
if( inters() )
{
auto pti = inters.get();
pti.draw( data.img );
Line2d l1 = data.seg1.getLine().getOrthogLine( pti );
l1.draw( data.img, img::DrawParams().setColor( 0,0,100) );
Line2d l2 = data.seg2.getLine().getOrthogLine( pti );
l2.draw( data.img, img::DrawParams().setColor( 100,0,0) );
}
Line2d li2( Point2d(350,120),Point2d(20,50));
li2.draw( data.img );
auto inters1 = data.seg1.intersects( li2 );
if( inters1() )
inters1.get().draw( data.img, img::DrawParams().setPointStyle(img::PtStyle::Diam).setColor( 250,0,0));
auto inters2 = data.seg2.intersects( li2 );
if( inters2() )
inters2.get().draw( data.img, img::DrawParams().setPointStyle(img::PtStyle::Diam).setColor( 250,0,0));
auto segDist1 = action_SI_drawDist( data.seg1, param );
auto segDist2 = action_SI_drawDist( data.seg2, param );
data.putTextLine( "distance mouse/s1=" + std::to_string(segDist1) + " mouse/s2=" + std::to_string(segDist2) ,0 );
data.showImage();
}
/// Segment intersection demo
void demo_SI( int demidx )
{
Param_SI data( demidx, "segment_intersection",
"Intersection of segments\n Select a point and move it around. \
When they intersect, you get the orthogonal lines of the two segments, at the intersection point.\n \
Also shows parallel segments" );
data.vpt[0] = Point2d(100,200);
data.vpt[1] = Point2d(200,300);
data.vpt[2] = Point2d(150,50);
data.vpt[3] = Point2d(300,250);
action_SI( &data );
data.setMouseCB( action_SI );
if( 27 == cv::waitKey(0) )
std::exit(0);
}
//------------------------------------------------------------------
struct Param_6 : Data
{
explicit Param_6( int demidx, std::string wname, std::string txt ): Data( demidx, wname, txt )
{}
float angle = 20;
};
void action_6( void* param )
{
auto& data = *reinterpret_cast<Param_6*>(param);
data.clearImage();
double K = M_PI / 180.;
auto tx = data._pt_mouse.getX();
auto ty = data._pt_mouse.getY();
auto mouse_pos = std::make_pair(
Line2d( LineDir::H, ty ),
Line2d( LineDir::V, tx )
);
draw( data.img, mouse_pos, img::DrawParams().setColor( 200,200,200) );
/* auto org = std::make_pair(
Line2d( LineDir::H, ty ),
Line2d( LineDir::V, tx )
);
draw( data.img, org );
*/
Homogr H = Homogr().addTranslation(-tx,-ty).addRotation( data.angle * K ).addTranslation(tx,ty);
draw( data.img, data.vpt[0] );
draw( data.img, data.vpt[1] );
Line2d l1( data.vpt[0], data.vpt[1] );
Line2d l2 = l1.getRotatedLine( data.vpt[0], data.angle * K );
auto dpar = img::DrawParams();
l1.draw( data.img, dpar.setColor( 250,0,0) );
l2.draw( data.img, dpar.setColor( 0,250,0) );
Segment s1( data.vpt[2], data.vpt[3] );
Segment s2 = H*s1;
s1.draw( data.img, dpar.setColor( 250,0,0) );
s2.draw( data.img, dpar.setColor( 0,0,250) );
s1.getPts().first.draw( data.img, dpar.selectPoint() );
s1.getPts().second.draw( data.img, dpar.selectPoint() );
}
void demo_6( int demidx )
{
Param_6 data( demidx, "homography_lines_seg",
"Apply homography to lines and segments\n Hit [lm] to change angle, \
and select points of blue segment with mouse" );
double angle_delta = 5.; // degrees
data.setMouseCB( action_6 );
action_6( &data );
data.showImage();
KeyboardLoop kbloop;
kbloop.addKeyAction( 'm', [&](void*){ data.angle += angle_delta; std::cout << "val=" <<data.angle<<'\n'; }, "increment angle" );
kbloop.addKeyAction( 'l', [&](void*){ data.angle -= angle_delta; std::cout << "val=" <<data.angle<<'\n'; }, "decrement angle" );
kbloop.addCommonAction( [&](void*){ action_6(&data);} );
kbloop.start( data );
}
//------------------------------------------------------------------
/// This one has two windows
struct Param_H: public Data
{
int hmethod = 1;
img::Image<cv::Mat> img2;
std::string win2 = "Computed projection";
explicit Param_H( int demidx, std::string wname ): Data( demidx, wname )
{
cv::namedWindow( win2 );
cv::moveWindow( win2, _imWidth, 50 );
img2.setSize( _imHeight, _imWidth );
}
void showImage()
{
img.show( win1 );
img2.show( win2 );
}
void clearImage()
{
img.clear();
img2.clear();
}
void reset()
{
auto pa1 = Point2d(100,100);
auto pa2 = Point2d(400,300);
auto v1 = get4Pts( FRect(pa1, pa2) );
auto pb1 = Point2d(80,150);
auto pb2 = Point2d(450,350);
auto v2 = get4Pts( FRect(pb1, pb2) );
std::copy( v1.begin(), v1.end(), vpt.begin() );
std::copy( v2.begin(), v2.end(), vpt.begin()+4 );
}
};
void action_H( void* param )
{
auto& data = *reinterpret_cast<Param_H*>(param);
data.clearImage();
std::vector<Point2d> v1(4);
std::vector<Point2d> v2(4);
std::copy( data.vpt.begin(), data.vpt.begin()+4, v1.begin() );
std::copy( data.vpt.begin()+4, data.vpt.end(), v2.begin() );
for( int i=0; i<4; i++ )
{
Segment s1( v1[i], v1[i==3?0:i+1] );
Segment s2( v2[i], v2[i==3?0:i+1] );
s1.draw( data.img, img::DrawParams().setColor( 0,0,250) );
s2.draw( data.img, img::DrawParams().setColor( 250,0,0) );
s1.draw( data.img2, img::DrawParams().setColor( 0,0,250) );
Segment( v1[i], v2[i] ).draw( data.img );
v1[i].draw( data.img );
v2[i].draw( data.img );
}
std::vector<Point2d> vseg;
auto center_x = 160;
auto center_y = 220;
auto size_v = 40;
auto size_h = 60;
vseg.emplace_back( Point2d(center_x+size_h, center_y) );
vseg.emplace_back( Point2d(center_x-size_h, center_y) );
vseg.emplace_back( Point2d(center_x, center_y+size_v) );
vseg.emplace_back( Point2d(center_x, center_y-size_v) );
Segment sa1( vseg[0], vseg[1] );
Segment sb1( vseg[2], vseg[3] );
sa1.draw( data.img, img::DrawParams().setColor( 0,100,100) );
sb1.draw( data.img, img::DrawParams().setColor( 0,100,100) );
cv::putText( data.img.getReal(), "source points", cv::Point2i( v1[0].getX(), v1[0].getY() ), 0, 0.8, cv::Scalar( 250,0,0 ), 2 );
cv::putText( data.img.getReal(), "dest points", cv::Point2i( v2[0].getX(), v2[0].getY() ), 0, 0.8, cv::Scalar( 0,0,250 ), 2 );
Homogr H;
H.buildFrom4Points( v1, v2, data.hmethod );
std::cout << "Computed Homography:\n" << H << '\n';
std::vector<Point2d> vpt3;
for( int i=0; i<4; i++ )
vpt3.push_back( H * data.vpt[i] );
for( int i=0; i<4; i++ )
{
Segment s1( vpt3[i], vpt3[i==3?0:i+1] );
s1.draw( data.img2, img::DrawParams().setColor( 0,250,0) );
}
auto vsegH = H * vseg;
Segment sa2( vsegH[0], vsegH[1] );
Segment sb2( vsegH[2], vsegH[3] );
sa2.draw( data.img2, img::DrawParams().setColor( 0,100,100) );
sb2.draw( data.img2, img::DrawParams().setColor( 0,100,100) );
FRect rect( Point2d(200,160), Point2d(330,250));
rect.draw( data.img );
auto rect2 = H * rect;
rect2.draw( data.img2 );
auto e_x = 320.;
auto e_y = 360.;
auto e_h = 70.;
Circle c_ell( e_x, e_y, e_h );
c_ell.draw( data.img );
auto ell = H * c_ell;
ell.draw( data.img2 );
auto ecenter = ell.getCenter();
ecenter.draw( data.img2 );
auto ell_bb = ell.getBB();
ell_bb.draw( data.img2 );
data.showImage();
}
/// Demo of computing a homography from two sets of 4 points
void demo_H( int demidx )
{
Param_H data( demidx, "Compute Homography from 4 points" );
data.vpt.resize(8);
data.reset();
std::cout << "Demo " << demidx << ": compute homography from two sets of 4 points\n"
<< " - usage: move points with mouse in left window, right window will show source rectangle (blue)\n"
<< "and computed projected rectangle (green)\n"
<< " - keys:\n -a: switch backend computing library\n -r: reset points\n";
data.setMouseCB( action_H );
action_H( &data );
KeyboardLoop kbloop;
kbloop.addKeyAction( 'r', [&](void*){ data.reset(); } );
kbloop.addKeyAction( 'a', [&](void*)
{
data.hmethod = data.hmethod?0:1;
#if !defined(HOMOG2D_USE_EIGEN)
if( data.hmethod == 0 )
{
std::cout << "Unable, build without Eigen support, see manual, switch to Opencv\n";
data.hmethod = 1;
}
#endif
}
);
kbloop.start( data );
}
//------------------------------------------------------------------
/// This datatype holds two Polyline objects, one closed, one open. They are both edited the same way and
/// we switch drawing
struct Param_PL : Data
{
explicit Param_PL( int demidx, std::string title, std::string txt ):Data( demidx, title, txt )
{
v_po.push_back( &polyline_o );
v_po.push_back( &polyline_c );
}
OPolyline polyline_o;
CPolyline polyline_c;
std::vector<detail::Common<double>*> v_po; ///< both are stored here, so we can easily switch between them
bool showClosedPoly = false;
};
void action_PL( void* param )
{
auto& data = *reinterpret_cast<Param_PL*>(param);
data.clearImage();
data.polyline_o.set( data.vpt );
data.polyline_c.set( data.vpt );
auto color = img::DrawParams().setColor( 0,10,200);
if( data.polyline_c.isSimple() )
color = img::DrawParams().setColor( 250,10,20);
auto len = data.showClosedPoly ? data.polyline_c.length() : data.polyline_o.length();
if( data.showClosedPoly )
data.polyline_c.draw( data.img, color );
else
data.polyline_o.draw( data.img, color );
auto col_green = img::DrawParams().setColor(10,250,10);
Line2d li( Point2d( 10,60), Point2d( 400,270) );
li.draw( data.img, col_green );
data.putTextLine( std::string("Nb pts=") + std::to_string( data.polyline_c.size() ), 0 );
data.putTextLine( std::string("length=") + std::to_string(len) );
auto intersPts_o = li.intersects(data.polyline_o).get();
auto intersPts_c = li.intersects(data.polyline_c).get();;
auto intersPts = ( data.showClosedPoly ? intersPts_c : intersPts_o );
for( const auto& pt: intersPts )
pt.draw( data.img );
Circle cir( 350,180,85);
cir.draw( data.img, col_green );
auto i_cir_o = cir.intersects( data.polyline_o );
auto i_cir_c = cir.intersects( data.polyline_c );
FRect rect( 90,160,205,245);
rect.draw( data.img, col_green );
auto i_rect_o = rect.intersects( data.polyline_o );
auto i_rect_c = rect.intersects( data.polyline_c );
std::string str_ispoly{"Polygon: N"};
if( data.showClosedPoly )