Inclusion.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using static PaintDotNet.Base.DedicatedAnalysis.Inclusions.InclusionsGlobalSettings;
  6. namespace PaintDotNet.Base.DedicatedAnalysis.Inclusions.Model
  7. {
  8. /// <summary>
  9. /// 夹杂物(inclusion)
  10. ///
  11. /// </summary>
  12. public class Inclusion
  13. {
  14. /// <summary>
  15. /// 构成夹杂物的微粒集合
  16. /// </summary>
  17. public List<Particle> particles;
  18. /// <summary>
  19. /// 当前夹杂物判定所使用的标准
  20. /// </summary>
  21. public InclusionsStandard standard;
  22. /// <summary>
  23. /// 像素长度
  24. /// </summary>
  25. public double pixelLength;
  26. /// <summary>
  27. /// 像素宽度
  28. /// </summary>
  29. public double pixelWidth;
  30. /// <summary>
  31. /// 物理长度
  32. /// </summary>
  33. public double physicalLength;
  34. /// <summary>
  35. /// 物理宽度
  36. /// </summary>
  37. public double physicalWidth;
  38. /// <summary>
  39. /// 长宽比
  40. /// </summary>
  41. public double lwRatio;
  42. /// <summary>
  43. /// 评级-用于平均视场
  44. /// </summary>
  45. public double rating;
  46. /// <summary>
  47. /// 视场编号
  48. /// </summary>
  49. public int viewNum;
  50. /// <summary>
  51. /// 视场左限
  52. /// </summary>
  53. public int viewLeft;
  54. /// <summary>
  55. /// 视场上限
  56. /// </summary>
  57. public int viewTop;
  58. /// <summary>
  59. /// 视场右限
  60. /// </summary>
  61. public int viewRight;
  62. /// <summary>
  63. /// 视场下限
  64. /// </summary>
  65. public int viewBottom;
  66. /// <summary>
  67. /// 矩形轮廓
  68. /// </summary>
  69. public Rectangle rectProfile;
  70. /// <summary>
  71. /// 矩形轮廓(K法)
  72. /// </summary>
  73. public Rectangle rectKMethodProfile;
  74. public int offsetX;
  75. public int offsetY;
  76. // 排列方式
  77. public InclusionArrangement arrangement
  78. {
  79. get
  80. {
  81. return particles.Count > 1 ? InclusionArrangement.Aligned : InclusionArrangement.Scattered;
  82. }
  83. }
  84. /// <summary>
  85. /// 形状
  86. /// </summary>
  87. private ParticleShape shape_;
  88. public ParticleShape shape
  89. {
  90. get
  91. {
  92. return shape_;
  93. }
  94. set
  95. {
  96. shape_ = value;
  97. switch (value)
  98. {
  99. case ParticleShape.Elongated:
  100. {
  101. switch (this.arrangement)
  102. {
  103. case InclusionArrangement.Aligned:
  104. {
  105. this.classes = ClassesOfInclusions.γ;
  106. }
  107. break;
  108. case InclusionArrangement.Scattered:
  109. {
  110. this.classes = ClassesOfInclusions.α;
  111. }
  112. break;
  113. default: break;
  114. }
  115. }
  116. break;
  117. case ParticleShape.Globular:
  118. {
  119. switch (this.arrangement)
  120. {
  121. case InclusionArrangement.Aligned:
  122. {
  123. this.classes = ClassesOfInclusions.β;
  124. }
  125. break;
  126. case InclusionArrangement.Scattered:
  127. {
  128. this.classes = ClassesOfInclusions.δ;
  129. }
  130. break;
  131. default: break;
  132. }
  133. }
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// 形状系数(shape factor)
  142. /// </summary>
  143. public double shapeFactor;
  144. /// <summary>
  145. /// 面积(area)
  146. /// </summary>
  147. public double area;
  148. /// <summary>
  149. /// 面积(area)
  150. /// </summary>
  151. public double areaK;
  152. /// <summary>
  153. /// 直径(diameter)
  154. /// </summary>
  155. public double diameter;
  156. /// <summary>
  157. /// 评级行号
  158. /// </summary>
  159. public int rowNumber;
  160. /// <summary>
  161. /// 评级列号
  162. /// </summary>
  163. public int columnNumber;
  164. /// <summary>
  165. ///
  166. /// </summary>
  167. public int isEdit = 0;
  168. /// <summary>
  169. /// 夹杂物分类
  170. /// </summary>
  171. public ClassesOfInclusions classes;
  172. /// <summary>
  173. /// 夹杂物类型
  174. /// </summary>
  175. public TypesOfInclusions type;
  176. /// <summary>
  177. /// 夹杂物颜色
  178. /// </summary>
  179. private ColorOfInclusions color_;
  180. public ColorOfInclusions color {
  181. get
  182. {
  183. return color_;
  184. }
  185. set
  186. {
  187. color_ = value;
  188. this.standard.determineType(this);
  189. }
  190. }
  191. /// <summary>
  192. /// 化学特性
  193. /// </summary>
  194. public String chemicalCharacteristics = "";
  195. public bool superBig = false;
  196. /// <summary>
  197. /// 构造函数
  198. /// </summary>
  199. /// <param name="standard"></param>
  200. public Inclusion(InclusionsStandard standard)
  201. {
  202. this.particles = new List<Particle>();
  203. this.standard = standard;
  204. }
  205. public Inclusion(InclusionsStandard standard, PointF[] points)
  206. {
  207. this.particles = new List<Particle>();
  208. this.standard = standard;
  209. OpenCvSharp.Point[] cvPoints = new OpenCvSharp.Point[points.Length];
  210. for (int i = 0; i < points.Length; i++)
  211. {
  212. OpenCvSharp.Point point = new OpenCvSharp.Point(points[i].X, points[i].Y);
  213. cvPoints[i] = point;
  214. }
  215. Particle particle = new Particle(cvPoints, standard);
  216. this.addParticles(particle);
  217. this.isEdit = 1;
  218. }
  219. public void addParticles(Particle particle)
  220. {
  221. this.particles.Add(particle);
  222. parametersCalculation();
  223. }
  224. public void addParticles(List<Particle> particles)
  225. {
  226. this.particles.AddRange(particles);
  227. parametersCalculation();
  228. }
  229. public void removeParticles(Particle particle)
  230. {
  231. this.particles.Remove(particle);
  232. parametersCalculation();
  233. }
  234. /// <summary>
  235. /// 参数计算
  236. /// </summary>
  237. private void parametersCalculation()
  238. {
  239. if (this.particles.Count == 0)
  240. {
  241. return;
  242. }
  243. int minx = 0, miny = 0, maxx = 0, maxy = 0;
  244. int minxK = 0, minyK = 0, maxxK = 0, maxyK = 0;
  245. int countGlobular=0;
  246. for (int i = 0; i < this.particles.Count; i++)
  247. {
  248. Particle particle = this.particles[i];
  249. if (i == 0) {
  250. this.offsetX = particle.offsetX;
  251. this.offsetY = particle.offsetY;
  252. }
  253. if (particle.shape == ParticleShape.Globular)
  254. {
  255. countGlobular++;
  256. }
  257. if (i == 0)
  258. {
  259. minx = particle.rectProfile.Left;
  260. maxx = particle.rectProfile.Right;
  261. miny = particle.rectProfile.Top;
  262. maxy = particle.rectProfile.Bottom;
  263. minxK = particle.rectKMethodProfile.Left;
  264. maxxK = particle.rectKMethodProfile.Right;
  265. minyK = particle.rectKMethodProfile.Top;
  266. maxyK = particle.rectKMethodProfile.Bottom;
  267. continue;
  268. }
  269. if (particle.rectProfile.Left < minx)
  270. {
  271. minx = particle.rectProfile.Left;
  272. minxK = particle.rectKMethodProfile.Left;
  273. }
  274. if (particle.rectProfile.Right > maxx)
  275. {
  276. maxx = particle.rectProfile.Right;
  277. maxxK = particle.rectKMethodProfile.Left;
  278. }
  279. if (particle.rectProfile.Top < miny)
  280. {
  281. miny = particle.rectProfile.Top;
  282. minyK = particle.rectKMethodProfile.Left;
  283. }
  284. if (particle.rectProfile.Bottom > maxy)
  285. {
  286. maxy = particle.rectProfile.Bottom;
  287. maxyK = particle.rectKMethodProfile.Left;
  288. }
  289. }
  290. // 计算长度,宽度/直径,面积,形状系数,长宽比
  291. if (this.standard.globalSettings.rollingDirection == RollingDirection.PORTRAIT)
  292. {
  293. this.pixelLength = maxy - miny;
  294. this.pixelWidth = maxx - minx;
  295. }
  296. else
  297. {
  298. this.pixelWidth = maxy - miny;
  299. this.pixelLength = maxx - minx;
  300. }
  301. this.physicalLength = this.pixelLength * this.standard.globalSettings.pxPerUnit;
  302. this.physicalWidth = this.pixelWidth * this.standard.globalSettings.pxPerUnit;
  303. this.diameter = this.physicalLength > this.physicalWidth? this.physicalLength : this.physicalWidth;
  304. if (countGlobular/this.particles.Count > 0.8)
  305. {
  306. this.shape = ParticleShape.Globular;
  307. this.area = this.diameter* this.diameter*(Math.PI/4);
  308. }
  309. else
  310. {
  311. this.shape = ParticleShape.Elongated;
  312. this.area = this.physicalLength * this.physicalWidth * (Math.PI / 4);
  313. }
  314. this.areaK = this.particles.Sum(m => m.areaK);
  315. this.shapeFactor = Math.Log((Math.PI / 4)*(this.physicalLength * this.physicalLength / this.area) / Math.Log(this.physicalLength / 1));
  316. this.lwRatio = Math.Round(this.physicalLength / this.physicalWidth,2);
  317. this.rectProfile = new Rectangle(minx, miny, maxx - minx, maxy - miny);
  318. this.rectKMethodProfile = new Rectangle(minxK, minyK, maxx - minx, maxy - miny);
  319. this.standard.determineType(this);
  320. }
  321. }
  322. /// <summary>
  323. /// 排列方式
  324. /// </summary>
  325. public enum InclusionArrangement
  326. {
  327. /// <summary>
  328. /// 分散的
  329. /// </summary>
  330. Scattered,
  331. /// <summary>
  332. /// 排成行的
  333. /// </summary>
  334. Aligned
  335. }
  336. /// <summary>
  337. /// 形状
  338. /// </summary>
  339. public enum ParticleShape
  340. {
  341. /// <summary>
  342. /// 拉长的
  343. /// </summary>
  344. Elongated,
  345. /// <summary>
  346. /// 球状的
  347. /// </summary>
  348. Globular
  349. }
  350. /// <summary>
  351. /// 夹杂物分类
  352. /// </summary>
  353. public enum ClassesOfInclusions
  354. {
  355. α, β, γ, δ
  356. }
  357. }