Particle.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using OpenCvSharp;
  2. using System;
  3. using System.Drawing;
  4. using static PaintDotNet.Base.DedicatedAnalysis.Inclusions.InclusionsGlobalSettings;
  5. using Point = System.Drawing.Point;
  6. namespace PaintDotNet.Base.DedicatedAnalysis.Inclusions.Model
  7. {
  8. /// <summary>
  9. /// 微粒(particle)
  10. /// 单个析出的沉淀物,一般为非金属。
  11. /// </summary>
  12. public class Particle
  13. {
  14. #region 属性
  15. /// <summary>
  16. /// 组成微粒的像素数组
  17. /// </summary>
  18. public OpenCvSharp.Point[] points;
  19. public Point[] points2;
  20. /// <summary>
  21. /// 当前微粒判定所使用的标准
  22. /// </summary>
  23. public InclusionsStandard standard;
  24. /// <summary>
  25. /// 水平距离
  26. /// </summary>
  27. public double horizontalDistance;
  28. /// <summary>
  29. /// 垂直距离
  30. /// </summary>
  31. public double verticalDistance;
  32. /// <summary>
  33. /// 像素长度
  34. /// </summary>
  35. public double pixelLength;
  36. /// <summary>
  37. /// 像素宽度
  38. /// </summary>
  39. public double pixelWidth;
  40. /// <summary>
  41. /// 物理长度
  42. /// </summary>
  43. public double physicalLength;
  44. /// <summary>
  45. /// 物理宽度
  46. /// </summary>
  47. public double physicalWidth;
  48. /// <summary>
  49. /// 长宽比
  50. /// </summary>
  51. public double lwRatio;
  52. /// <summary>
  53. /// 形状
  54. /// </summary>
  55. public ParticleShape shape;
  56. /// <summary>
  57. /// 矩形轮廓
  58. /// </summary>
  59. public Rectangle rectProfile;
  60. /// <summary>
  61. /// 矩形轮廓(K法)
  62. /// </summary>
  63. public Rectangle rectKMethodProfile;
  64. public double areaK;
  65. public int offsetX;
  66. public int offsetY;
  67. #endregion
  68. /// <summary>
  69. /// 构造方法
  70. /// </summary>
  71. /// <param name="points">组成微粒的像素数组</param>
  72. /// <param name="standard">当前微粒判定所使用的标准</param>
  73. public Particle(OpenCvSharp.Point[] points, InclusionsStandard standard)
  74. {
  75. this.points = points;
  76. this.points2 = new Point[points.Length];
  77. this.standard = standard;
  78. parametersCalculation();
  79. this.areaK = Cv2.ContourArea(points);
  80. }
  81. /// <summary>
  82. /// 参数计算
  83. /// </summary>
  84. private void parametersCalculation()
  85. {
  86. int minx = 0, miny = 0, maxx = 0, maxy = 0;
  87. for (int i = 0; i < this.points.Length; i++)
  88. {
  89. this.points2[i] = new Point(this.points[i].X, this.points[i].Y);
  90. if (i==0)
  91. {
  92. minx = this.points[i].X;
  93. maxx = this.points[i].X;
  94. miny = this.points[i].Y;
  95. maxy = this.points[i].Y;
  96. continue;
  97. }
  98. if (this.points[i].X < minx)
  99. {
  100. minx = this.points[i].X;
  101. }
  102. if (this.points[i].X > maxx)
  103. {
  104. maxx = this.points[i].X;
  105. }
  106. if (this.points[i].Y < miny)
  107. {
  108. miny = this.points[i].Y;
  109. }
  110. if (this.points[i].Y > maxy)
  111. {
  112. maxy = this.points[i].Y;
  113. }
  114. }
  115. // 计算
  116. this.horizontalDistance = maxx - minx;
  117. this.verticalDistance = maxy - miny;
  118. // 计算长度,宽度/直径,面积,长宽比
  119. // 计算长度,宽度/直径,面积,形状系数,长宽比
  120. if (this.standard.globalSettings.rollingDirection == RollingDirection.PORTRAIT)
  121. {
  122. this.pixelLength = maxy - miny;
  123. this.pixelWidth = maxx - minx;
  124. }
  125. else
  126. {
  127. this.pixelWidth = maxy - miny;
  128. this.pixelLength = maxx - minx;
  129. }
  130. this.rectProfile = new Rectangle(minx, miny, maxx - minx, maxy - miny);
  131. this.rectKMethodProfile = this.rectProfile;
  132. this.lwRatio = this.pixelLength / this.pixelWidth;
  133. if (this.lwRatio < this.standard.globalSettings.lwRatioLimit)
  134. {
  135. this.shape = ParticleShape.Globular;
  136. }
  137. else
  138. {
  139. this.shape = ParticleShape.Elongated;
  140. }
  141. this.physicalLength = this.pixelLength * this.standard.globalSettings.pxPerUnit;
  142. this.physicalWidth = this.pixelWidth * this.standard.globalSettings.pxPerUnit;
  143. }
  144. }
  145. }