LineScanCameraCalibrator.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using CCDCount.MODEL.ShuLiModel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace CCDCount.DLL.Tools
  9. {
  10. public static class LineScanCameraCalibrator
  11. {
  12. /// <summary>
  13. /// 计算线阵相机X和Y方向的单像素精度
  14. /// 线阵相机在X方向(像素方向)和Y方向(扫描方向)有不同的缩放系数
  15. /// </summary>
  16. /// <param name="knownPairs">已知物理距离的点对集合</param>
  17. /// <param name="actualDistance">实际物理距离(单位:mm)</param>
  18. /// <returns>包含X和Y方向单像素精度的结构体</returns>
  19. public static PixelScale CalculateLineScanPixelScale(List<MaxLengthModel> knownPairs, double actualDistance)
  20. {
  21. if (knownPairs == null || knownPairs.Count == 0)
  22. throw new ArgumentException("至少需要一个点对来进行校准");
  23. List<double> xScales = new List<double>();
  24. List<double> yScales = new List<double>();
  25. foreach (var pair in knownPairs)
  26. {
  27. int deltaX = pair.Point2.X - pair.Point1.X;
  28. int deltaY = pair.Point2.Y - pair.Point1.Y;
  29. // 计算像素欧几里得距离
  30. double pixelDistance = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
  31. if (pixelDistance == 0)
  32. continue; // 避免除零错误
  33. // 计算每像素的物理距离(缩放因子)
  34. double scalePerPixel = actualDistance / pixelDistance;
  35. // 对于线阵相机,我们需要根据X和Y方向的贡献来分解缩放因子
  36. // X方向精度 = 总缩放因子 * |ΔX| / 像素距离
  37. // Y方向精度 = 总缩放因子 * |ΔY| / 像素距离
  38. if (Math.Abs(deltaX) > double.Epsilon)
  39. {
  40. double xScale = scalePerPixel * Math.Abs(deltaX) / pixelDistance;
  41. xScales.Add(xScale);
  42. }
  43. if (Math.Abs(deltaY) > double.Epsilon)
  44. {
  45. double yScale = scalePerPixel * Math.Abs(deltaY) / pixelDistance;
  46. yScales.Add(yScale);
  47. }
  48. }
  49. // 计算平均值作为最终精度
  50. double avgXScale = xScales.Count > 0 ? xScales.Average() : 1.0;
  51. double avgYScale = yScales.Count > 0 ? yScales.Average() : 1.0;
  52. return new PixelScale(avgXScale, avgYScale);
  53. }
  54. /// <summary>
  55. /// 使用更精确的方法计算线阵相机的像素精度
  56. /// 分别处理纯X方向、纯Y方向和对角线方向的点对
  57. /// </summary>
  58. /// <param name="horizontalPairs">纯水平方向点对(Y坐标相同)</param>
  59. /// <param name="verticalPairs">纯垂直方向点对(X坐标相同)</param>
  60. /// <param name="diagonalPairs">对角线方向点对</param>
  61. /// <param name="actualDistance">实际物理距离(单位:mm)</param>
  62. /// <returns>包含X和Y方向单像素精度的结构体</returns>
  63. public static PixelScale CalculatePreciseLineScanPixelScale(
  64. List<MaxLengthModel> horizontalPairs,
  65. List<MaxLengthModel> verticalPairs,
  66. List<MaxLengthModel> diagonalPairs,
  67. double actualDistance)
  68. {
  69. double xScale = 1.0, yScale = 1.0;
  70. // 优先使用纯水平方向点对计算X方向精度
  71. if (horizontalPairs != null && horizontalPairs.Count > 0)
  72. {
  73. var xScales = new List<double>();
  74. foreach (var pair in horizontalPairs)
  75. {
  76. if (pair.Point1.Y != pair.Point2.Y) continue; // 确保是水平方向
  77. int deltaX = pair.Point2.X - pair.Point1.X;
  78. if (Math.Abs(deltaX) > 0)
  79. {
  80. xScales.Add(actualDistance / Math.Abs(deltaX));
  81. }
  82. }
  83. if (xScales.Count > 0)
  84. xScale = xScales.Average();
  85. }
  86. // 优先使用纯垂直方向点对计算Y方向精度
  87. if (verticalPairs != null && verticalPairs.Count > 0)
  88. {
  89. var yScales = new List<double>();
  90. foreach (var pair in verticalPairs)
  91. {
  92. if (pair.Point1.X != pair.Point2.X) continue; // 确保是垂直方向
  93. int deltaY = pair.Point2.Y - pair.Point1.Y;
  94. if (Math.Abs(deltaY) > 0)
  95. {
  96. yScales.Add(actualDistance / Math.Abs(deltaY));
  97. }
  98. }
  99. if (yScales.Count > 0)
  100. yScale = yScales.Average();
  101. }
  102. // 如果缺少某个方向的纯方向点对,使用对角线点对进行补充
  103. if (horizontalPairs == null || horizontalPairs.Count == 0)
  104. {
  105. // 从对角线点对中估算X方向精度
  106. if (diagonalPairs != null && diagonalPairs.Count > 0)
  107. {
  108. var xScalesFromDiagonal = new List<double>();
  109. foreach (var pair in diagonalPairs)
  110. {
  111. int deltaX = pair.Point2.X - pair.Point1.X;
  112. int deltaY = pair.Point2.Y - pair.Point1.Y;
  113. double pixelDistance = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
  114. if (pixelDistance > 0)
  115. {
  116. double scalePerPixel = actualDistance / pixelDistance;
  117. if (Math.Abs(deltaX) > double.Epsilon)
  118. {
  119. double xScaleFromDiagonal = scalePerPixel * Math.Abs(deltaX) / pixelDistance;
  120. xScalesFromDiagonal.Add(xScaleFromDiagonal);
  121. }
  122. }
  123. }
  124. if (xScalesFromDiagonal.Count > 0 && xScale == 1.0) // 只在没有纯水平点对时更新
  125. xScale = xScalesFromDiagonal.Average();
  126. }
  127. }
  128. if (verticalPairs == null || verticalPairs.Count == 0)
  129. {
  130. // 从对角线点对中估算Y方向精度
  131. if (diagonalPairs != null && diagonalPairs.Count > 0)
  132. {
  133. var yScalesFromDiagonal = new List<double>();
  134. foreach (var pair in diagonalPairs)
  135. {
  136. int deltaX = pair.Point2.X - pair.Point1.X;
  137. int deltaY = pair.Point2.Y - pair.Point1.Y;
  138. double pixelDistance = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
  139. if (pixelDistance > 0)
  140. {
  141. double scalePerPixel = actualDistance / pixelDistance;
  142. if (Math.Abs(deltaY) > double.Epsilon)
  143. {
  144. double yScaleFromDiagonal = scalePerPixel * Math.Abs(deltaY) / pixelDistance;
  145. yScalesFromDiagonal.Add(yScaleFromDiagonal);
  146. }
  147. }
  148. }
  149. if (yScalesFromDiagonal.Count > 0 && yScale == 1.0) // 只在没有纯垂直点对时更新
  150. yScale = yScalesFromDiagonal.Average();
  151. }
  152. }
  153. return new PixelScale(xScale, yScale);
  154. }
  155. /// <summary>
  156. /// 使用已知的像素精度计算两点间的物理距离
  157. /// </summary>
  158. /// <param name="point1">第一个点</param>
  159. /// <param name="point2">第二个点</param>
  160. /// <param name="scale">像素精度</param>
  161. /// <returns>物理距离(单位:mm)</returns>
  162. public static double CalculatePhysicalDistance(Point point1, Point point2, PixelScale scale)
  163. {
  164. int deltaX = point2.X - point1.X;
  165. int deltaY = point2.Y - point1.Y;
  166. // 使用各自的像素精度计算X和Y方向的物理距离
  167. double physicalDistanceX = Math.Abs(deltaX) * scale.XScale;
  168. double physicalDistanceY = Math.Abs(deltaY) * scale.YScale;
  169. // 使用勾股定理计算实际距离
  170. return Math.Sqrt(
  171. Math.Pow(physicalDistanceX, 2) +
  172. Math.Pow(physicalDistanceY, 2)
  173. );
  174. }
  175. /// <summary>
  176. /// 自动分类点对为水平、垂直和对角线方向
  177. /// </summary>
  178. /// <param name="allPairs">所有点对</param>
  179. /// <param name="horizontalPairs">输出:纯水平方向点对</param>
  180. /// <param name="verticalPairs">输出:纯垂直方向点对</param>
  181. /// <param name="diagonalPairs">输出:对角线方向点对</param>
  182. public static void ClassifyPointPairs(
  183. List<MaxLengthModel> allPairs,
  184. out List<MaxLengthModel> horizontalPairs,
  185. out List<MaxLengthModel> verticalPairs,
  186. out List<MaxLengthModel> diagonalPairs)
  187. {
  188. horizontalPairs = new List<MaxLengthModel>();
  189. verticalPairs = new List<MaxLengthModel>();
  190. diagonalPairs = new List<MaxLengthModel>();
  191. foreach (var pair in allPairs)
  192. {
  193. int deltaX = pair.Point2.X - pair.Point1.X;
  194. int deltaY = pair.Point2.Y - pair.Point1.Y;
  195. if (deltaY == 0 && Math.Abs(deltaX) > 0)
  196. {
  197. // 水平方向点对(Y坐标相同)
  198. horizontalPairs.Add(pair);
  199. }
  200. else if (deltaX == 0 && Math.Abs(deltaY) > 0)
  201. {
  202. // 垂直方向点对(X坐标相同)
  203. verticalPairs.Add(pair);
  204. }
  205. else
  206. {
  207. // 对角线方向点对
  208. diagonalPairs.Add(pair);
  209. }
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// 存储X和Y方向的像素精度
  215. /// </summary>
  216. public struct PixelScale
  217. {
  218. public double XScale { get; set; } // X方向单像素精度 (mm/pixel)
  219. public double YScale { get; set; } // Y方向单像素精度 (mm/pixel)
  220. public PixelScale(double xScale, double yScale)
  221. {
  222. XScale = xScale;
  223. YScale = yScale;
  224. }
  225. public override string ToString()
  226. {
  227. return $"XScale: {XScale:F6} mm/pixel, YScale: {YScale:F6} mm/pixel";
  228. }
  229. }
  230. }