-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdlp4500.cpp
2261 lines (1931 loc) · 92.2 KB
/
dlp4500.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
#include "dlp4500.h"
dlpclass::dlpclass()
{
settings.Load("DLP_LightCrafter_4500_3D_Scan_Application_Config.txt");
//D://photo//src//
// 检索的设置
settings.Get(&algorithm_type);
settings.Get(&camera_type);
settings.Get(&connect_id_projector);
settings.Get(&connect_id_camera);
settings.Get(&config_file_projector);
settings.Get(&config_file_camera);
settings.Get(&config_file_calib_projector);
settings.Get(&config_file_calib_camera);
settings.Get(&config_file_geometry);
settings.Get(&config_file_structured_light_1);
settings.Get(&config_file_structured_light_2);
settings.Get(&continuous_scanning);
settings.Get(&calib_data_file_projector);
settings.Get(&calib_data_file_camera);
settings.Get(&dir_calib_data);
settings.Get(&dir_camera_calib_image_output);
settings.Get(&dir_system_calib_image_output);
settings.Get(&dir_scan_data_output);
settings.Get(&dir_scan_images_output);
settings.Get(&output_name_image_camera_calib_board);
settings.Get(&output_name_image_camera_calib);
settings.Get(&output_name_image_system_calib);
settings.Get(&output_name_image_depthmap);
settings.Get(&output_name_xyz_pointcloud);
}
dlpclass::~dlpclass()
{
}
bool dlpclass::verify()
{
// 验证支持列表中的摄像机和算法类型
if(camera_type.Get() != 2)
{ std::cout << "Unsupported CAMERA_TYPE set in the configuration file. Modify DLP_LightCrafter_3D_Scan_Application_Config.txt" << std::endl
<< "Press any key to exit..." << std::endl;
std::cin.get();
return 0;
}
if(algorithm_type.Get() > 1)
{ std::cout << "Unsupported ALGORITHM_TYPE set in the configuration file. Modify DLP_LightCrafter_3D_Scan_Application_Config.txt" << std::endl
<< "Press any key to exit..." << std::endl;
std::cin.get();
return 0;
}
//连接投影仪
ret = dlp::DLP_Platform::ConnectSetup(projector,connect_id_projector.Get(),config_file_projector.Get(),true);
if(ret.hasErrors())
{ std::cout << "\n\nPlease resolve the LightCrafter connection issue before proceeding to next step..." << std::endl;
return 0;}
//根据用户选择连接相机
if(camera_type.Get() == 2)
{
ret = dlp::Camera::ConnectSetup(camera_ds,connect_id_camera.Get(),config_file_camera.Get(),true);
std::cout << "ret = " << ret << std::endl ;
if(ret.hasErrors())
{ std::cout << "\n\nPlease resolve the camera connection issue before proceeding to next step..." << std::endl << ret.ToString () << std::endl;
return 0;}
}
else
{ std::cout << "error :dlpclass::verifystd::" << std::endl;// 执行不到的代码
return 0;}
return 1;
}
/*
0:退出
1:生成相机校准板,并输入特征测量值
2:准备DLP LightCrafter 4500(每个投影仪一次)
3:准备校准和扫描系统
4:校准相机
5:校准系统
6:执行扫描(仅垂直模式)
7:执行扫描(仅水平模式)
8:执行扫描(垂直和水平模式)
9.重新连接照相机和投影仪
*/
void dlpclass::menu(int menu_select)
{
if(camera_type.Get() == 2)
{
switch(menu_select){
case 0:
break;
//1:生成相机校准板,并输入特征测量值
case 1:
{
// GenerateCameraCalibrationBoard(10);
GenerateCameraCalibrationBoard(&camera_ds,
config_file_calib_camera.Get(),
dir_camera_calib_image_output.Get() + output_name_image_camera_calib_board.Get() + ".bmp");
//"D://photo//camera_calibration_board//camera_calibration_board.bmp"
break;
}
//2:准备DLP LightCrafter 4500(每个投影仪一次)
case 2:
{
PrepareProjectorPatterns(&projector,
config_file_calib_projector.Get(),
&algo_three_phase_vert,
config_file_structured_light_1.Get(),
&algo_three_phase_horz,
config_file_structured_light_2.Get(),
false, // 固件将上传
&total_pattern_count);
break;
}
//3:准备校准和扫描系统
case 3:
{
PrepareProjectorPatterns(&projector,
config_file_calib_projector.Get(),
&algo_gray_code_vert,
config_file_structured_light_1.Get(),
&algo_gray_code_horz,
config_file_structured_light_2.Get(),
true, // 固件将不会被上传
&total_pattern_count);
break;
}
//4:校准相机
case 4:
{
CalibrateCamera(&camera_ds,
config_file_calib_camera.Get(),
calib_data_file_camera.Get(),
dir_camera_calib_image_output.Get() +
output_name_image_camera_calib.Get(),
&projector);
break;
}
//5:校准系统
case 5:
{
CalibrateSystem(&camera_ds,
config_file_calib_camera.Get(),
calib_data_file_camera.Get(),
&projector,
config_file_calib_projector.Get(),
calib_data_file_projector.Get(),
dir_system_calib_image_output.Get() +
output_name_image_system_calib.Get());
break;
}
//6:执行扫描(仅垂直模式)
case 6:
{
if(algorithm_type.Get() == 0)
ScanObject(&camera_ds,
false,
calib_data_file_camera.Get(),
&projector,
calib_data_file_projector.Get(),
&algo_gray_code_vert,
&algo_gray_code_horz,
true,
false,
config_file_geometry.Get(),
continuous_scanning.Get());
else if(algorithm_type.Get() == 1)
ScanObject(&camera_ds,
false,
calib_data_file_camera.Get(),
&projector,
calib_data_file_projector.Get(),
&algo_three_phase_vert,
&algo_three_phase_horz,
true,
false,
config_file_geometry.Get(),
continuous_scanning.Get());
break;
}
//7:执行扫描(仅水平模式)
case 7:
{
if(algorithm_type.Get() == 0)
ScanObject(&camera_ds,
false,
calib_data_file_camera.Get(),
&projector,
calib_data_file_projector.Get(),
&algo_gray_code_vert,
&algo_gray_code_horz,
false,
true,
config_file_geometry.Get(),
continuous_scanning.Get());
else if(algorithm_type.Get() == 1)
ScanObject(&camera_ds,
false,
calib_data_file_camera.Get(),
&projector,
calib_data_file_projector.Get(),
&algo_three_phase_vert,
&algo_three_phase_horz,
false,
true,
config_file_geometry.Get(),
continuous_scanning.Get());
break;
}
//8:执行扫描(垂直和水平模式)
case 8:
{
if(algorithm_type.Get() == 0)
ScanObject(&camera_ds,
false,
calib_data_file_camera.Get(),
&projector,
calib_data_file_projector.Get(),
&algo_gray_code_vert,
&algo_gray_code_horz,
true,
true,
config_file_geometry.Get(),
continuous_scanning.Get());
else if(algorithm_type.Get() == 1)
ScanObject(&camera_ds,
false,
calib_data_file_camera.Get(),
&projector,
calib_data_file_projector.Get(),
&algo_three_phase_vert,
&algo_three_phase_horz,
true,
true,
config_file_geometry.Get(),
continuous_scanning.Get());
break;
}
//9.重新连接照相机和投影仪
case 9:
{
camera_ds.Disconnect();
projector.Disconnect();
dlp::DLP_Platform::ConnectSetup(projector,connect_id_projector.Get(),config_file_projector.Get(),true);
dlp::Camera::ConnectSetup(camera_ds,connect_id_camera.Get(),config_file_camera.Get(),true);
break;
}
//断连
case 10:
{
camera_ds.Disconnect();
projector.Disconnect();
break;
}
//Video
case 11:
{
Video(&camera_ds);
break;
}
default:
{
std::cout << "Invalid menu selection! \n" << std::endl;
break;
}
}
}
}
//1:生成相机校准板,并输入特征测量值
void dlpclass::GenerateCameraCalibrationBoard(double side_length)
{
std::cout << std::endl << "<<<<<<<<<<<<<<<<<<<<<< Generate Camera Calibration Board >>>>>>>>>>>>>>>>>>>>>>" << std::endl;
dlp::Camera *camera = &camera_ds;
const std::string &camera_calib_settings_file = config_file_calib_camera.Get();
const std::string &camera_calib_board_name = "D://photo//camera_calibration_board//camera_calibration_board.bmp";
// 摄像机标定的变量
dlp::Calibration::Camera camera_calib;
dlp::Parameters camera_calib_settings;
dlp::Image camera_calib_board_image;
// 检查相机是否为空
if(!camera) return;
// 负载校准设置
std::cout << "Loading camera calibration settings..." << std::endl;
if(camera_calib_settings.Load(camera_calib_settings_file).hasErrors())
{
//相机校准设置文件未加载
std::cout << "Could NOT load camera calibration settings!" << std::endl << ret.ToString() << std::endl;
return;}
//设置当前相机的相机校准
camera_calib.SetCamera((*camera));
std::cout << "Setting up camera calibration..." << std::endl;
ret = camera_calib.Setup(camera_calib_settings);
if(ret.hasErrors())
{
std::cout << "Camera calibration setup FAILED!" << std::endl << ret.ToString() << std::endl;
return;}
// 生成并保存校准图像
std::cout << "Generating camera calibration board..." << std::endl;
camera_calib.GenerateCalibrationBoard(&camera_calib_board_image);
// 保存校准板
std::cout << "Saving calibration board..." << std::endl;
camera_calib_board_image.Save(camera_calib_board_name);
std::cout << "\nPlease print the camera calibration board and attach it to a flat surface The calibration board image can be found in: "
<< camera_calib_board_name <<
"\n\nOnce the calibration image has been printed and attached to a flat surface, measure the size of the square on the board \n \
\nNOTE: Enter the measurement in the units desired for the point cloud (i.e. mm, in, cm, etc.)\n \
\nNOTE: Both camera and system calibrations must be redone! " << std::endl;
// 检索测量
std::cout << "Enter the length of the square (do NOT include units): " << std::endl;
std::cout << "Saving feature distances to calibration settings file..."<<std::endl;
camera_calib_settings.Set(dlp::Calibration::Parameters::BoardFeatureRowDistance(side_length));
camera_calib_settings.Set(dlp::Calibration::Parameters::BoardFeatureColumnDistance(side_length));
camera_calib_settings.Save(camera_calib_settings_file);
}
void dlpclass::GenerateCameraCalibrationBoard(dlp::Camera *camera, //相机
const std::string &camera_calib_settings_file, //相机校准设置
const std::string &camera_calib_board_name) //校准板保存文件位置
{
std::cout << std::endl << "<<<<<<<<<<<<<<<<<<<<<< Generate Camera Calibration Board >>>>>>>>>>>>>>>>>>>>>>" << std::endl;
// 摄像机标定的变量
dlp::Calibration::Camera camera_calib;
dlp::Parameters camera_calib_settings;
dlp::Image camera_calib_board_image;
// 检查相机是否为空
if(!camera) return;
// 负载校准设置
std::cout << "Loading camera calibration settings..." << std::endl;
if(camera_calib_settings.Load(camera_calib_settings_file).hasErrors())
{
//相机校准设置文件未加载
std::cout << "Could NOT load camera calibration settings!" << std::endl << ret.ToString() << std::endl;
return;}
//设置当前相机的相机校准
camera_calib.SetCamera((*camera));
std::cout << "Setting up camera calibration..." << std::endl;
ret = camera_calib.Setup(camera_calib_settings);
if(ret.hasErrors())
{
std::cout << "Camera calibration setup FAILED!" << std::endl << ret.ToString() << std::endl;
return;}
// 生成并保存校准图像
std::cout << "Generating camera calibration board..." << std::endl;
camera_calib.GenerateCalibrationBoard(&camera_calib_board_image);
// 保存校准板
std::cout << "Saving calibration board..." << std::endl;
camera_calib_board_image.Save(camera_calib_board_name);
// 等待用户打印校准板
std::cout << "\nPlease print the camera calibration board and attach it to a flat surface The calibration board image can be found in: "
<< camera_calib_board_name
<< "\n\nOnce the calibration image has been printed and attached to a flat surface, measure the size of the square on the board \n \
\nNOTE: Enter the measurement in the units desired for the point cloud (i.e. mm, in, cm, etc.)\n \
\nNOTE: Both camera and system calibrations must be redone! " << std::endl;
// 检索测量
std::cout << "Enter the length of the square (do NOT include units): " ;
//std::cin >> side_length;
std::cout << std::endl;
std::cout << "Saving feature distances to calibration settings file..."<<std::endl;
camera_calib_settings.Set(dlp::Calibration::Parameters::BoardFeatureRowDistance(side_length));
camera_calib_settings.Set(dlp::Calibration::Parameters::BoardFeatureColumnDistance(side_length));
camera_calib_settings.Save(camera_calib_settings_file);
}
//2:准备DLP LightCrafter 4500(每个投影仪一次) previously_prepared=false
//3:准备校准和扫描系统 previously_prepared=true
void dlpclass::PrepareProjectorPatterns(dlp::DLP_Platform *projector,
const std::string &projector_calib_settings_file,
dlp::StructuredLight *structured_light_vertical,
const std::string &structured_light_vertical_settings_file,
dlp::StructuredLight *structured_light_horizontal,
const std::string &structured_light_horizontal_settings_file,
const bool previously_prepared,
unsigned int *total_pattern_count)
{
std::cout << std::endl << std::endl << "<<<<<<<<<<<<<<<<<<<<<< Load DLP LightCrafter 4500 Firmware >>>>>>>>>>>>>>>>>>>>>>" << std::endl;
// 投影仪标定变量
dlp::Calibration::Projector projector_calib;
dlp::Parameters projector_calib_settings;
// 结构光序列
dlp::Pattern::Sequence calibration_patterns;
dlp::Pattern::Sequence vertical_patterns;
dlp::Pattern::Sequence horizontal_patterns;
dlp::Pattern::Sequence all_patterns;
// 结构光的设置
dlp::Parameters structured_light_vertical_settings;
dlp::Parameters structured_light_horizontal_settings;
projector_calib.SetDebugEnable(false);
// 检查投影仪是否无效
if(!projector) return;
// 检查投影仪是否连接好
if(!projector->isConnected())
{
std::cout << "Projector NOT connected!" << std::endl;
return;
}
// 投影仪校准设置
std::cout << "Loading projector calibration settings..." << std::endl;
if(projector_calib_settings.Load(projector_calib_settings_file).hasErrors())
{
//投影仪校准设置文件未加载
std::cout << "Could NOT load projector calibration settings!" << std::endl << ret.ToString() << std::endl;
return;
}
//设置投影仪校准为当前投影仪
projector_calib.SetDlpPlatform((*projector));
// 添加临时相机分辨率设置
projector_calib_settings.Set(dlp::Calibration::Parameters::ImageColumns(1000));
projector_calib_settings.Set(dlp::Calibration::Parameters::ImageRows(1000));
std::cout << "Setting up projector calibration..." << std::endl;
ret = projector_calib.Setup(projector_calib_settings);
if(ret.hasErrors())
{
std::cout << "Projector calibration setup FAILED: " << std::endl << ret.ToString() << std::endl;
return;
}
//创建校准图像并转换为单色
std::cout << "Generating projector calibration board..." << std::endl;
dlp::Image projector_calibration_board_image;
projector_calib.GenerateCalibrationBoard(&projector_calibration_board_image);
projector_calibration_board_image.ConvertToMonochrome();
// 创建校准模式
std::cout << "Generating projector calibration pattern..." << std::endl;
dlp::Pattern calib_pattern;
calib_pattern.bitdepth = dlp::Pattern::Bitdepth::MONO_1BPP;
calib_pattern.color = dlp::Pattern::Color::WHITE;
calib_pattern.data_type = dlp::Pattern::DataType::IMAGE_DATA;
calib_pattern.image_data.Create(projector_calibration_board_image);
// 将校准模式添加到校准模式序列中
calibration_patterns.Add(calib_pattern);
// 设置结构光模式
if(!structured_light_vertical) return;
if(!structured_light_horizontal) return;
// 加载垂直结构光设置
std::cout << "Loading vertical structured light settings..." << std::endl;
if(structured_light_vertical_settings.Load(structured_light_vertical_settings_file).hasErrors())
{
// 结构化光设置文件未加载
std::cout << "Structured light settings did NOT load successfully" << std::endl;
return;
}
// 加载水平结构光设置
std::cout << "Loading horizontal structured light settings..." << std::endl;
if(structured_light_horizontal_settings.Load(structured_light_horizontal_settings_file).hasErrors()){
//结构化光设置文件未加载
std::cout << "Structured light settings did NOT load successfully" << std::endl;
return;
}
structured_light_horizontal->SetDlpPlatform((*projector));
structured_light_vertical->SetDlpPlatform((*projector));
std::cout << "Setting up vertical structured light module..." << std::endl <<
structured_light_vertical->Setup(structured_light_vertical_settings).ToString() << std::endl <<
"Setting up horizontal structured light module..." << std::endl <<
structured_light_horizontal->Setup(structured_light_horizontal_settings).ToString() << std::endl;
// 生成模式序列
std::cout << "Generating vertical structured light module patterns..." << std::endl;
structured_light_vertical->GeneratePatternSequence(&vertical_patterns);
std::cout << "Generating horizontal structured light module patterns..." << std::endl;
structured_light_horizontal->GeneratePatternSequence(&horizontal_patterns);
// 组合所有模式序列
std::cout << "Combining all patterns for projector..." << std::endl;
all_patterns.Add(calibration_patterns);
all_patterns.Add(vertical_patterns);
all_patterns.Add(horizontal_patterns);
//如果previously_prepared是假的,模块应该假设投影仪之前没有准备好
//这对于LightCrafter 4500很重要,因为映像存储在固件中
//因此固件必须上传准备投影仪,但这只需要发生一次
dlp::Parameters force_preparation;
force_preparation.Set(dlp::DLP_Platform::Parameters::SequencePrepared(previously_prepared));
projector->Setup(force_preparation);
//如果需要上传固件,则显示调试消息以便观察进程
projector->SetDebugEnable(!previously_prepared);
// 准备投影仪
std::cout << "Preparing projector for calibration and structured light..." << std::endl;
ret = projector->PreparePatternSequence(all_patterns);
projector->SetDebugEnable(false);
if( ret.hasErrors()){
std::cout << "Projector prepare sequence FAILED: " << std::endl << ret.ToString() << std::endl;
(*total_pattern_count) = 0;
}
std::cout << "Projector prepared" << std::endl;
(*total_pattern_count) = all_patterns.GetCount();
}
//4:校准相机
void dlpclass::CalibrateCamera(dlp::Camera *camera,
const std::string &camera_calib_settings_file,
const std::string &camera_calib_data_file,
const std::string &camera_calib_image_file_names,
dlp::DLP_Platform *projector)
{
std::cout << std::endl << std::endl << "<<<<<<<<<<<<<<<<<<<<<< Calibrating the Camera >>>>>>>>>>>>>>>>>>>>>>" << std::endl;
// 摄像机标定的变量
dlp::Calibration::Camera camera_calib;
dlp::Calibration::Data camera_calib_data;
dlp::Parameters camera_calib_settings;
// 检查相机是否为空
if(!camera) return;
// 检查摄像头是否连接好
if(!camera->isConnected())
{
std::cout << "Camera NOT connected!" << std::endl;
return;
}
// 检查投影仪是否无效
if(!projector) return;
// 检查投影仪是否连接好
if(!projector->isConnected())
{
std::cout << "Projector NOT connected!" << std::endl;
return;
}
// 负载校准设置
std::cout << "Loading camera calibration settings..." << std::endl;
if(camera_calib_settings.Load(camera_calib_settings_file).hasErrors())
{
// 相机校准设置文件未加载
std::cout << "Could NOT load camera calibration settings!" << std::endl << ret.ToString() << std::endl;
return;
}
// 设置当前相机的相机校准
camera_calib.SetCamera((*camera));
std::cout << "Setting up camera calibration" << std::endl;
ret = camera_calib.Setup(camera_calib_settings);
if(ret.hasErrors())
{
std::cout << "Camera calibration setup FAILED: " << std::endl << ret.ToString() << std::endl;
return;
}
// 投射一个白色图案来照亮校准板
std::cout << "Projecting white pattern to illuminate calibration board" << std::endl;
projector->ProjectSolidWhitePattern();
// 获取所需校准图像的数量
unsigned int boards_success;
unsigned int boards_required;
camera_calib.GetCalibrationProgress(&boards_success,&boards_required);
std::cout << "Calibrating camera..." << std::endl <<
"Calibration boards required = " << boards_required << std::endl <<
"Calibration boards captured = " << boards_success << std::endl;
// 显示校准说明
std::cout << "\nRead the following instructions in their entirety!" << std::endl <<
"Camera calibration instructions:" << std::endl <<
"1. After the live view has started, place the calibration board so it is completely visible within the camera frame and is stationary" << std::endl <<
" - The board should be placed at the desired scanning distance illuminated and sharp" << std::endl <<
" - Lock the aperture and focus in place if possible" << std::endl <<
" - If the camera focus or aperture is adjusted, the calibration must be redone" << std::endl <<
"3. Select the live view window and press the SPACE_BAR key to capture the calibration board's current position" << std::endl <<
"4. Move the calibration board and repeat step 3 until calibration is complete" << std::endl <<
" - Capture images with the board at different distances from the camera" << std::endl <<
" - Capture images with the board at different positions in the camera frame" << std::endl <<
" - The position of the camera can be changed as well rather than moving the board" << std::endl <<
"\nNOTE: Press ESC key to quit the calibration routine" << std::endl;
dlp::CmdLine::PressEnterToContinue("Press ENTER after reading the above instructions...");
// 如果还没有启动,请启动相机
if(!camera->isStarted()){
if(camera->Start().hasErrors()){
dlp::CmdLine::Print("Could NOT start camera! \n");
return;
}
else{
dlp::CmdLine::Print("Started camera. \n");
}
}
// 给相机一些时间来填充图像缓冲区
dlp::Time::Sleep::Milliseconds(50);
//开始捕捉图像进行相机校准
dlp::Image camera_printed_board;
dlp::Image::Window camera_view;
// 打开相机视图
camera_view.Open("Camera Calibration - press SPACE to capture or ESC to exit");
// 捕获图像,直到足够多的板已成功添加
while(boards_success < boards_required){
std::cout << "boards_success : " << boards_success << " boards_required : " << boards_required << std::endl;
// 等待空格键添加校准图像
unsigned int return_key = 0;
while(return_key != ' '){
camera_printed_board.Clear(); // 清除图像对象
camera->GetFrame(&camera_printed_board); // 捕获最新的相框
camera_view.Update(camera_printed_board); // 显示图像
camera_view.WaitForKey(16,&return_key); // 等待按键被按下或50ms超时
if(return_key == 27) break; // ESC键被按下
}
// ESC键被按下
if(return_key == 27){
// 关闭图像窗口
camera_view.Close();
// 停止相机捕捉
if(camera->Stop().hasErrors()){
std::cout << "Camera failed to stop! Exiting calibration routine..." << std::endl;
}
// 退出校准功能
std::cout << "Exiting camera calibration..." << std::endl;
return;
}
// 添加校准板图像
bool success = false;
camera_printed_board.ConvertToMonochrome(); // 转换图像为单色
camera_calib.AddCalibrationBoard(camera_printed_board,&success);
// 更新状态
camera_calib.GetCalibrationProgress(&boards_success,&boards_required);
// 显示消息
if(success){
std::cout << "Calibration board successfully added! Captured " << dlp::Number::ToString(boards_success) << " of " << dlp::Number::ToString(boards_required) << std::endl;
camera_printed_board.Save(camera_calib_image_file_names + dlp::Number::ToString(boards_success) + ".bmp");
}
else{
std::cout << "\nCalibration board NOT found! Please check the following:" << std::endl <<
"- All internal corners of the calibration board are visible within the camera frame" << std::endl <<
"- The calibration board does not have any glare" << std::endl <<
"- The calibration board is well illuminated" << std::endl <<
"- The calibration board is in focus" << std::endl;
}
}
// 释放图像内存
camera_printed_board.Clear();
// 校准相机
double reprojection_error;
ret = camera_calib.Calibrate(&reprojection_error);
if(!ret.hasErrors()){
std::cout << "Camera was successfully calibrated!" << std::endl <<
"Reprojection error (closer to zero is better) = " << reprojection_error << std::endl;
// 获取摄像机校准
camera_calib.GetCalibrationData(&camera_calib_data);
// 保存相机校准数据
camera_calib_data.Save(camera_calib_data_file);
std::cout << "Camera calibration data saved to :" << camera_calib_data_file << std::endl;
}
else{
std::cout << "Camera calibration FAILED!" << std::endl;
}
return;
}
//5:校准系统
void dlpclass::CalibrateSystem(dlp::Camera *camera,
const std::string &camera_calib_settings_file,
const std::string &camera_calib_data_file,
dlp::DLP_Platform *projector,
const std::string &projector_calib_settings_file,
const std::string &projector_calib_data_file,
const std::string &calib_image_file_names)
{
// 摄像机标定的变量
dlp::Calibration::Camera camera_calib;
dlp::Calibration::Data camera_calib_data;
dlp::Parameters camera_calib_settings;
// 投影仪标定变量
dlp::Calibration::Projector projector_calib;
dlp::Calibration::Data projector_calib_data;
dlp::Parameters projector_calib_settings;
camera_calib.SetDebugEnable(false);
projector_calib.SetDebugEnable(false);
// 检查相机是否为空
if(!camera) return;
// 检查摄像头是否连接好
if(!camera->isConnected())
{
std::cout << "Camera NOT connected! \n" << std::endl;
return;
}
// 检查投影仪是否无效
if(!projector) return;
// 检查投影仪是否连接好
if(!projector->isConnected())
{
std::cout << "Projector NOT connected! \n" << std::endl;
return;
}
// 检查相机校准数据
bool camera_calib_exists = true;
camera_calib_data.Load(camera_calib_data_file);
if(camera_calib.SetCalibrationData(camera_calib_data).hasErrors()) camera_calib_exists = false;
// 相机校准设置
std::cout << "Loading camera calibration settings..." << std::endl;
if(camera_calib_settings.Load(camera_calib_settings_file).hasErrors())
{
// 相机校准设置文件未加载
std::cout << "Could NOT load camera calibration settings!" << std::endl << ret.ToString() << std::endl;
return;
}
// 投影仪校准设置
std::cout << "Loading projector calibration settings..." << std::endl;
if(projector_calib_settings.Load(projector_calib_settings_file).hasErrors())
{
// 投影仪校准设置文件未加载
std::cout << "Could NOT load projector calibration settings!" << std::endl <<ret.ToString() << std::endl;
return;
}
//用投影仪所需的数字调整相机校准板
dlp::Calibration::Parameters::BoardCount projector_boards_required;
projector_calib_settings.Get(&projector_boards_required);
camera_calib_settings.Set(projector_boards_required);
// 设置当前相机的相机校准
camera_calib.SetCamera((*camera));
projector_calib.SetCamera((*camera));
projector_calib.SetDlpPlatform((*projector));
std::cout << "Setting up camera calibration..." << std::endl;
ret = camera_calib.Setup(camera_calib_settings);
if(ret.hasErrors())
{
std::cout << "Camera calibration setup FAILED: " << std::endl << ret.ToString() << std::endl;
return;
}
std::cout << "Setting up projector calibration..." << std::endl;
ret = projector_calib.Setup(projector_calib_settings);
if(ret.hasErrors())
{
std::cout << "Projector calibration setup FAILED: " << std::endl << ret.ToString() << std::endl;
return;
}
// 如果还没有启动,请启动相机
if(!camera->isStarted())
{
if(camera->Start().hasErrors())
{
std::cout << "Could NOT start camera! " << std::endl;
return;
}
}
// 项目校准模式聚焦投影仪
ret = projector->DisplayPatternInSequence(0,true);
if(ret.hasErrors())
{
std::cout << ret.ToString() << std::endl;
return;
}
dlp::Time::Sleep::Milliseconds(100);
std::cout << std::endl <<"Place the calibration board at the desired scanning distance and focus the projector..." << std::endl << std::endl;
// 获取所需校准图像的数量
unsigned int cam_boards_success;
unsigned int cam_boards_required;
unsigned int proj_boards_success;
unsigned int proj_boards_required;
camera_calib.GetCalibrationProgress(&cam_boards_success,&cam_boards_required);
projector_calib.GetCalibrationProgress(&proj_boards_success,&proj_boards_required);
// 显示校准说明
std::cout << "Read the following instructions in their entirety!" << std::endl <<
"System calibration instructions:" << std::endl <<
"1. After the live view has started, place the calibration board at the furthest desired scanning distance" << std::endl <<
" - The entire projection must fit on the calibration board!" << std::endl <<
"2. Place the camera in a position so that the projection is completely visible within the camera frame" << std::endl <<
" - The board should be placed at the desired scanning distance" << std::endl <<
"3. Lock the projector and camera in place relative to each other if possible" << std::endl <<
" - If the camera or projector move relative to each other, the system calibration must be redone" << std::endl <<
"4. Select the live view window and press the SPACE_BAR key to capture the calibration board's current position" << std::endl <<
"5. Move the calibration board and repeat step 4 until calibration is complete" << std::endl <<
" - Capture images with the board at different distances from the projector" << std::endl <<
" - The projected pattern must always fit on the calibration board" << std::endl <<
" - Both the projected pattern and printed pattern must be viewable by the camera" << std::endl <<
" - The position of the camera and projector relative to each other should NOT be changed!" << std::endl;
dlp::CmdLine::PressEnterToContinue("\nNOTE: Press ESC key to quit the calibration routine\n");
// 开始捕捉图像进行相机校准
dlp::Image camera_printed_board;
dlp::Image projector_camera_combo;
dlp::Image::Window camera_view;
unsigned int return_key = 0;
// 打开相机实时视图
camera_view.Open("System Calibration - press SPACE to capture or ESC to exit");
while(proj_boards_success < proj_boards_required)
{
// 先拿相机板
while(cam_boards_success < cam_boards_required)
{
// 投影白色图案,照亮相机校准板
projector->ProjectSolidWhitePattern();
dlp::Time::Sleep::Milliseconds(250);
// 等待空格键添加校准图像
while(return_key != ' ')
{
camera_printed_board.Clear();
camera->GetFrame(&camera_printed_board);
camera_view.Update(camera_printed_board);
camera_view.WaitForKey(16,&return_key);
if(return_key == 27) break;
}
if(return_key == 27){
camera_view.Close();
if(camera->Stop().hasErrors())
std::cout << "Camera failed to stop!" << std::endl;
std:: cout <<"Exiting calibration..." << std::endl;
return;
}
// 重置返回键
return_key = 0;
// 添加校准板图像
bool success;
camera_printed_board.ConvertToMonochrome();
camera_calib.AddCalibrationBoard(camera_printed_board,&success);
// 更新状态
camera_calib.GetCalibrationProgress(&cam_boards_success,&cam_boards_required);
// 显示消息
if(success)
{
std::cout <<"Camera calibration board successfully added! Captured " + dlp::Number::ToString(cam_boards_success) + " of " + dlp::Number::ToString(cam_boards_required) << std::endl;
camera_printed_board.Save(calib_image_file_names + "camera_" + dlp::Number::ToString(cam_boards_success) + ".bmp");
// 现在就把投影仪校准好
projector->DisplayPatternInSequence(0,true);
dlp::Time::Sleep::Milliseconds(250);
camera->GetFrame(&projector_camera_combo);
camera_view.Update(projector_camera_combo);
camera_view.WaitForKey(1,&return_key);
dlp::Image projector_black;
projector->ProjectSolidBlackPattern();
dlp::Time::Sleep::Milliseconds(250);
camera->GetFrame(&projector_black);
camera_view.Update(projector_black);
camera_view.WaitForKey(1,&return_key);
// 添加打印板和组合板
dlp::Image projector_pattern;
projector_camera_combo.ConvertToMonochrome();
projector_black.ConvertToMonochrome();
projector_calib.RemovePrinted_AddProjectedBoard(camera_printed_board,
projector_black,
projector_camera_combo,
&projector_pattern,
&success);
projector_black.Clear();
// 更新状态
projector_calib.GetCalibrationProgress(&proj_boards_success,&proj_boards_required);
if(success)
{
std::cout << "Projector calibration board successfully added! Captured " + dlp::Number::ToString(proj_boards_success) + " of " + dlp::Number::ToString(proj_boards_required) << std::endl;
projector_pattern.Save(calib_image_file_names + "projector_pattern_" + dlp::Number::ToString(proj_boards_success) +".bmp");
projector_camera_combo.Save(calib_image_file_names + "projector_camera_combo_" + dlp::Number::ToString(proj_boards_success) +".bmp");
}
else
{
std::cout << "Projector calibration board NOT found! Please check the following:" << std::endl <<
"- All internal corners of the projected calibration board are visible within the camera frame" << std::endl <<
"- The projector calibration board does NOT fall off of the printed calibration board" << std::endl <<
"- The calibration board does not have any glare" << std::endl <<
"- The calibration board is well illuminated" << std::endl <<
"- The calibration board is in focus" << std::endl;
// 删除最近添加的相机校准板
camera_calib.RemoveLastCalibrationBoard();
camera_calib.GetCalibrationProgress(&cam_boards_success,&cam_boards_required);
}
}
else
std::cout << "Calibration board NOT found! Please check the following:" << std::endl <<
"- All internal corners of the calibration board are visible within the camera frame" << std::endl <<
"- The calibration board does not have any glare" << std::endl <<
"- The calibration board is well illuminated" << std::endl <<
"- The calibration board is in focus" << std::endl;
}
}
// 释放图像内存
camera_printed_board.Clear();
// 关闭窗口
camera_view.Close();
// 校准相机
double error;
int camera_calib_option = 0;