Projection2DTo2D.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Emgu.CV;
  8. using Emgu.CV.Structure;
  9. namespace bbiwarg.Utility
  10. {
  11. /// <summary>
  12. /// Computes and stores a homography matrix and provides functions to export it and project points.
  13. /// </summary>
  14. class Projection2DTo2D
  15. {
  16. /// <summary>
  17. /// size of the image the original points are in
  18. /// </summary>
  19. private ImageSize sizeA;
  20. /// <summary>
  21. /// size of the image the projected points are in
  22. /// </summary>
  23. private ImageSize sizeB;
  24. /// <summary>
  25. /// calibration points in the first image (match points in calibrationPointsB)
  26. /// </summary>
  27. private List<PointF> calibrationPointsA;
  28. /// <summary>
  29. /// calibration points in the second image (match points in calibrationPointsA)
  30. /// </summary>
  31. private List<PointF> calibrationPointsB;
  32. /// <summary>
  33. /// number of points used for the calibration
  34. /// </summary>
  35. private int numPointsForCalibration;
  36. /// <summary>
  37. /// homography matrix used to compute the projected points
  38. /// </summary>
  39. private HomographyMatrix homography;
  40. /// <summary>
  41. /// true iff the calibration is finished
  42. /// </summary>
  43. public bool IsCalibrated { get; private set; }
  44. /// <summary>
  45. /// Constructs a Projection2DTo2D.
  46. /// </summary>
  47. /// <param name="sizeA">size of the image the original points are in</param>
  48. /// <param name="sizeB">size of the image the projected points are in</param>
  49. /// <param name="numPointsForCalibration">number of points used for the calibration</param>
  50. public Projection2DTo2D(ImageSize sizeA, ImageSize sizeB, int numPointsForCalibration = 4)
  51. {
  52. this.sizeA = sizeA;
  53. this.sizeB = sizeB;
  54. this.numPointsForCalibration = numPointsForCalibration;
  55. reset();
  56. }
  57. /// <summary>
  58. /// Resets the calibration.
  59. /// </summary>
  60. public void reset()
  61. {
  62. homography = null;
  63. IsCalibrated = false;
  64. calibrationPointsA = new List<PointF>();
  65. calibrationPointsB = new List<PointF>();
  66. }
  67. /// <summary>
  68. /// Adds a pair of calibration points.
  69. /// </summary>
  70. /// <param name="pointA">point in the first image</param>
  71. /// <param name="pointB">point in the second image</param>
  72. public void addCalibrationPoints(Vector2D pointA, Vector2D pointB)
  73. {
  74. calibrationPointsA.Add(sizeA.getRelativePoint(pointA));
  75. calibrationPointsB.Add(sizeB.getRelativePoint(pointB));
  76. if (calibrationPointsA.Count == numPointsForCalibration)
  77. calibrate();
  78. }
  79. /// <summary>
  80. /// Projects a point.
  81. /// </summary>
  82. /// <param name="pointA">the point to project</param>
  83. /// <returns>projected point</returns>
  84. public Vector2D projectPoint(Vector2D pointA)
  85. {
  86. PointF[] pointfsB = new PointF[1] { sizeA.getRelativePoint(pointA) };
  87. homography.ProjectPoints(pointfsB);
  88. return sizeB.getAbsolutePoint(new Vector2D(pointfsB[0]));
  89. }
  90. /// <summary>
  91. /// Computes the homography from the lists of calibration points.
  92. /// </summary>
  93. private void calibrate()
  94. {
  95. homography = CameraCalibration.FindHomography(calibrationPointsA.ToArray(), calibrationPointsB.ToArray(), Emgu.CV.CvEnum.HOMOGRAPHY_METHOD.DEFAULT, 0.995);
  96. calibrationPointsA.Clear();
  97. calibrationPointsB.Clear();
  98. IsCalibrated = true;
  99. exportHomography();
  100. }
  101. /// <summary>
  102. /// Writes the homography to a file.
  103. /// </summary>
  104. private void exportHomography()
  105. {
  106. String[] fileData = new String[homography.Size.Height];
  107. StringBuilder sb = new StringBuilder();
  108. for (int r = 0; r < homography.Size.Height; r++)
  109. {
  110. for (int c = 0; c < homography.Size.Width; c++)
  111. {
  112. sb.Append(homography.Data[r, c]);
  113. sb.Append(" ");
  114. }
  115. fileData[r] = sb.ToString();
  116. sb.Clear();
  117. }
  118. System.IO.File.WriteAllLines(Parameters.HomographyFileName, fileData);
  119. }
  120. }
  121. }