-
Notifications
You must be signed in to change notification settings - Fork 0
/
Marker.cpp
1485 lines (1278 loc) · 45.9 KB
/
Marker.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 ALVAR, A Library for Virtual and Augmented Reality.
*
* Copyright 2007-2012 VTT Technical Research Centre of Finland
*
* Contact: VTT Augmented Reality Team <[email protected]>
* <http://www.vtt.fi/multimedia/alvar.html>
*
* ALVAR is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ALVAR; if not, see
* <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
*/
/*****************************************************************************
****************************** I N C L U D E ******************************
****************************************************************************/
#include "Alvar.h"
#include "Marker.h"
#include <opencv2/highgui/highgui.hpp>
namespace alvar {
using namespace std;
/*****************************************************************************
*** class Marker
****************************************************************************/
Marker::Marker(double _edge_length, int _res, double _margin)
{
margin_error = 0;
decode_error = 0;
track_error = 0;
SetMarkerSize(_edge_length, _res, _margin);
return;
}
Marker::Marker(const Marker& m) : marker_content(m.marker_content)
{
SetMarkerSize(m.edge_length, m.res, m.margin);
pose = m.pose;
margin_error = m.margin_error;
decode_error = m.decode_error;
track_error = m.track_error;
// cvCopy(m.marker_content, marker_content);
marker_corners.resize(m.marker_corners.size());
copy(m.marker_corners.begin(), m.marker_corners.end(), marker_corners.begin());
marker_points.resize(m.marker_points.size());
copy(m.marker_points.begin(), m.marker_points.end(), marker_points.begin());
marker_corners_img.resize(m.marker_corners_img.size());
copy(m.marker_corners_img.begin(), m.marker_corners_img.end(), marker_corners_img.begin());
#ifdef VISUALIZE_MARKER_POINTS
marker_allpoints_img.resize(m.marker_allpoints_img.size());
copy(m.marker_allpoints_img.begin(), m.marker_allpoints_img.end(), marker_allpoints_img.begin());
#endif
return;
}
void Marker::VisualizeMarkerPose(cv::Mat& image, Camera& /* cam */,
std::vector<cv::Point2d>& visualize2d_points, cv::Scalar color) const
{
// Cube
for (size_t i = 0 ; i < 4 ; i++)
{
cv::line(image, visualize2d_points[i],
visualize2d_points[(i + 1) % 4], color);
cv::line(image, visualize2d_points[i],
visualize2d_points[4 + i], color);
cv::line(image, visualize2d_points[4 + i],
visualize2d_points[4 + (( i+ 1) % 4)], color);
}
// Coordinates
cv::line(image, visualize2d_points[8], visualize2d_points[9], cv::Scalar(0, 0, 255));
cv::line(image, visualize2d_points[8], visualize2d_points[10], cv::Scalar(0, 255, 0));
cv::line(image, visualize2d_points[8], visualize2d_points[11], cv::Scalar(255, 0, 0));
return;
}
void Marker::VisualizeMarkerContent(cv::Mat& image, Camera& /* cam */, cv::Point2d datatext_point, cv::Point2d content_point) const
{
#ifdef VISUALIZE_MARKER_POINTS
for (const auto& pt : marker_allpoints_img)
{
if (pt.val == 0)
cv::circle(image, pt, 1, cv::Scalar(0, 255,0));
else if (pt.val == 255)
cv::circle(image, pt, 1, cv::Scalar(0, 0, 255));
else
cv::circle(image, pt, 2, cv::Scalar(0, 255, 255));
}
#endif
// Marker data
std::stringstream val;
val << int(GetId());
cv::putText(image, val.str(), datatext_point, cv::FONT_HERSHEY_PLAIN, 0.5,
cv::Scalar(0, 255, 255));
// MarkerContent
int xc = int(content_point.x);
int yc = int(content_point.y);
for (int j = 0 ; j < res * 3 ; j++)
{
for (int i = 0 ; i < res * 3 ; i++)
{
int x = xc + i;
int y = yc + j;
if ((x >= 0) && (x < image.cols) &&
(y >= 0) && (y < image.rows))
{
if (marker_content.at<uchar>(j / 3, i / 3))
{
image.at<cv::Vec3b>(y, x) = cv::Vec3b(255, 255, 255);
}
else
{
image.at<cv::Vec3b>(y, x) = cv::Vec3b(0, 0, 0);
}
}
}
}
return;
}
void Marker::VisualizeMarkerError(cv::Mat& image, Camera& /* cam */, cv::Point2d errortext_point) const
{
std::stringstream val;
if (GetError(MARGIN_ERROR | DECODE_ERROR) > 0)
{
val.str("");
val << int(GetError(MARGIN_ERROR) * 100) << "% ";
val << int(GetError(DECODE_ERROR) * 100) << "% ";
cv::putText(image, val.str(), errortext_point, cv::FONT_HERSHEY_PLAIN, 0.5, cv::Scalar(0, 0, 255));
}
else if (GetError(TRACK_ERROR) > 0.01)
{
val.str("");
val << int(GetError(TRACK_ERROR) * 100) << "%";
cv::putText(image, val.str(), errortext_point, cv::FONT_HERSHEY_PLAIN, 0.5, cv::Scalar(0, 0, 128));
}
return;
}
void MarkerData::VisualizeMarkerContent(cv::Mat& image, Camera& /* cam */,
cv::Point2d datatext_point, cv::Point2d /* content_point */) const
{
#ifdef VISUALIZE_MARKER_POINTS
for (const auto& pt : marker_allpoints_img)
{
if (pt.val == 0)
cv::circle(image, pt, 1, cv::Scalar(0, 255, 0));
else if (pt.val == 255)
cv::circle(image, pt, 1, cv::Scalar(0, 0, 255));
else
cv::circle(image, pt, 2, cv::Scalar(0, 255, 255));
}
#endif
// Marker data
std::stringstream val;
cv::Scalar rgb(0, 255, 255);
if (content_type == MarkerContentType::MARKER_CONTENT_TYPE_NUMBER)
{
val << int(GetId());
}
else
{
if (content_type == MarkerContentType::MARKER_CONTENT_TYPE_FILE)
rgb = cv::Scalar(255, 0, 0);
if (content_type == MarkerContentType::MARKER_CONTENT_TYPE_HTTP)
rgb = cv::Scalar(255, 0, 255);
val << data.str;
}
cv::putText(image, val.str(), datatext_point, cv::FONT_HERSHEY_PLAIN, 2.0, rgb);
return;
}
void Marker::Visualize(cv::Mat& image, Camera& cam, cv::Scalar color) const
{
std::vector<cv::Point3d> visualize3d_points =
{
// Cube vertices
{-(edge_length / 2), -(edge_length / 2), 0},
{-(edge_length / 2), (edge_length / 2), 0},
{ (edge_length / 2), (edge_length / 2), 0},
{ (edge_length / 2), -(edge_length / 2), 0},
{-(edge_length / 2), -(edge_length / 2), edge_length},
{-(edge_length / 2), (edge_length / 2), edge_length},
{ (edge_length / 2), (edge_length / 2), edge_length},
{ (edge_length / 2), -(edge_length / 2), edge_length},
// Coodinate axes
{0, 0, 0},
{edge_length, 0, 0},
{0, edge_length, 0},
{0, 0, edge_length}
};
std::vector<cv::Point2d> visualize2d_points(12);
cam.ProjectPoints(visualize3d_points, pose, visualize2d_points);
VisualizeMarkerPose(image, cam, visualize2d_points, color);
VisualizeMarkerContent(image, cam, visualize2d_points[0], visualize2d_points[8]);
VisualizeMarkerError(image, cam, visualize2d_points[2]);
return;
}
void Marker::CompareCorners(vector<PointDouble>& _marker_corners_img, int* orientation, double* error)
{
vector<PointDouble >::iterator corners_new = _marker_corners_img.begin();
vector<PointDouble >::const_iterator corners_old = marker_corners_img.begin();
vector<double> errors(4);
for (int i = 0 ; i < 4 ; i++)
{
errors[0] += PointSquaredDistance(marker_corners_img[i], _marker_corners_img[i]);
errors[1] += PointSquaredDistance(marker_corners_img[i], _marker_corners_img[(i + 1) % 4]);
errors[2] += PointSquaredDistance(marker_corners_img[i], _marker_corners_img[(i + 2) % 4]);
errors[3] += PointSquaredDistance(marker_corners_img[i], _marker_corners_img[(i + 3) % 4]);
}
*orientation = min_element(errors.begin(), errors.end()) - errors.begin();
*error = sqrt(errors[*orientation] / 4);
*error /= sqrt(max(PointSquaredDistance(marker_corners_img[0], marker_corners_img[2]),
PointSquaredDistance(marker_corners_img[1], marker_corners_img[3])));
return;
}
#if 0 // TTC!!
void Marker::CompareContent(vector<PointDouble>& _marker_corners_img, cv::Mat& gray, Camera *cam, int *orientation) const {
// TODO: Note, that to use this method you need to straighten the content
// TODO: This method can be used with image based trackingt
}
#endif
bool Marker::UpdateContentBasic(vector<PointDouble>& _marker_corners_img, cv::Mat& gray, Camera& cam,
int /* frame_no */ /*= 0*/)
{
vector<PointDouble> marker_corners_img_undist;
marker_corners_img_undist.resize(_marker_corners_img.size());
copy(_marker_corners_img.begin(), _marker_corners_img.end(), marker_corners_img_undist.begin());
// Figure out the marker point position in the image
Homography H;
vector<PointDouble> marker_points_img(marker_points.size());
marker_points_img.resize(marker_points.size());
cam.Undistort(marker_corners_img_undist);
H.Find(marker_corners, marker_corners_img_undist);
H.ProjectPoints(marker_points, marker_points_img);
cam.Distort(marker_points_img);
// Read the content
int x, y;
double min = 255.0, max = 0.0;
for (int j = 0 ; j < marker_content.rows ; j++)
{
for (int i = 0 ; i < marker_content.cols ; i++)
{
x = static_cast<int>(0.5 + Limit(marker_points_img[(j * marker_content.cols) + i].x, 1.0,
static_cast<double>(gray.cols - 2)));
y = static_cast<int>(0.5 + Limit(marker_points_img[(j * marker_content.cols) + i].y, 1.0,
static_cast<double>(gray.rows - 2)));
marker_points_img[(j * marker_content.cols) + i].val = gray.at<uchar>(y, x);
/*
// Use median of 5 neighbor pixels
vector<int> vals;
vals.clear();
vals.push_back();
vals.push_back((int)cvGetReal2D(gray, y-1, x));
vals.push_back((int)cvGetReal2D(gray, y, x-1));
vals.push_back((int)cvGetReal2D(gray, y+1, x));
vals.push_back((int)cvGetReal2D(gray, y, x+1));
nth_element(vals.begin(), vals.begin()+2, vals.end());
tmp = vals[2];
*/
marker_content.at<uchar>(j, i) = marker_points_img[(j * marker_content.cols) + i].val;
if (marker_points_img[(j * marker_content.cols) + i].val > max)
max = marker_points_img[(j * marker_content.cols) + i].val;
if (marker_points_img[(j * marker_content.cols) + i].val < min)
min = marker_points_img[(j * marker_content.cols) + i].val;
}
}
// Take few additional points from border and just
// outside the border to make the right thresholding
vector<PointDouble> marker_margin_w_img(marker_margin_w.size());
vector<PointDouble> marker_margin_b_img(marker_margin_b.size());
H.ProjectPoints(marker_margin_w, marker_margin_w_img);
H.ProjectPoints(marker_margin_b, marker_margin_b_img);
cam.Distort(marker_margin_w_img);
cam.Distort(marker_margin_b_img);
min = max = 0; // Now min and max values are averages over black and white border pixels.
for (auto& marker_margin : marker_margin_w_img)
{
x = static_cast<int>(0.5 + Limit(marker_margin.x, 0.0, static_cast<double>(gray.cols - 1)));
y = static_cast<int>(0.5 + Limit(marker_margin.y, 0.0, static_cast<double>(gray.rows - 1)));
marker_margin.val = gray.at<uchar>(y, x);
max += marker_margin.val;
//if(marker_margin_w_img[i].val > max) max = marker_margin_w_img[i].val;
//if(marker_margin_w_img[i].val < min) min = marker_margin_w_img[i].val;
}
for (auto& marker_margin : marker_margin_b_img)
{
x = static_cast<int>(0.5 + Limit(marker_margin.x, 0.0, static_cast<double>(gray.cols - 1)));
y = static_cast<int>(0.5 + Limit(marker_margin.y, 0.0, static_cast<double>(gray.rows - 1)));
marker_margin.val = gray.at<uchar>(y, x);
min += marker_margin.val;
//if(marker_margin_b_img[i].val > max) max = marker_margin_b_img[i].val;
//if(marker_margin_b_img[i].val < min) min = marker_margin_b_img[i].val;
}
max /= marker_margin_w_img.size();
min /= marker_margin_b_img.size();
// Threshold the marker content
cv::threshold(marker_content, marker_content, (max + min) / 2.0, 255, cv::THRESH_BINARY);
// Count erroneous margin nodes
int erroneous = 0;
int total = 0;
for (auto& marker_margin : marker_margin_w_img)
{
if (marker_margin.val < (max + min) / 2.0)
erroneous++;
total++;
}
for (auto& marker_margin : marker_margin_b_img)
{
if (marker_margin.val > (max + min) / 2.0)
erroneous++;
total++;
}
margin_error = static_cast<double>(erroneous) / total;
// track_error; // TTC!!
#ifdef VISUALIZE_MARKER_POINTS
// Now we fill also this temporary debug table for visualizing marker code reading
// TODO: this whole vector is only for debug purposes
marker_allpoints_img.clear();
for (auto& marker_margin : marker_margin_w_img)
{
PointDouble p = marker_margin;
if (p.val < (max + min) / 2.0)
p.val = 255; // error
else
p.val = 0; // ok
marker_allpoints_img.push_back(p);
}
for (auto& marker_margin : marker_margin_b_img)
{
PointDouble p = marker_margin;
if (p.val > (max + min) / 2.0)
p.val = 255; // error
else
p.val = 0; // ok
marker_allpoints_img.push_back(p);
}
for (auto& marker_point : marker_points_img)
{
PointDouble p = marker_point;
p.val = 128; // Unknown?
marker_allpoints_img.push_back(p);
}
#endif
return true;
}
void Marker::UpdatePose(vector<PointDouble>& _marker_corners_img, Camera& cam, int orientation,
int /* frame_no */ /* =0 */, bool update_pose /* =true */)
{
marker_corners_img.resize(_marker_corners_img.size());
copy(_marker_corners_img.begin(), _marker_corners_img.end(), marker_corners_img.begin());
// Calculate exterior orientation
if (orientation > 0)
std::rotate(marker_corners_img.begin(), marker_corners_img.begin() + orientation, marker_corners_img.end());
if (update_pose)
cam.CalcExteriorOrientation(marker_corners, marker_corners_img, pose);
return;
}
bool Marker::DecodeContent(int& orientation)
{
orientation = 0;
decode_error = 0;
return (true);
}
void Marker::SaveMarkerImage(const char* filename, int save_res) const
{
if (save_res == 0)
{
// TODO: More intelligent deduction of a minimum save_res
save_res = static_cast<int>((res + margin + margin) * 12);
}
double scale = save_res / (res + margin + margin);
// Create a black image
cv::Mat img(cv::Size(save_res, save_res), CV_8U, cv::Scalar(0));
// Resize anc copy the marker to the black image at the correct position
int submat_pos = static_cast<int>(margin * scale);
int submat_size = static_cast<int>(res * scale);
cv::Mat submat(img, cv::Rect(submat_pos, submat_pos, submat_size, submat_size));
int resize = static_cast<int>(res * scale + 0.5);
cv::Mat img_content;
cv::resize(marker_content, img_content, cv::Size(resize, resize), cv::INTER_NEAREST);
img_content.copyTo(submat);
cv::imwrite(filename, img);
return;
}
void Marker::ScaleMarkerToImage(cv::Mat& image) const
{
const int multiplier = 96;
// Create a black image
int image_size = static_cast<int>(multiplier * (res + margin + margin) + 0.5);
cv::Mat img(cv::Size(image_size, image_size), CV_8U, cv::Scalar(0));
// Resize anc copy the marker to the black image at the correct position
cv::Mat img_content;
int submat_pos = static_cast<int>(multiplier * margin + 0.5);
int submat_size = static_cast<int>(multiplier * res + 0.5);
cv::Mat submat(img, cv::Rect(submat_pos, submat_pos, submat_size, submat_size));
cv::resize(marker_content, img_content, cv::Size(submat_size, submat_size), cv::INTER_NEAREST);
img_content.copyTo(submat);
image = img;
return;
}
void Marker::SetMarkerSize(double _edge_length, int _res, double _margin)
{
// TODO: Is this right place for default marker size?
edge_length = (_edge_length ? _edge_length : 1);
res = _res; //(_res?_res:10);
margin = ((_margin != 0.0) ? _margin : 1);
double x_min = -0.5 * edge_length;
double y_min = -0.5 * edge_length;
double x_max = 0.5 * edge_length;
double y_max = 0.5 * edge_length;
double cx_min = (x_min * res) / (res + margin + margin);
double cy_min = (y_min * res) / (res + margin + margin);
// double cx_max = (x_max * res)/(res + margin + margin); // TTC!!
double cy_max = (y_max * res) / (res + margin + margin);
double step = edge_length / (res + margin + margin);
// marker_corners
marker_corners_img.resize(4);
// Same order as the detected corners
marker_corners.clear();
marker_corners.push_back(PointDouble(x_min, y_min));
marker_corners.push_back(PointDouble(x_max, y_min));
marker_corners.push_back(PointDouble(x_max, y_max));
marker_corners.push_back(PointDouble(x_min, y_max));
// Rest can be done only if we have existing resolution
if (res > 0)
{
// marker_points
marker_points.clear();
for (int j = 0 ; j < res ; ++j)
{
for (int i = 0 ; i < res ; ++i)
{
PointDouble pt;
pt.y = cy_max - (step * j) - (step / 2);
pt.x = cx_min + (step * i) + (step / 2);
marker_points.push_back(pt);
}
}
// Samples to be used in margins
// TODO: Now this works only if the "margin" is without decimals
// TODO: This should be made a lot cleaner
marker_margin_w.clear();
marker_margin_b.clear();
for (int j = -1 ; j <= margin - 1 ; j++)
{
PointDouble pt;
// Sides
for (int i = 0 ; i < res ; i++)
{
pt.x = cx_min + step * i + step / 2;
pt.y = y_min + step * j + step / 2;
if (j < 0)
marker_margin_w.push_back(pt);
else
marker_margin_b.push_back(pt);
pt.y = y_max - step * j - step / 2;
if (j < 0)
marker_margin_w.push_back(pt);
else
marker_margin_b.push_back(pt);
pt.y = cy_min + step * i + step / 2;
pt.x = x_min + step * j + step / 2;
if (j < 0)
marker_margin_w.push_back(pt);
else
marker_margin_b.push_back(pt);
pt.x = x_max - step * j - step / 2;
if (j < 0)
marker_margin_w.push_back(pt);
else
marker_margin_b.push_back(pt);
}
// Corners
for (int i = -1 ; i <= margin - 1 ; i++)
{
pt.x = x_min + step * i + step / 2;
pt.y = y_min + step * j + step / 2;
if ((j < 0) || (i < 0))
marker_margin_w.push_back(pt);
else
marker_margin_b.push_back(pt);
pt.x = x_min + step * i + step / 2;
pt.y = y_max - step * j - step / 2;
if ((j < 0) || (i < 0))
marker_margin_w.push_back(pt);
else
marker_margin_b.push_back(pt);
pt.x = x_max - step * i - step / 2;
pt.y = y_max - step * j - step / 2;
if ((j < 0) || (i < 0))
marker_margin_w.push_back(pt);
else
marker_margin_b.push_back(pt);
pt.x = x_max - step * i - step / 2;
pt.y = y_min + step * j + step / 2;
if ((j < 0) || (i < 0))
marker_margin_w.push_back(pt);
else
marker_margin_b.push_back(pt);
}
}
/*
for(int j = -margin-1; j < res+margin+margin+2; ++j) {
for(int i = 0; i < res+margin+margin+2; ++i) {
PointDouble pt;
pt.y = y_min - step/2 + step*j;
pt.x = x_min - step/2 + step*i;
if ((pt.x < x_min) || (pt.y < y_min) ||
(pt.x > x_max) || (pt.y > y_max))
{
marker_margin_w.push_back(pt);
}
else
if ((pt.x < cx_min) || (pt.y < cy_min) ||
(pt.x > cx_max) || (pt.y > cy_max))
{
marker_margin_b.push_back(pt);
}
}
}
*/
/*
//double step = edge_length / (res + margin + margin);
for(int j = 0; j < res+margin+margin+2; ++j) {
for(int i = 0; i < res+margin+margin+2; ++i) {
PointDouble pt;
pt.y = y_min - step/2 + step*j;
pt.x = x_min - step/2 + step*i;
if ((pt.x < x_min) || (pt.y < y_min) ||
(pt.x > x_max) || (pt.y > y_max))
{
marker_margin_w.push_back(pt);
}
else
if ((pt.x < cx_min) || (pt.y < cy_min) ||
(pt.x > cx_max) || (pt.y > cy_max))
{
marker_margin_b.push_back(pt);
}
}
}
*/
/*
marker_margin_w.clear();
marker_margin_b.clear();
for (double y=y_min-(step/2); y<y_max+(step/2); y+=step) {
for (double x=x_min-(step/2); x<x_max+(step/2); x+=step) {
PointDouble pt(x, y);
if ((x < x_min) || (y < y_min) ||
(x > x_max) || (y > y_max))
{
marker_margin_w.push_back(pt);
}
else
if ((x < cx_min) || (y < cy_min) ||
(x > cx_max) || (y > cy_max))
{
marker_margin_b.push_back(pt);
}
}
}
*/
/*
marker_points.clear();
marker_margin_w.clear();
marker_margin_b.clear();
for(int j = 0; j < res+margin+margin+2; ++j) {
for(int i = 0; i < res+margin+margin+2; ++i) {
PointDouble pt;
}
}
*/
// marker content
cv::Mat temp(res, res, CV_8U, cv::Scalar(255));
marker_content = temp;
} // end if
return;
}
/*****************************************************************************
*** class MarkerArtoolkit
****************************************************************************/
bool MarkerArtoolkit::DecodeContent(int& orientation)
{
int a = marker_content.at<uchar>(0, 0);
int b = marker_content.at<uchar>(res - 1, 0);
int c = marker_content.at<uchar>(res - 1, res - 1);
int d = marker_content.at<uchar>(0, res - 1);
if (!a && !b && c)
orientation = 0;
else if (!b && !c && d)
orientation = 1;
else if (!c && !d && a)
orientation = 2;
else if (!d && !a && b)
orientation = 3;
else
return (false);
Bitset bs;
bs.clear();
for (int j = 0 ; j < res ; j++)
{
for (int i = 0 ; i < res ; i++)
{
if (orientation == 0)
{
if ((j == 0) && (i == 0))
continue;
if ((j == res - 1) && (i == 0))
continue;
if ((j == res - 1) && (i == res - 1))
continue;
if (marker_content.at<uchar>(j, i))
bs.push_back(false);
else bs.push_back(true);
}
else if (orientation == 1)
{
if (((res - i - 1) == res - 1) && (j == 0))
continue;
if (((res - i - 1) == res - 1) && (j == res - 1))
continue;
if (((res - i - 1) == 0) && (j == res - 1))
continue;
if (marker_content.at<uchar>(res-i-1, j))
bs.push_back(false);
else
bs.push_back(true);
}
else if (orientation == 2)
{
if (((res - j - 1) == res - 1) && ((res - i - 1) == res - 1))
continue;
if (((res - j - 1) == 0) && ((res - i - 1) == res - 1))
continue;
if (((res - j - 1) == 0) && ((res - i - 1) == 0))
continue;
if (marker_content.at<uchar>(res - j - 1, res - i - 1))
bs.push_back(false);
else
bs.push_back(true);
}
else if (orientation == 3)
{
if ((i == 0) && ((res - j - 1) == res - 1))
continue;
if ((i == 0) && ((res - j - 1) == 0))
continue;
if ((i == res - 1) && ((res - j - 1) == 0))
continue;
if (marker_content.at<uchar>(i, res - j - 1))
bs.push_back(false);
else
bs.push_back(true);
}
}
}
id = bs.ulong();
return (true);
}
void MarkerArtoolkit::SetContent(unsigned long _id)
{
// Fill in the content values
margin_error = 0;
decode_error = 0;
id = _id;
Bitset bs;
bs.push_back_meaningful(_id);
for (int j = res - 1 ; j >= 0 ; j--)
{
for (int i = res - 1 ; i >= 0 ; i--)
{
if ((j == 0) && (i == 0))
marker_content.at<uchar>(j, i) = 0;
else if ((j == res-1) && (i == 0))
marker_content.at<uchar>(j, i) = 0;
else if ((j == res - 1) && (i == res - 1))
marker_content.at<uchar>(j, i) = 255;
else
{
if (bs.Length() && bs.pop_back())
marker_content.at<uchar>(j, i) = 0;
else
marker_content.at<uchar>(j, i) = 255;
}
}
}
return;
}
/*****************************************************************************
*** class MarkerData
****************************************************************************/
void MarkerData::DecodeOrientation(int& error, int& total, int& orientation)
{
vector<int> errors(4, 0);
int color = 255;
// Resolution identification
int j = res / 2;
for (int i = 0 ; i < res ; i++)
{
total++;
if (marker_content.at<uchar>(j, i) != color)
errors[0]++;
if (marker_content.at<uchar>(i, j) != color)
errors[1]++;
color = (color ? 0 : 255);
}
errors[2] = errors[0];
errors[3] = errors[1];
// Orientation identification
int i = res / 2;
for (j = (res / 2) - 2 ; j <= (res / 2) + 2 ; j++)
{
if (j < (res / 2))
{
total++;
if (marker_content.at<uchar>(j, i) != 0)
errors[0]++;
if (marker_content.at<uchar>(i, j) != 0)
errors[1]++;
if (marker_content.at<uchar>(j, i) != 255)
errors[2]++;
if (marker_content.at<uchar>(i, j) != 255)
errors[3]++;
}
else if (j > (res / 2))
{
total++;
if (marker_content.at<uchar>(j, i) != 255)
errors[0]++;
if (marker_content.at<uchar>(i, j) != 255)
errors[1]++;
if (marker_content.at<uchar>(j, i) != 0)
errors[2]++;
if (marker_content.at<uchar>(i, j) != 0)
errors[3]++;
}
}
orientation = min_element(errors.begin(), errors.end()) - errors.begin();
error = errors[orientation];
//*orientation = 0; // ttehop
return;
}
bool MarkerData::DetectResolution(vector<PointDouble>& _marker_corners_img, cv::Mat& gray, Camera& cam)
{
vector<PointDouble> marker_corners_img_undist;
marker_corners_img_undist.resize(_marker_corners_img.size());
copy(_marker_corners_img.begin(), _marker_corners_img.end(), marker_corners_img_undist.begin());
// line_points
std::vector<PointDouble> line_points;
PointDouble pt;
line_points.clear();
pt.x = 0;
pt.y = 0;
line_points.push_back(pt);
pt.x = -0.5 * edge_length;
pt.y = 0;
line_points.push_back(pt);
pt.x = +0.5 * edge_length;
pt.y = 0;
line_points.push_back(pt);
pt.x = 0;
pt.y = -0.5 * edge_length;
line_points.push_back(pt);
pt.x = 0;
pt.y = +0.5 * edge_length;
line_points.push_back(pt);
// Figure out the marker point position in the image
// TODO: Note that line iterator cannot iterate outside image
// therefore we need to distort the endpoints and iterate straight lines.
// Right way would be to iterate undistorted lines and distort line points.
Homography H;
vector<PointDouble> line_points_img(line_points.size());
line_points_img.resize(line_points.size());
cam.Undistort(marker_corners_img_undist);
H.Find(marker_corners, marker_corners_img_undist);
H.ProjectPoints(line_points, line_points_img);
cam.Distort(line_points_img);
// Now we have undistorted line end points
// Find lines and then distort the whole line
int white_count[4] = { 0, 0, 0, 0 }; // white counts for lines 1->0, 2->0, 3->0, 4->0
cv::Point2i pt2 = line_points_img[0];
if ((pt2.x < 0) || (pt2.y < 0) || (pt2.x >= gray.cols) || (pt2.y >= gray.rows))
{
return (false);
}
bool white = true;
for (int i = 0 ; i < 4 ; i++)
{
cv::Point2i pt1 = line_points_img[i + 1];
if ((pt1.x < 0) || (pt1.y < 0) || (pt1.x >= gray.cols) || (pt1.y >= gray.rows))
{
return (false);
}
cv::LineIterator iterator(gray, pt1, pt2, 8, false);
std::vector<uchar> vals;
for (int ii = 0 ; ii < iterator.count ; ii++)
{
iterator++;
vals.push_back(*(*iterator));
}
uchar vmin = *(std::min_element(vals.begin(), vals.end()));
uchar vmax = *(std::max_element(vals.begin(), vals.end()));
uchar thresh = (vmin + vmax) / 2;
white = true;
int bc = 0, wc = 0, N = 2;
for (uchar& val : vals)
{
// change the color status if we had
// N subsequent pixels of the other color
if (val < thresh)
{
bc++;
wc = 0;
}
else
{
wc++;
bc = 0;
}
if (white && (bc >= N))
{
white = false;
}
else if (!white && (wc >= N))
{
white = true;
white_count[i]++;
}
}
}
bool ret = true;
if ((white_count[0] + white_count[1]) == (white_count[2] + white_count[3]))
{
ret = false;
}
else if ((white_count[0] + white_count[1]) > (white_count[2] + white_count[3]))
{
if ((white_count[0] != white_count[1]) || (white_count[0] < 2))
{
ret = false;
} // end if
else
{
int nof_whites = white_count[0] * 2 - (white ? 1 : 0); // 'white' contains middle color
int new_res = 2 * nof_whites - 1;
SetMarkerSize(edge_length, new_res, margin);
} // end else
}
else
{
if ((white_count[2] != white_count[3]) || (white_count[2] < 2) || (((white_count[2] % 2) == 0) != white))
{