/
ArUcoCameraCalibrationExample.cs
1516 lines (1284 loc) · 55.9 KB
/
ArUcoCameraCalibrationExample.cs
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
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Linq;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.Calib3dModule;
using OpenCVForUnity.UnityUtils;
using OpenCVForUnity.ImgcodecsModule;
using OpenCVForUnity.UnityUtils.Helper;
using OpenCVForUnity.ObjdetectModule;
using OpenCVForUnity.ArucoModule;
namespace OpenCVForUnityExample
{
/// <summary>
/// ArUco Camera Calibration Example
/// An example of camera calibration using the objdetect module. (ChessBoard, CirclesGlid, AsymmetricCirclesGlid and ChArUcoBoard)
/// Referring to https://docs.opencv.org/master/d4/d94/tutorial_camera_calibration.html.
/// https://github.com/opencv/opencv/blob/master/samples/cpp/tutorial_code/calib3d/camera_calibration/camera_calibration.cpp
/// https://docs.opencv.org/3.4.0/d7/d21/tutorial_interactive_calibration.html
/// https://github.com/opencv/opencv/tree/master/apps/interactive-calibration
/// https://docs.opencv.org/3.2.0/da/d13/tutorial_aruco_calibration.html
/// https://github.com/opencv/opencv_contrib/blob/master/modules/aruco/samples/calibrate_camera_charuco.cpp
/// </summary>
[RequireComponent(typeof(WebCamTextureToMatHelper))]
public class ArUcoCameraCalibrationExample : MonoBehaviour
{
/// <summary>
/// The marker type used for calibration.
/// </summary>
[Tooltip("The marker type used for calibration.")]
public MarkerType markerType = MarkerType.ChessBoard;
/// <summary>
/// The marker type dropdown.
/// </summary>
public Dropdown markerTypeDropdown;
/// <summary>
/// Number of inner corners per a item column. (square, circle)
/// </summary>
[Tooltip("Number of inner corners per a item column. (square, circle)")]
public NumberOfBoardSizeWidth boardSizeW = NumberOfBoardSizeWidth.W_9;
/// <summary>
/// The board size W dropdown.
/// </summary>
public Dropdown boardSizeWDropdown;
/// <summary>
/// Number of inner corners per a item row. (square, circle)
/// </summary>
[Tooltip("Number of inner corners per a item row. (square, circle)")]
public NumberOfBoardSizeHeight boardSizeH = NumberOfBoardSizeHeight.H_6;
/// <summary>
/// The board size H dropdown.
/// </summary>
public Dropdown boardSizeHDropdown;
/// <summary>
/// The save path input field.
/// </summary>
public InputField savePathInputField;
/// <summary>
/// The show undistort image.
/// </summary>
public bool showUndistortImage = true;
/// <summary>
/// The show undistort image toggle.
/// </summary>
public Toggle showUndistortImageToggle;
[Header("Normal Calibration Option")]
/// <summary>
/// The normal calibration options group.
/// </summary>
public GameObject normalCalibrationOptionsGroup;
/// <summary>
/// The size of a square in some user defined metric system (pixel, millimeter)
/// </summary>
[Tooltip("The size of a square in some user defined metric system (pixel, millimeter)")]
public float squareSize = 50f;
/// <summary>
/// The square size input field.
/// </summary>
public InputField squareSizeInputField;
/// <summary>
/// If your calibration board is inaccurate, unmeasured, roughly planar targets
/// (Checkerboard patterns on paper using off-the-shelf printers are the most convenient calibration targets and most of them are not accurate enough.),
/// a method from [219] can be utilized to dramatically improve the accuracies of the estimated camera intrinsic parameters.
/// Need to set the measured values from the actual chess board to "squareSize" and "gridWidth".
/// https://docs.opencv.org/4.2.0/d9/d0c/group__calib3d.html#ga11eeb16e5a458e1ed382fb27f585b753
/// </summary>
[Tooltip("If your calibration board is inaccurate, unmeasured, roughly planar targets (Checkerboard patterns on paper using off-the-shelf printers are the most convenient calibration targets and most of them are not accurate enough.), a method from [219] can be utilized to dramatically improve the accuracies of the estimated camera intrinsic parameters. Need to set the measured values from the actual chess board to \"squareSize\" and \"gridWidth\".")]
public bool useNewCalibrationMethod = true;
/// <summary>
/// The use new calibration method toggle.
/// </summary>
public Toggle useNewCalibrationMethodToggle;
/// <summary>
/// The measured distance between top-left (0, 0, 0) and top-right (squareSize*(boardSizeW - 1), 0, 0) corners of the pattern grid points.
/// </summary>
[Tooltip("The measured distance between top-left (0, 0, 0) and top-right (squareSize*(boardSizeW - 1), 0, 0) corners of the pattern grid points.")]
public float gridWidth = 400f;
/// <summary>
/// The glid width input field.
/// </summary>
public InputField gridWidthInputField;
/// <summary>
/// Determines if use findChessboardCornersSB method. (More accurate than the findChessboardCorners and cornerSubPix methods)
/// https://docs.opencv.org/4.2.0/d9/d0c/group__calib3d.html#gad0e88e13cd3d410870a99927510d7f91
/// </summary>
[Tooltip("Determines if use findChessboardCornersSB method. (More accurate than the findChessboardCorners and cornerSubPix methods)")]
public bool useFindChessboardCornersSBMethod = true;
/// <summary>
/// Determines if enable CornerSubPix method. (Improve the found corners' coordinate accuracy for chessboard)
/// </summary>
[Tooltip("Determines if enable CornerSubPix method. (Improve the found corners' coordinate accuracy for chessboard)")]
public bool enableCornerSubPix = true;
[Header("ArUco Calibration Option")]
/// <summary>
/// The arUco calibration options group.
/// </summary>
public GameObject arUcoCalibrationOptionsGroup;
/// <summary>
/// The dictionary identifier used for ArUco marker detection.
/// </summary>
[Tooltip("The dictionary identifier used for ArUco marker detection.")]
public ArUcoDictionary dictionaryId = ArUcoDictionary.DICT_6X6_250;
/// <summary>
/// The dictionary id dropdown.
/// </summary>
public Dropdown dictionaryIdDropdown;
/// <summary>
/// Determines if refine marker detection. (only valid for ArUco boards)
/// </summary>
[Tooltip("Determines if refine marker detection. (only valid for ArUco boards)")]
public bool refineMarkerDetection = true;
[Header("Image Input Option")]
/// <summary>
/// Determines if calibrates camera using the list of calibration images.
/// </summary>
[Tooltip("Determines if calibrates camera using the list of calibration images.")]
public bool isImagesInputMode = false;
/// <summary>
/// The calibration images directory path.
/// Set a relative directory path from the starting point of the "StreamingAssets" folder. e.g. "objdetect/calibration_images/".
/// </summary>
[Tooltip("Set a relative directory path from the starting point of the \"StreamingAssets\" folder. e.g. \"OpenCVForUnity/objdetect/calibration_images\"")]
public string calibrationImagesDirectory = "OpenCVForUnity/objdetect/calibration_images";
/// <summary>
/// The texture.
/// </summary>
Texture2D texture;
/// <summary>
/// The webcam texture to mat helper.
/// </summary>
WebCamTextureToMatHelper webCamTextureToMatHelper;
/// <summary>
/// The gray mat.
/// </summary>
Mat grayMat;
/// <summary>
/// The bgr mat.
/// </summary>
Mat bgrMat;
/// <summary>
/// The undistorted bgr mat.
/// </summary>
Mat undistortedBgrMat;
/// <summary>
/// The rgba mat.
/// </summary>
Mat rgbaMat;
/// <summary>
/// The cameraparam matrix.
/// </summary>
Mat camMatrix;
/// <summary>
/// The distortion coeffs.
/// </summary>
MatOfDouble distCoeffs;
/// <summary>
/// The rvecs.
/// </summary>
List<Mat> rvecs;
/// <summary>
/// The tvecs.
/// </summary>
List<Mat> tvecs;
List<Mat> imagePoints;
List<Mat> allImgs;
bool isInitialized = false;
bool isCalibrating = false;
double repErr = 0;
bool shouldCaptureFrame = false;
const int findChessboardCornersFlags =
Calib3d.CALIB_CB_ADAPTIVE_THRESH |
Calib3d.CALIB_CB_NORMALIZE_IMAGE |
//Calib3d.CALIB_CB_FILTER_QUADS |
Calib3d.CALIB_CB_FAST_CHECK |
0;
const int findChessboardCornersSBFlags =
Calib3d.CALIB_CB_NORMALIZE_IMAGE |
Calib3d.CALIB_CB_EXHAUSTIVE |
Calib3d.CALIB_CB_ACCURACY |
0;
const int findCirclesGridFlags =
//Calib3d.CALIB_CB_CLUSTERING |
0;
const int calibrationFlags =
//Calib3d.CALIB_USE_INTRINSIC_GUESS |
//Calib3d.CALIB_FIX_PRINCIPAL_POINT |
//Calib3d.CALIB_FIX_ASPECT_RATIO |
//Calib3d.CALIB_ZERO_TANGENT_DIST |
//Calib3d.CALIB_FIX_K1 |
//Calib3d.CALIB_FIX_K2 |
//Calib3d.CALIB_FIX_K3 |
//Calib3d.CALIB_FIX_K4 |
//Calib3d.CALIB_FIX_K5 |
Calib3d.CALIB_USE_LU |
0;
/*
// for ChArUcoBoard.
// chessboard square side length (normally in meters)
const float chArUcoBoradSquareLength = 0.04f;
// marker side length (same unit than squareLength)
const float chArUcoBoradMarkerLength = 0.02f;
const int charucoMinMarkers = 2;
Mat ids;
List<Mat> corners;
List<Mat> rejectedCorners;
Mat recoveredIdxs;
Mat charucoCorners;
Mat charucoIds;
CharucoBoard charucoBoard;
ArucoDetector arucoDetector;
CharucoDetector charucoDetector;
*/
Dictionary dictionary;
List<List<Mat>> allCorners;
List<Mat> allIds;
// Use this for initialization
IEnumerator Start()
{
//if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
Utils.setDebugMode(true);
webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper>();
// fix the screen orientation.
Screen.orientation = ScreenOrientation.LandscapeLeft;
// wait for the screen orientation to change.
yield return null;
markerTypeDropdown.value = (int)markerType;
boardSizeWDropdown.value = (int)boardSizeW - 1;
boardSizeHDropdown.value = (int)boardSizeH - 1;
showUndistortImageToggle.isOn = showUndistortImage;
squareSizeInputField.text = squareSize.ToString();
useNewCalibrationMethodToggle.isOn = useNewCalibrationMethod;
gridWidthInputField.text = gridWidth.ToString();
dictionaryIdDropdown.value = (int)dictionaryId;
bool arUcoCalibMode = markerType == MarkerType.ChArUcoBoard;
normalCalibrationOptionsGroup.gameObject.SetActive(!arUcoCalibMode);
arUcoCalibrationOptionsGroup.gameObject.SetActive(arUcoCalibMode);
#if UNITY_WEBGL && !UNITY_EDITOR
isImagesInputMode = false;
#endif
if (isImagesInputMode)
{
isImagesInputMode = InitializeImagesInputMode();
}
if (!isImagesInputMode)
{
#if UNITY_ANDROID && !UNITY_EDITOR
// Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2).
webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true;
#endif
webCamTextureToMatHelper.Initialize();
}
}
/// <summary>
/// Raises the webcam texture to mat helper initialized event.
/// </summary>
public void OnWebCamTextureToMatHelperInitialized()
{
Debug.Log("OnWebCamTextureToMatHelperInitialized");
Mat webCamTextureMat = webCamTextureToMatHelper.GetMat();
InitializeCalibraton(webCamTextureMat);
// If the WebCam is front facing, flip the Mat horizontally. Required for successful detection of AR markers.
if (webCamTextureToMatHelper.IsFrontFacing() && !webCamTextureToMatHelper.flipHorizontal)
{
webCamTextureToMatHelper.flipHorizontal = true;
}
else if (!webCamTextureToMatHelper.IsFrontFacing() && webCamTextureToMatHelper.flipHorizontal)
{
webCamTextureToMatHelper.flipHorizontal = false;
}
}
/// <summary>
/// Raises the webcam texture to mat helper disposed event.
/// </summary>
public void OnWebCamTextureToMatHelperDisposed()
{
Debug.Log("OnWebCamTextureToMatHelperDisposed");
DisposeCalibraton();
}
/// <summary>
/// Raises the webcam texture to mat helper error occurred event.
/// </summary>
/// <param name="errorCode">Error code.</param>
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
{
Debug.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
}
// Update is called once per frame
void Update()
{
if (isImagesInputMode)
return;
if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
{
Mat rgbaMat = webCamTextureToMatHelper.GetMat();
Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
if (shouldCaptureFrame)
{
shouldCaptureFrame = false;
Mat frameMat = grayMat.clone();
double e = CaptureFrame(frameMat);
if (e > 0)
repErr = e;
}
DrawFrame(grayMat, bgrMat);
if (showUndistortImage)
{
Calib3d.undistort(bgrMat, undistortedBgrMat, camMatrix, distCoeffs);
DrawCalibrationResult(undistortedBgrMat);
Imgproc.cvtColor(undistortedBgrMat, rgbaMat, Imgproc.COLOR_BGR2RGBA);
}
else
{
DrawCalibrationResult(bgrMat);
Imgproc.cvtColor(bgrMat, rgbaMat, Imgproc.COLOR_BGR2RGBA);
}
Utils.matToTexture2D(rgbaMat, texture);
}
}
private void InitializeCalibraton(Mat frameMat)
{
texture = new Texture2D(frameMat.cols(), frameMat.rows(), TextureFormat.RGBA32, false);
Utils.matToTexture2D(frameMat, texture);
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
gameObject.transform.localScale = new Vector3(frameMat.cols(), frameMat.rows(), 1);
Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
float width = frameMat.width();
float height = frameMat.height();
float imageSizeScale = 1.0f;
float widthScale = (float)Screen.width / width;
float heightScale = (float)Screen.height / height;
if (widthScale < heightScale)
{
Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
imageSizeScale = (float)Screen.height / (float)Screen.width;
}
else
{
Camera.main.orthographicSize = height / 2;
}
// set cameraparam.
camMatrix = CreateCameraMatrix(width, height);
Debug.Log("camMatrix " + camMatrix.dump());
distCoeffs = new MatOfDouble(0, 0, 0, 0, 0);
Debug.Log("distCoeffs " + distCoeffs.dump());
// calibration camera.
Size imageSize = new Size(width * imageSizeScale, height * imageSizeScale);
double apertureWidth = 0;
double apertureHeight = 0;
double[] fovx = new double[1];
double[] fovy = new double[1];
double[] focalLength = new double[1];
Point principalPoint = new Point(0, 0);
double[] aspectratio = new double[1];
Calib3d.calibrationMatrixValues(camMatrix, imageSize, apertureWidth, apertureHeight, fovx, fovy, focalLength, principalPoint, aspectratio);
Debug.Log("imageSize " + imageSize.ToString());
Debug.Log("apertureWidth " + apertureWidth);
Debug.Log("apertureHeight " + apertureHeight);
Debug.Log("fovx " + fovx[0]);
Debug.Log("fovy " + fovy[0]);
Debug.Log("focalLength " + focalLength[0]);
Debug.Log("principalPoint " + principalPoint.ToString());
Debug.Log("aspectratio " + aspectratio[0]);
grayMat = new Mat(frameMat.rows(), frameMat.cols(), CvType.CV_8UC1);
bgrMat = new Mat(frameMat.rows(), frameMat.cols(), CvType.CV_8UC3);
undistortedBgrMat = new Mat();
rgbaMat = new Mat(frameMat.rows(), frameMat.cols(), CvType.CV_8UC4);
rvecs = new List<Mat>();
tvecs = new List<Mat>();
imagePoints = new List<Mat>();
allImgs = new List<Mat>();
/*
ids = new Mat();
corners = new List<Mat>();
rejectedCorners = new List<Mat>();
recoveredIdxs = new Mat();
DetectorParameters detectorParams = new DetectorParameters();
detectorParams.set_minDistanceToBorder(3);
detectorParams.set_useAruco3Detection(true);
detectorParams.set_cornerRefinementMethod(Objdetect.CORNER_REFINE_SUBPIX);
detectorParams.set_minSideLengthCanonicalImg(16);
detectorParams.set_errorCorrectionRate(0.8);
dictionary = Objdetect.getPredefinedDictionary((int)dictionaryId);
RefineParameters refineParameters = new RefineParameters(10f, 3f, true);
arucoDetector = new ArucoDetector(dictionary, detectorParams, refineParameters);
charucoCorners = new Mat();
charucoIds = new Mat();
charucoBoard = new CharucoBoard( new Size((int)boardSizeW, (int)boardSizeH), chArUcoBoradSquareLength, chArUcoBoradMarkerLength, dictionary);
charucoDetector = new CharucoDetector(charucoBoard);
CharucoParameters charucoParameters = charucoDetector.getCharucoParameters();
charucoParameters.set_cameraMatrix(camMatrix);
charucoParameters.set_distCoeffs(distCoeffs);
charucoParameters.set_minMarkers(charucoMinMarkers);
charucoDetector.setCharucoParameters(charucoParameters);
charucoDetector.setDetectorParameters(detectorParams);
charucoDetector.setRefineParameters(refineParameters);
*/
allIds = new List<Mat>();
allCorners = new List<List<Mat>>();
isInitialized = true;
}
private void DisposeCalibraton()
{
ResetCalibration();
if (grayMat != null)
grayMat.Dispose();
if (bgrMat != null)
bgrMat.Dispose();
if (undistortedBgrMat != null)
undistortedBgrMat.Dispose();
if (rgbaMat != null)
rgbaMat.Dispose();
if (texture != null)
{
Texture2D.Destroy(texture);
texture = null;
}
foreach (var item in rvecs)
{
item.Dispose();
}
rvecs.Clear();
foreach (var item in tvecs)
{
item.Dispose();
}
tvecs.Clear();
/*
if (ids != null)
ids.Dispose();
foreach (var item in corners)
{
item.Dispose();
}
corners.Clear();
foreach (var item in rejectedCorners)
{
item.Dispose();
}
rejectedCorners.Clear();
if (recoveredIdxs != null)
recoveredIdxs.Dispose();
if (charucoCorners != null)
charucoCorners.Dispose();
if (charucoIds != null)
charucoIds.Dispose();
if (charucoBoard != null)
charucoBoard.Dispose();
if (arucoDetector != null)
arucoDetector.Dispose();
if (charucoDetector != null)
charucoDetector.Dispose();
*/
isInitialized = false;
}
private void DrawFrame(Mat grayMat, Mat bgrMat)
{
Imgproc.cvtColor(grayMat, bgrMat, Imgproc.COLOR_GRAY2BGR);
switch (markerType)
{
default:
case MarkerType.ChessBoard:
case MarkerType.CirclesGlid:
case MarkerType.AsymmetricCirclesGlid:
// detect markers.
MatOfPoint2f points = new MatOfPoint2f();
bool found = false;
switch (markerType)
{
default:
case MarkerType.ChessBoard:
if (useFindChessboardCornersSBMethod)
{
found = Calib3d.findChessboardCornersSB(grayMat, new Size((int)boardSizeW, (int)boardSizeH), points, findChessboardCornersSBFlags);
}
else
{
found = Calib3d.findChessboardCorners(grayMat, new Size((int)boardSizeW, (int)boardSizeH), points, findChessboardCornersFlags);
}
break;
case MarkerType.CirclesGlid:
found = Calib3d.findCirclesGrid(grayMat, new Size((int)boardSizeW, (int)boardSizeH), points, findCirclesGridFlags | Calib3d.CALIB_CB_SYMMETRIC_GRID);
break;
case MarkerType.AsymmetricCirclesGlid:
found = Calib3d.findCirclesGrid(grayMat, new Size((int)boardSizeW, (int)boardSizeH), points, findCirclesGridFlags | Calib3d.CALIB_CB_ASYMMETRIC_GRID);
break;
}
if (found)
{
// draw markers.
Calib3d.drawChessboardCorners(bgrMat, new Size((int)boardSizeW, (int)boardSizeH), points, found);
}
break;
case MarkerType.ChArUcoBoard:
/*
// detect markers.
arucoDetector.detectMarkers(grayMat, corners, ids, rejectedCorners);
// refine marker detection.
if (refineMarkerDetection)
{
// https://github.com/opencv/opencv/blob/377be68d923e40900ac5526242bcf221e3f355e5/modules/objdetect/src/aruco/charuco_detector.cpp#L310
arucoDetector.refineDetectedMarkers(grayMat, charucoBoard, corners, ids, rejectedCorners);
}
// if at least one marker detected
if (ids.total() > 0)
{
charucoDetector.detectBoard(grayMat, charucoCorners, charucoIds, corners, ids);
// draw markers.
if (corners.Count == ids.total() || ids.total() == 0)
Objdetect.drawDetectedMarkers(bgrMat, corners, ids, new Scalar(0, 255, 0, 255));
// if at least one charuco corner detected
if (charucoCorners.total() == charucoIds.total() || charucoIds.total() == 0)
{
Objdetect.drawDetectedCornersCharuco(bgrMat, charucoCorners, charucoIds, new Scalar(0, 0, 255, 255));
}
}
*/
break;
}
}
private void DrawCalibrationResult(Mat bgrMat)
{
double[] camMatrixArr = new double[(int)camMatrix.total()];
camMatrix.get(0, 0, camMatrixArr);
double[] distCoeffsArr = new double[(int)distCoeffs.total()];
distCoeffs.get(0, 0, distCoeffsArr);
int textLeft = 320;
int ff = Imgproc.FONT_HERSHEY_SIMPLEX;
double fs = 0.4;
Scalar c = new Scalar(255, 255, 255, 255);
int t = 0;
int lt = Imgproc.LINE_AA;
bool blo = false;
int frameCount = (markerType == MarkerType.ChArUcoBoard) ? allCorners.Count : imagePoints.Count;
Imgproc.putText(bgrMat, frameCount + " FRAME CAPTURED", new Point(bgrMat.cols() - textLeft, 20), ff, fs, c, t, lt, blo);
Imgproc.putText(bgrMat, "IMAGE_WIDTH: " + bgrMat.width(), new Point(bgrMat.cols() - textLeft, 40), ff, fs, c, t, lt, blo);
Imgproc.putText(bgrMat, "IMAGE_HEIGHT: " + bgrMat.height(), new Point(bgrMat.cols() - textLeft, 60), ff, fs, c, t, lt, blo);
Imgproc.putText(bgrMat, "CALIBRATION_FLAGS: " + calibrationFlags, new Point(bgrMat.cols() - textLeft, 80), ff, fs, c, t, lt, blo);
Imgproc.putText(bgrMat, "CAMERA_MATRIX: ", new Point(bgrMat.cols() - 310, 100), ff, fs, c, t, lt, blo);
for (int i = 0; i < camMatrixArr.Length; i = i + 3)
{
Imgproc.putText(bgrMat, " " + camMatrixArr[i] + ", " + camMatrixArr[i + 1] + ", " + camMatrixArr[i + 2] + ",", new Point(bgrMat.cols() - textLeft, 120 + 20 * i / 3), ff, fs, c, t, lt, blo);
}
Imgproc.putText(bgrMat, "DISTORTION_COEFFICIENTS: ", new Point(bgrMat.cols() - textLeft, 180), ff, fs, c, t, lt, blo);
for (int i = 0; i < distCoeffsArr.Length; ++i)
{
Imgproc.putText(bgrMat, " " + distCoeffsArr[i] + ",", new Point(bgrMat.cols() - textLeft, 200 + 20 * i), ff, fs, c, t, lt, blo);
}
Imgproc.putText(bgrMat, "AVG_REPROJECTION_ERROR: " + repErr, new Point(bgrMat.cols() - textLeft, 300), ff, fs, c, t, lt, blo);
if (frameCount == 0)
Imgproc.putText(bgrMat, "Please press the capture button to start!", new Point(5, bgrMat.rows() - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_AA, false);
}
private double CaptureFrame(Mat frameMat)
{
double repErr = -1;
switch (markerType)
{
default:
case MarkerType.ChessBoard:
case MarkerType.CirclesGlid:
case MarkerType.AsymmetricCirclesGlid:
MatOfPoint2f points = new MatOfPoint2f();
Size patternSize = new Size((int)boardSizeW, (int)boardSizeH);
bool found = false;
switch (markerType)
{
default:
case MarkerType.ChessBoard:
if (useFindChessboardCornersSBMethod)
{
found = Calib3d.findChessboardCornersSB(frameMat, patternSize, points, findChessboardCornersSBFlags);
}
else
{
found = Calib3d.findChessboardCorners(frameMat, patternSize, points, findChessboardCornersFlags);
}
break;
case MarkerType.CirclesGlid:
found = Calib3d.findCirclesGrid(frameMat, patternSize, points, findCirclesGridFlags | Calib3d.CALIB_CB_SYMMETRIC_GRID);
break;
case MarkerType.AsymmetricCirclesGlid:
found = Calib3d.findCirclesGrid(frameMat, patternSize, points, findCirclesGridFlags | Calib3d.CALIB_CB_ASYMMETRIC_GRID);
break;
}
if (found)
{
if (markerType == MarkerType.ChessBoard && !useFindChessboardCornersSBMethod && enableCornerSubPix)
{
int winSize = 11;
Imgproc.cornerSubPix(frameMat, points, new Size(winSize, winSize), new Size(-1, -1), new TermCriteria(TermCriteria.EPS + TermCriteria.COUNT, 30, 0.0001));
}
imagePoints.Add(points);
allImgs.Add(frameMat);
Debug.Log(imagePoints.Count + " Frame captured.");
}
else
{
Debug.Log("Invalid frame.");
frameMat.Dispose();
if (points != null)
points.Dispose();
return -1;
}
if (imagePoints.Count < 1)
{
Debug.Log("Not enough points for calibration.");
repErr = -1;
}
else
{
MatOfPoint3f objectPoint = new MatOfPoint3f(new Mat(imagePoints[0].rows(), 1, CvType.CV_32FC3));
CalcChessboardCorners(patternSize, squareSize, objectPoint, markerType);
float grid_width = squareSize * ((int)patternSize.width - 1);
bool release_object = false;
if (useNewCalibrationMethod)
{
grid_width = gridWidth;
release_object = true;
}
float[] tlPt = new float[3]; // top-left point
objectPoint.get(0, 0, tlPt);
float[] trPt = new float[3]; // top-right point
objectPoint.get((int)patternSize.width - 1, 0, trPt);
trPt[0] = tlPt[0] + grid_width;
objectPoint.put((int)patternSize.width - 1, 0, trPt);
Mat newObjPoints = objectPoint.clone();
List<Mat> objectPoints = new List<Mat>();
for (int i = 0; i < imagePoints.Count; ++i)
{
objectPoints.Add(objectPoint.clone());
}
int iFixedPoint = -1;
if (release_object)
iFixedPoint = (int)patternSize.width - 1;
repErr = Calib3d.calibrateCameraRO(
objectPoints,
imagePoints,
frameMat.size(),
iFixedPoint,
camMatrix,
distCoeffs,
rvecs,
tvecs,
newObjPoints,
calibrationFlags
);
//if (release_object)
//{
// Debug.Log("New board corners: ");
// Point3[] newPoints = new MatOfPoint3f(newObjPoints).toArray();
// Debug.Log(newPoints[0]);
// Debug.Log(newPoints[(int)patternSize.width - 1]);
// Debug.Log(newPoints[(int)patternSize.width * ((int)patternSize.height - 1)]);
// Debug.Log(newPoints[newPoints.Length - 1]);
//}
objectPoint.Dispose();
}
break;
case MarkerType.ChArUcoBoard:
/*
List<Mat> corners = new List<Mat>();
Mat ids = new Mat();
arucoDetector.detectMarkers(frameMat, corners, ids, rejectedCorners);
if (refineMarkerDetection)
{
// https://github.com/opencv/opencv/blob/377be68d923e40900ac5526242bcf221e3f355e5/modules/objdetect/src/aruco/charuco_detector.cpp#L310
arucoDetector.refineDetectedMarkers(frameMat, charucoBoard, corners, ids, rejectedCorners);
}
if (ids.total() > 0)
{
allCorners.Add(corners);
allIds.Add(ids);
allImgs.Add(frameMat);
Debug.Log(allCorners.Count + " Frame captured.");
}
else
{
Debug.Log("Invalid frame.");
frameMat.Dispose();
if (ids != null)
ids.Dispose();
foreach (var item in corners)
{
item.Dispose();
}
corners.Clear();
return -1;
}
// calibrate camera using charuco boards
repErr = CalibrateCameraCharuco(allCorners, allIds, charucoBoard, frameMat.size(), camMatrix, distCoeffs, rvecs, tvecs, calibrationFlags, calibrationFlags);
*/
break;
}
Debug.Log("repErr: " + repErr);
Debug.Log("camMatrix: " + camMatrix.dump());
Debug.Log("distCoeffs: " + distCoeffs.dump());
return repErr;
}
/*
private double CalibrateCameraCharuco(List<List<Mat>> allCorners, List<Mat> allIds, CharucoBoard board, Size imageSize, Mat cameraMatrix, Mat distCoeffs, List<Mat> rvecs = null, List<Mat> tvecs = null, int calibrationFlags = 0, int minMarkers = 2)
{
// prepare data for charuco calibration
int nFrames = allCorners.Count;
List<Mat> allCharucoCorners = new List<Mat>();
List<Mat> allCharucoIds = new List<Mat>();
List<Mat> filteredImages = new List<Mat>();
for (int i = 0; i < nFrames; ++i)
{
// interpolate using camera parameters
Mat currentCharucoCorners = new Mat();
Mat currentCharucoIds = new Mat();
charucoDetector.detectBoard(allImgs[i], currentCharucoCorners, currentCharucoIds, allCorners[i], allIds[i]);
//if (currentCharucoIds.total() > 0)
if (currentCharucoIds.total() > 0 && currentCharucoCorners.total() == currentCharucoIds.total())
{
allCharucoCorners.Add(currentCharucoCorners);
allCharucoIds.Add(currentCharucoIds);
filteredImages.Add(allImgs[i]);
}
else
{
currentCharucoCorners.Dispose();
currentCharucoIds.Dispose();
}
}
if (allCharucoCorners.Count < 1)
{
Debug.Log("Not enough corners for calibration.");
return -1;
}
if (rvecs == null)
rvecs = new List<Mat>();
if (tvecs == null)
tvecs = new List<Mat>();
return Aruco.calibrateCameraCharuco(allCharucoCorners, allCharucoIds, board, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs, calibrationFlags); // error
}
*/
private void ResetCalibration()
{
foreach (var item in allImgs)
{
item.Dispose();
}
allImgs.Clear();
repErr = 0;
camMatrix = CreateCameraMatrix(bgrMat.width(), bgrMat.height());
distCoeffs = new MatOfDouble(0, 0, 0, 0, 0);
foreach (var item in imagePoints)
{
item.Dispose();
}
imagePoints.Clear();
foreach (var corners in allCorners)
{
foreach (var item in corners)
{
item.Dispose();
}
}
allCorners.Clear();
foreach (var item in allIds)
{
item.Dispose();
}
allIds.Clear();
}
private Mat CreateCameraMatrix(float width, float height)
{
int max_d = (int)Mathf.Max(width, height);
double fx = max_d;
double fy = max_d;
double cx = width / 2.0f;
double cy = height / 2.0f;
Mat camMatrix = new Mat(3, 3, CvType.CV_64FC1);
camMatrix.put(0, 0, fx);
camMatrix.put(0, 1, 0);
camMatrix.put(0, 2, cx);
camMatrix.put(1, 0, 0);
camMatrix.put(1, 1, fy);
camMatrix.put(1, 2, cy);
camMatrix.put(2, 0, 0);
camMatrix.put(2, 1, 0);
camMatrix.put(2, 2, 1.0f);
return camMatrix;
}
private void CalcChessboardCorners(Size patternSize, float squareSize, MatOfPoint3f corners, MarkerType markerType)
{
if ((int)(patternSize.width * patternSize.height) != corners.rows())
{
Debug.Log("Invalid corners size.");
corners.create((int)(patternSize.width * patternSize.height), 1, CvType.CV_32FC3);
}
int width = (int)patternSize.width;
int height = (int)patternSize.height;
switch (markerType)
{
default:
case MarkerType.ChessBoard:
case MarkerType.CirclesGlid:
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
corners.put(width * i + j, 0, new float[] { j * squareSize, i * squareSize, 0f });
}
}
break;
case MarkerType.AsymmetricCirclesGlid:
for (int i = 0; i < height; ++i)
{