GrainSizeStandard.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using OpenCvSharp;
  2. //using PaintDotNet.Base.DedicatedAnalysis.GrainSize.Model;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. namespace PaintDotNet.Base.DedicatedAnalysis.GrainSize
  6. {
  7. /// <summary>
  8. /// 夹杂物标准
  9. /// </summary>
  10. public abstract class GrainSizeStandard
  11. {
  12. public static Color[] RAINBOW_COLORS = { Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Blue, Color.Cyan, Color.Purple };
  13. /// <summary>
  14. /// 设置
  15. /// </summary>
  16. public GrainSizeGlobalSettings globalSettings = new GrainSizeGlobalSettings();
  17. //#region 识别测量区域中的微粒
  18. ///// <summary>
  19. ///// 识别测量区域中的微粒
  20. ///// </summary>
  21. ///// <param name="field">视场</param>
  22. ///// <returns>识别出的微粒列表</returns>
  23. //public virtual List<Particle> identifyParticles(Mat field)
  24. //{
  25. // Cv2.Threshold(field, field, 175, 255, ThresholdTypes.BinaryInv);
  26. // OpenCvSharp.Point[][] contours;
  27. // HierarchyIndex[] hierarchy;
  28. // Cv2.FindContours(field, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxNone);
  29. // List<Particle> particles = new List<Particle>();
  30. // for (int i = 0; i < contours.Length; i++)
  31. // {
  32. // if (hierarchy[i].Parent != -1)
  33. // {
  34. // continue;
  35. // }
  36. // OpenCvSharp.Point[] contour = contours[i];
  37. // particles.Add(new Particle(contour, this));
  38. // }
  39. // return particles;
  40. //}
  41. //#endregion
  42. //#region 过滤微粒
  43. ///// <summary>
  44. ///// 过滤微粒
  45. ///// </summary>
  46. ///// 使用minimumLength,minimumWidth最小尺寸来过滤微粒
  47. ///// <param name="particles">过滤前的微粒列表</param>
  48. ///// <returns>过滤后的微粒列表</returns>
  49. //public virtual List<Particle> filterParticles(List<Particle> particles)
  50. //{
  51. // for (int i = particles.Count - 1; i >= 0; i--)
  52. // {
  53. // Particle particle = particles[i];
  54. // if (particle.physicalLength < this.globalSettings.minimumLength ||
  55. // particle.physicalWidth < this.globalSettings.minimumWidth)
  56. // {
  57. // particles.Remove(particle);
  58. // }
  59. // }
  60. // return particles;
  61. //}
  62. //#endregion
  63. //#region 划分夹杂物
  64. ///// <summary>
  65. ///// 划分夹杂物
  66. ///// </summary>
  67. ///// 使用distancesBetweenParticles,distancesBetweenStringers划分夹杂物
  68. ///// <param name="particles">过滤后的微粒列表</param>
  69. ///// <returns>夹杂物列表</returns>
  70. //public virtual List<Inclusion> divideInclusion(List<Particle> particles)
  71. //{
  72. // return this.inclusionsDivideInclusion(this.particlesDivideInclusion(particles));
  73. //}
  74. //#endregion
  75. //#region 微粒划分夹杂物
  76. ///// <summary>
  77. ///// 微粒划分夹杂物
  78. ///// </summary>
  79. ///// <param name="particles">过滤后的微粒列表</param>
  80. ///// <returns>夹杂物列表</returns>
  81. //protected virtual List<Inclusion> particlesDivideInclusion(List<Particle> particles)
  82. //{
  83. // List<Inclusion> inclusions = new List<Inclusion>();
  84. // // 第一次划分 微粒划分夹杂物
  85. // foreach (Particle particle in particles)
  86. // {
  87. // if (inclusions.Count == 0)
  88. // {
  89. // Inclusion inclusion = new Inclusion(this);
  90. // inclusion.addParticles(particle);
  91. // inclusions.Add(inclusion);
  92. // continue;
  93. // }
  94. // foreach (Inclusion inclusion in inclusions)
  95. // {
  96. // if (inclusion.particles[0].shape != particle.shape)
  97. // {
  98. // continue;
  99. // }
  100. // // 矩形相交
  101. // if (inclusion.rectProfile.IntersectsWith(particle.rectProfile))
  102. // {
  103. // inclusion.addParticles(particle);
  104. // goto particleJump;
  105. // }
  106. // else
  107. // {
  108. // int verticalDistancePx = this.globalSettings.distancesBetweenParticlesE, horizontalDistancePx = this.globalSettings.distancesBetweenParticlesT;
  109. // // 先比较垂直距离
  110. // if (this.globalSettings.rollingDirection == InclusionsGlobalSettings.RollingDirection.PORTRAIT)
  111. // {
  112. // if (particle.rectProfile.Bottom < inclusion.rectProfile.Top)
  113. // {
  114. // verticalDistancePx = inclusion.rectProfile.Top - particle.rectProfile.Bottom;
  115. // }
  116. // if (particle.rectProfile.Top > inclusion.rectProfile.Bottom)
  117. // {
  118. // verticalDistancePx = particle.rectProfile.Top - inclusion.rectProfile.Bottom;
  119. // }
  120. // if (particle.rectProfile.Right < inclusion.rectProfile.Left)
  121. // {
  122. // horizontalDistancePx = inclusion.rectProfile.Left - particle.rectProfile.Right;
  123. // }
  124. // if (particle.rectProfile.Left > inclusion.rectProfile.Right)
  125. // {
  126. // horizontalDistancePx = particle.rectProfile.Left - inclusion.rectProfile.Right;
  127. // }
  128. // }
  129. // else
  130. // {
  131. // if (particle.rectProfile.Bottom < inclusion.rectProfile.Top)
  132. // {
  133. // horizontalDistancePx = inclusion.rectProfile.Top - particle.rectProfile.Bottom;
  134. // }
  135. // if (particle.rectProfile.Top > inclusion.rectProfile.Bottom)
  136. // {
  137. // horizontalDistancePx = particle.rectProfile.Top - inclusion.rectProfile.Bottom;
  138. // }
  139. // if (particle.rectProfile.Right < inclusion.rectProfile.Left)
  140. // {
  141. // verticalDistancePx = inclusion.rectProfile.Left - particle.rectProfile.Right;
  142. // }
  143. // if (particle.rectProfile.Left > inclusion.rectProfile.Right)
  144. // {
  145. // verticalDistancePx = particle.rectProfile.Left - inclusion.rectProfile.Right;
  146. // }
  147. // }
  148. // if (verticalDistancePx * this.globalSettings.pxPerUnit <= this.globalSettings.distancesBetweenParticlesE &&
  149. // horizontalDistancePx * this.globalSettings.pxPerUnit <= this.globalSettings.distancesBetweenParticlesT)
  150. // {
  151. // inclusion.addParticles(particle);
  152. // goto particleJump;
  153. // }
  154. // }
  155. // }
  156. // Inclusion newInclusion = new Inclusion(this);
  157. // newInclusion.addParticles(particle);
  158. // inclusions.Add(newInclusion);
  159. // particleJump:;
  160. // }
  161. // return inclusions;
  162. //}
  163. //#endregion
  164. //#region 夹杂物划分夹杂物(合并)
  165. ///// <summary>
  166. ///// 夹杂物划分夹杂物(合并)
  167. ///// </summary>
  168. ///// <param name="inclusions">夹杂物列表</param>
  169. ///// <returns>夹杂物列表</returns>
  170. //protected virtual List<Inclusion> inclusionsDivideInclusion(List<Inclusion> inclusions)
  171. //{
  172. // // 第二次划分 夹杂物间划分
  173. // for (int i = 0; i < inclusions.Count; i++)
  174. // {
  175. // if (i > inclusions.Count - 1)
  176. // {
  177. // break;
  178. // }
  179. // Inclusion inclusion = inclusions[i];
  180. // for (int o = inclusions.Count - 1; o > i; o--)
  181. // {
  182. // if (o > inclusions.Count - 1)
  183. // {
  184. // break;
  185. // }
  186. // // 矩形相交
  187. // if (inclusion.rectProfile.IntersectsWith(inclusions[o].rectProfile))
  188. // {
  189. // inclusion.addParticles(inclusions[o].particles);
  190. // inclusions.RemoveAt(o);
  191. // }
  192. // else
  193. // {
  194. // if (inclusions[o].particles[0].shape != inclusion.particles[0].shape)
  195. // {
  196. // continue;
  197. // }
  198. // int verticalDistancePx = this.globalSettings.distancesBetweenParticlesE, horizontalDistancePx = this.globalSettings.distancesBetweenParticlesT;
  199. // // 先比较垂直距离
  200. // if (this.globalSettings.rollingDirection == InclusionsGlobalSettings.RollingDirection.PORTRAIT)
  201. // {
  202. // if (inclusions[o].rectProfile.Bottom < inclusion.rectProfile.Top)
  203. // {
  204. // verticalDistancePx = inclusion.rectProfile.Top - inclusions[o].rectProfile.Bottom;
  205. // }
  206. // if (inclusions[o].rectProfile.Top > inclusion.rectProfile.Bottom)
  207. // {
  208. // verticalDistancePx = inclusions[o].rectProfile.Top - inclusion.rectProfile.Bottom;
  209. // }
  210. // if (inclusions[o].rectProfile.Right < inclusion.rectProfile.Left)
  211. // {
  212. // horizontalDistancePx = inclusion.rectProfile.Left - inclusions[o].rectProfile.Right;
  213. // }
  214. // if (inclusions[o].rectProfile.Left > inclusion.rectProfile.Right)
  215. // {
  216. // horizontalDistancePx = inclusions[o].rectProfile.Left - inclusion.rectProfile.Right;
  217. // }
  218. // }
  219. // else
  220. // {
  221. // if (inclusions[o].rectProfile.Bottom < inclusion.rectProfile.Top)
  222. // {
  223. // horizontalDistancePx = inclusion.rectProfile.Top - inclusions[o].rectProfile.Bottom;
  224. // }
  225. // if (inclusions[o].rectProfile.Top > inclusion.rectProfile.Bottom)
  226. // {
  227. // horizontalDistancePx = inclusions[o].rectProfile.Top - inclusion.rectProfile.Bottom;
  228. // }
  229. // if (inclusions[o].rectProfile.Right < inclusion.rectProfile.Left)
  230. // {
  231. // verticalDistancePx = inclusion.rectProfile.Left - inclusions[o].rectProfile.Right;
  232. // }
  233. // if (inclusions[o].rectProfile.Left > inclusion.rectProfile.Right)
  234. // {
  235. // verticalDistancePx = inclusions[o].rectProfile.Left - inclusion.rectProfile.Right;
  236. // }
  237. // }
  238. // if (verticalDistancePx * this.globalSettings.pxPerUnit <= this.globalSettings.distancesBetweenParticlesE &&
  239. // horizontalDistancePx * this.globalSettings.pxPerUnit <= this.globalSettings.distancesBetweenParticlesT)
  240. // {
  241. // inclusion.addParticles(inclusions[o].particles);
  242. // inclusions.RemoveAt(o);
  243. // }
  244. // }
  245. // }
  246. // }
  247. // List<Inclusion> tobeCombined = new List<Inclusion>();
  248. // foreach (var item in inclusions)
  249. // {
  250. // if (item.particles.Count<3)
  251. // {
  252. // tobeCombined.Add(item);
  253. // }
  254. // }
  255. // inclusionsCombinationSplit(inclusions,tobeCombined);
  256. // return inclusions;
  257. //}
  258. //#endregion
  259. //#region 夹杂物组合
  260. ///// <summary>
  261. ///// 夹杂物组合
  262. ///// </summary>
  263. ///// <param name="allInclusions">全部的夹杂物</param>
  264. ///// <param name="tobeCombined">待合并的夹杂物</param>
  265. //public virtual List<Inclusion> inclusionsCombination(List<Inclusion> allInclusions, List<Inclusion> tobeCombined)
  266. //{
  267. // Inclusion inclusion = new Inclusion(this);
  268. // foreach (Inclusion item in tobeCombined)
  269. // {
  270. // inclusion.addParticles(item.particles);
  271. // allInclusions.Remove(item);
  272. // }
  273. // allInclusions.Add(inclusion);
  274. // return allInclusions;
  275. //}
  276. //#endregion
  277. //#region 夹杂物组合拆分
  278. ///// <summary>
  279. ///// 夹杂物组合拆分
  280. ///// </summary>
  281. ///// <param name="allInclusions">全部的夹杂物</param>
  282. ///// <param name="tobeCombined">待拆分的夹杂物</param>
  283. //public virtual List<Inclusion> inclusionsCombinationSplit(List<Inclusion> allInclusions, List<Inclusion> tobeCombined)
  284. //{
  285. // foreach (Inclusion item in tobeCombined)
  286. // {
  287. // foreach (Particle particle in item.particles)
  288. // {
  289. // Inclusion inclusion = new Inclusion(this);
  290. // inclusion.addParticles(particle);
  291. // allInclusions.Add(inclusion);
  292. // }
  293. // allInclusions.Remove(item);
  294. // }
  295. // return allInclusions;
  296. //}
  297. //#endregion
  298. ///// <summary>
  299. ///// 判断夹杂物类型
  300. ///// </summary>
  301. //public abstract void determineType(Inclusion inclusion);
  302. ///// <summary>
  303. ///// 边缘误差修正
  304. ///// </summary>
  305. ///// 根据视场边界修正所计算的夹杂物
  306. ///// <param name="inclusion"></param>
  307. //public abstract void edgeErrorsCorrection(List<Inclusion> inclusions);
  308. }
  309. }