Classify.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace OTSExtremum.Business
  8. {
  9. public class Classify
  10. {
  11. public enum Type
  12. {
  13. A = 0,
  14. B = 1,
  15. C = 2,
  16. D = 3,
  17. DS = 4
  18. }
  19. public static Dictionary<int, List<double>> getClassByGroup(string path)
  20. {
  21. Data.ParticleData particleData = new Data.ParticleData(path);
  22. DataTable particles = particleData.GetParticleListAndEm();
  23. List<double> A = new List<double>();
  24. List<double> B = new List<double>();
  25. List<double> C = new List<double>();
  26. List<double> D = new List<double>();
  27. List<double> DS = new List<double>();
  28. DataTable BD = particles.Clone();
  29. foreach (DataRow item in particles.Rows)
  30. {
  31. if (Convert.ToInt32(item["RectWidth"]) == 0)
  32. {
  33. continue;
  34. }
  35. //获取最大长度和最小宽度
  36. double h = Convert.ToDouble(item["DMAX"]);
  37. double w = Convert.ToDouble(item["DMIN"]);
  38. double dLengthWidthRatio = h / w;
  39. if (dLengthWidthRatio < 1)
  40. {
  41. dLengthWidthRatio = 1 / dLengthWidthRatio;
  42. }
  43. if (dLengthWidthRatio >= 3)//长宽比大于3的颗粒,根据化学元素不同,分为A类和C类
  44. {
  45. //A or C class
  46. string element = item["Element"].ToString();
  47. if (element.IndexOf("S-") > -1)
  48. {
  49. A.Add(Convert.ToDouble(item["PERIMETER"]));
  50. }
  51. else if (element.IndexOf("O-") > -1)
  52. {
  53. C.Add(Convert.ToDouble(item["PERIMETER"]));
  54. }
  55. }
  56. else//长宽比小于3的颗粒,有3种情况,一种是串条状的B类颗粒,一种是单独的D类颗粒,如果费雷特直径大于13则为DS类颗粒
  57. {
  58. // B, or D or DS
  59. // compute Feret's diameter
  60. double dFeretDiameter = Convert.ToDouble(item["PERIMETER"]);
  61. if (dFeretDiameter >= 13)
  62. {
  63. // DS
  64. DS.Add(Convert.ToDouble(item["PERIMETER"]));
  65. }
  66. else
  67. {
  68. //不能确定是B或D,先设为INVALID
  69. BD.Rows.Add(item.ItemArray);
  70. }
  71. }
  72. }
  73. foreach (DataRow item in BD.Rows)
  74. {
  75. double xs = 1142 / 1024;
  76. int left = Convert.ToInt32(item["RectLeft"]);
  77. int top = Convert.ToInt32(item["RectTop"]);
  78. int width = Convert.ToInt32(item["RectWidth"]);
  79. int height = Convert.ToInt32(item["RectHeight"]);
  80. int cenX = left + width / 2;
  81. int Bottom = top - height;
  82. bool isline = false;
  83. for (int i = 0; i < BD.Rows.Count; i++)
  84. {
  85. int leftB = Convert.ToInt32(item["RectLeft"]);
  86. int topB = Convert.ToInt32(item["RectTop"]);
  87. int widthB = Convert.ToInt32(item["RectWidth"]);
  88. int heightB = Convert.ToInt32(item["RectHeight"]);
  89. int cenBX = leftB + widthB / 2;
  90. int BottomB = topB - heightB;
  91. double dd = 0, ds = 0;
  92. ds = Math.Abs(cenX - cenBX);
  93. if (ds * xs < 10)//认为两个颗粒在一条竖直线上,但不在一起
  94. {
  95. if (Bottom > topB)//current particle is on the above
  96. {
  97. dd = Bottom - topB;
  98. if (dd * xs < 40)//认为这两个颗粒在一个串条上
  99. {
  100. isline = true;
  101. break;
  102. }
  103. }
  104. else if (BottomB > top) //current particle is on the below
  105. {
  106. dd = BottomB - top;
  107. if (dd * xs < 40)
  108. {
  109. isline = true;
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. if (isline)
  116. {
  117. B.Add(Convert.ToDouble(item["PERIMETER"]));
  118. }
  119. else
  120. {
  121. D.Add(Convert.ToDouble(item["PERIMETER"]));
  122. }
  123. }
  124. SetZeroBackfill(particles.Rows.Count, ref A);
  125. SetZeroBackfill(particles.Rows.Count, ref B);
  126. SetZeroBackfill(particles.Rows.Count, ref C);
  127. SetZeroBackfill(particles.Rows.Count, ref D);
  128. SetZeroBackfill(particles.Rows.Count, ref DS);
  129. Dictionary<int, List<double>> dic = new Dictionary<int, List<double>>();
  130. dic.Add((int)Type.A, A);
  131. dic.Add((int)Type.B, B);
  132. dic.Add((int)Type.C, C);
  133. dic.Add((int)Type.D, D);
  134. dic.Add((int)Type.DS, DS);
  135. return dic;
  136. }
  137. public static double[] getClass(string path)
  138. {
  139. Data.ParticleData particleData = new Data.ParticleData(path);
  140. DataTable particles = particleData.GetParticleListAndEm();
  141. double A=0, B=0, C=0, D=0, DS = 0;
  142. DataTable BD = particles.Clone();
  143. foreach (DataRow item in particles.Rows)
  144. {
  145. if (Convert.ToInt32(item["RectWidth"]) == 0)
  146. {
  147. continue;
  148. }
  149. //获取最大长度和最小宽度
  150. double h = Convert.ToDouble(item["DMAX"]);
  151. double w = Convert.ToDouble(item["DMIN"]);
  152. double dLengthWidthRatio = h / w;
  153. if (dLengthWidthRatio < 1)
  154. {
  155. dLengthWidthRatio = 1 / dLengthWidthRatio;
  156. }
  157. if (dLengthWidthRatio >= 3)//长宽比大于3的颗粒,根据化学元素不同,分为A类和C类
  158. {
  159. //A or C class
  160. string element = item["Element"].ToString();
  161. if (element.IndexOf("S-") > -1)
  162. {
  163. if (Convert.ToDouble(item["PERIMETER"]) > A)
  164. {
  165. A = Convert.ToDouble(item["PERIMETER"]);
  166. }
  167. }
  168. else if (element.IndexOf("O-") > -1)
  169. {
  170. if (Convert.ToDouble(item["PERIMETER"]) > C)
  171. {
  172. C = Convert.ToDouble(item["PERIMETER"]);
  173. }
  174. }
  175. }
  176. else//长宽比小于3的颗粒,有3种情况,一种是串条状的B类颗粒,一种是单独的D类颗粒,如果费雷特直径大于13则为DS类颗粒
  177. {
  178. // B, or D or DS
  179. // compute Feret's diameter
  180. double dFeretDiameter = Convert.ToDouble(item["PERIMETER"]);
  181. if (dFeretDiameter >= 13)
  182. {
  183. // DS
  184. if (Convert.ToDouble(item["PERIMETER"]) > DS)
  185. {
  186. DS = Convert.ToDouble(item["PERIMETER"]);
  187. }
  188. }
  189. else
  190. {
  191. //不能确定是B或D,先设为INVALID
  192. BD.Rows.Add(item.ItemArray);
  193. }
  194. }
  195. }
  196. foreach (DataRow item in BD.Rows)
  197. {
  198. double xs = 1142 / 1024;
  199. int left = Convert.ToInt32(item["RectLeft"]);
  200. int top = Convert.ToInt32(item["RectTop"]);
  201. int width = Convert.ToInt32(item["RectWidth"]);
  202. int height = Convert.ToInt32(item["RectHeight"]);
  203. int cenX = left + width / 2;
  204. int Bottom = top- height;
  205. bool isline = false;
  206. for (int i = 0; i < BD.Rows.Count; i++)
  207. {
  208. int leftB = Convert.ToInt32(item["RectLeft"]);
  209. int topB = Convert.ToInt32(item["RectTop"]);
  210. int widthB = Convert.ToInt32(item["RectWidth"]);
  211. int heightB = Convert.ToInt32(item["RectHeight"]);
  212. int cenBX = leftB + widthB / 2;
  213. int BottomB = topB - heightB;
  214. double dd = 0, ds = 0;
  215. ds =Math.Abs(cenX - cenBX);
  216. if (ds* xs < 10)//认为两个颗粒在一条竖直线上,但不在一起
  217. {
  218. if (Bottom > topB)//current particle is on the above
  219. {
  220. dd = Bottom - topB;
  221. if (dd* xs < 40)//认为这两个颗粒在一个串条上
  222. {
  223. isline= true;
  224. break;
  225. }
  226. }
  227. else if (BottomB > top) //current particle is on the below
  228. {
  229. dd = BottomB - top;
  230. if (dd* xs < 40)
  231. {
  232. isline= true;
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. if (isline)
  239. {
  240. if (Convert.ToDouble(item["PERIMETER"]) > B)
  241. {
  242. B = Convert.ToDouble(item["PERIMETER"]);
  243. }
  244. }
  245. else
  246. {
  247. if (Convert.ToDouble(item["PERIMETER"]) > D)
  248. {
  249. D = Convert.ToDouble(item["PERIMETER"]);
  250. }
  251. }
  252. }
  253. return new double[] {A,B,C,D,DS};
  254. }
  255. static void SetZeroBackfill(int count,ref List<double> ll)
  256. {
  257. for (int i = ll.Count; i < count; i++)
  258. {
  259. ll.Add(0);
  260. }
  261. ll.Sort((x, y) => -x.CompareTo(y));
  262. }
  263. }
  264. }