GrainSizeStandardClass.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. using OpenCvSharp;
  2. using PaintDotNet.Base.CommTool;
  3. using PaintDotNet.Base.SettingModel;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Windows.Forms;
  9. using PaintDotNet.Base.DedicatedAnalysis.GrainSize;
  10. namespace PaintDotNet.DedicatedAnalysis.GrainSizeStandard.IntegrationClass
  11. {
  12. class GrainSizeStandardClass
  13. {
  14. /// <summary>
  15. /// 晶粒度截点样式集成
  16. /// </summary>
  17. public GrainPointStyleClass styleClass = new GrainPointStyleClass();
  18. /// <summary>
  19. /// 需要根据当前操作图片备份和读取的晶粒度数据
  20. /// </summary>
  21. public GrainSizeStandardModel standardModel = new GrainSizeStandardModel();
  22. /// <summary>
  23. /// 根据图片保存晶粒度数据
  24. /// </summary>
  25. public Dictionary<string, GrainSizeStandardModel> standardDict = new Dictionary<string, GrainSizeStandardModel>();
  26. /// <summary>
  27. /// 获取设置颜色的集合,按照等级从小到大排序
  28. /// </summary>
  29. public List<Vec4b> setColorOfGrades = new List<Vec4b>();
  30. /// <summary>
  31. /// 选择的评定方法
  32. /// </summary>
  33. public MethodOfAssessment methodOfAssessment;
  34. /// <summary>
  35. /// 实现手动添加在原图情况下的截点位置跟随辅助线的移动而移动
  36. /// </summary>
  37. public void MoveOriginCutOffPoints(float offsetX, float offsetY, string currentGuideName, Mat mat)
  38. {
  39. standardModel.linePointList.Clear();
  40. styleClass.styleModel.straightLinePointList.Clear();
  41. styleClass.styleModel.roundLinePointList.Clear();
  42. //获取图形上所有的点
  43. standardModel.linePointList.AddRange(GuideClass.getGuidePointListAndClassLine(mat, currentGuideName, out styleClass.styleModel.straightLinePointList, out styleClass.styleModel.roundLinePointList));
  44. //##21247
  45. styleClass.styleModel.MoveOriginCutOffPoints((int)offsetX, (int)offsetY, offsetX, offsetY);
  46. }
  47. /// <summary>
  48. /// 刷新图形边线点集合
  49. /// </summary>
  50. public void AddRectangleToRectangleFCaps(string imagesKey/* = null*/, int grainMethodType, Mat imageMat, string currentGuideName, int matLevel, Mat mat, double physical_Micron)
  51. {
  52. GrainSizeStandardModel standardModel = this.standardModel;
  53. GrainPointStyleModel styleModel = styleClass.styleModel;
  54. if (imagesKey != null)
  55. {
  56. if (!this.standardDict.ContainsKey(imagesKey))
  57. this.standardDict.Add(imagesKey, this.standardModel.cloneListParamModel());
  58. standardModel = this.standardDict[imagesKey];
  59. if (!styleClass.styleDict.ContainsKey(imagesKey))
  60. styleClass.styleDict.Add(imagesKey, styleClass.styleModel.cloneListParamModel());
  61. styleModel = styleClass.styleDict[imagesKey];
  62. }
  63. if (grainMethodType == 1)
  64. {
  65. standardModel.linePointList.Clear();
  66. int guideStyle;
  67. //获取图形上所有的点
  68. standardModel.linePointList.AddRange(GuideClass.getGuideAreaPointList(imageMat.Width, imageMat.Height, currentGuideName, physical_Micron, out guideStyle));
  69. return;
  70. }
  71. standardModel.linePointList.Clear();
  72. styleModel.straightLinePointList.Clear();
  73. styleModel.roundLinePointList.Clear();
  74. //获取图形上所有的点
  75. standardModel.linePointList.AddRange(GuideClass.getGuidePointListAndClassLine(mat, currentGuideName, out styleModel.straightLinePointList, out styleModel.roundLinePointList));
  76. //垂直辅助线 / 水平辅助线
  77. bool vGuideStylesOrHGuideStyles = GuideClass.IsVGuideStylesOrHGuideStyles(currentGuideName);
  78. //自动添加截点,并赋值截距列表
  79. styleModel.AddRectangleToRectangleFCapsAndLine(mat, matLevel, vGuideStylesOrHGuideStyles, mat/*this.documentWorkspace.CompositionSurface*/.Width
  80. , mat/*this.documentWorkspace.CompositionSurface*/.Height);
  81. }
  82. /// <summary>
  83. /// 标准的方法类型(0 截点法/截距法, 1 面积法)
  84. /// </summary>
  85. public int GrainMethodType
  86. {
  87. get
  88. {
  89. if (methodOfAssessment == null)
  90. {
  91. return -1;//should never go here
  92. }
  93. return methodOfAssessment.grainMethodType;
  94. }
  95. }
  96. /// <summary>
  97. /// 晶粒度标准构造窗口时调用
  98. /// </summary>
  99. /// <param name="methodOfAssessment"></param>
  100. /// <returns></returns>
  101. public bool InitDialogMethod(MethodOfAssessment methodOfAssessment)
  102. {
  103. if (methodOfAssessment == null)
  104. return false;
  105. this.methodOfAssessment = methodOfAssessment;
  106. if (methodOfAssessment.grainMethodType == 1)
  107. GuideClass.createGuideAreaClassModelsNamed(this.methodOfAssessment.guideXmlName);
  108. else if (methodOfAssessment.grainMethodType == 0)
  109. GuideClass.createGuideClassModelsNamed(this.methodOfAssessment.guideXmlName);
  110. return true;
  111. }
  112. /// <summary>
  113. /// 晶粒度初始化辅助线组件时调用
  114. /// </summary>
  115. /// <returns></returns>
  116. public bool InitComboboxValue()
  117. {
  118. if (methodOfAssessment == null)
  119. return false;
  120. if (methodOfAssessment.grainMethodType == 1)
  121. GuideClass.createGuideAreaClassModelsNamed(this.methodOfAssessment.guideXmlName);
  122. else if (methodOfAssessment.grainMethodType == 0)
  123. GuideClass.createGuideClassModelsNamed(this.methodOfAssessment.guideXmlName);
  124. return true;
  125. }
  126. //Mat OriginalPicture2 = OriginalPicture0.Clone();//备注:改成切图方案更稳定些
  127. ////int Rows = OriginalPicture0.Rows;
  128. ////int Cols = OriginalPicture0.Cols;
  129. //////List<PointF> pointFs1 = GuideClass.getGuideAreaPointList(OriginalPicture0.Width, OriginalPicture0.Height, cal_GuideModel.oldSelectItem/*currentGuideName*/, physical_Micron);
  130. //////int[] point = new int[2];
  131. //////多通道图像的格式化
  132. ////foreach (var itemP in pointFs)
  133. //// OriginalPicture2.Set<Vec4b>(new int[] { (int)itemP.Y, (int)itemP.X }, new Vec4b(0, 0, 0, 255));
  134. //////Cv2.Erode(OriginalPicture1, OriginalPicture1, null, null, 1);
  135. ///
  136. //Cv2.Ellipse(imageMask, new RotatedRect(new Point2f((float)(rectangleF.X + rectangleF.Width / 2), (float)(rectangleF.Y + rectangleF.Height / 2)), new Size2f(rectangleF.Width, rectangleF.Height), 0), new Scalar(255), -1);
  137. //if (imageMask != null) imageMask.Dispose();
  138. //GC.Collect();
  139. //Cv2.ImShow("OriginalPicture1", OriginalPicture1);
  140. //Cv2.WaitKey();
  141. /// <summary>
  142. /// 刷新晶粒编号
  143. /// </summary>
  144. /// <param name="targetSelection">目标选择 1不包含边缘 2边缘只处理内部 3边缘处理内外部</param>
  145. /// <returns>返回晶粒个数</returns>
  146. public double GetNumberOfGrains(bool cal_RectFlag, int targetSelection, GrainSizeStandardModel cal_StandardModel, GrainSizeGuideModel cal_GuideModel, Mat OriginalPicture0, Mat analyzeMat, bool analyzePhase, double physical_Micron, out Mat temp)
  147. {
  148. int guideStyle;
  149. List<PointF> pointFs = GuideClass.getGuideAreaPointList(OriginalPicture0.Width, OriginalPicture0.Height, cal_GuideModel.oldSelectItem/*currentGuideName*/, physical_Micron, out guideStyle);// cal_StandardModel.linePointList;
  150. RectangleF rectangleF = cal_GuideModel.RectangleFLine;
  151. Mat erzhi;
  152. if (targetSelection == 2)
  153. {
  154. Mat OriginalPicture1 = new Mat();
  155. Mat imageMask = new Mat(OriginalPicture0.Size(), MatType.CV_8UC1, new Scalar(0));
  156. if (guideStyle == 1)
  157. Cv2.Circle(imageMask, (int)(rectangleF.X + rectangleF.Width / 2), (int)(rectangleF.Y + rectangleF.Width / 2), (int)(rectangleF.Width / 2), new Scalar(255), -1);
  158. else if (guideStyle == 2)
  159. {
  160. OpenCvSharp.Point[][] arr = new OpenCvSharp.Point[1][];
  161. arr[0] = new OpenCvSharp.Point[] { new OpenCvSharp.Point((int)(rectangleF.X), (int)(rectangleF.Y))
  162. , new OpenCvSharp.Point((int)(rectangleF.X+ rectangleF.Width), (int)(rectangleF.Y))
  163. , new OpenCvSharp.Point((int)(rectangleF.X+ rectangleF.Width), (int)(rectangleF.Y+ rectangleF.Height))
  164. , new OpenCvSharp.Point((int)(rectangleF.X), (int)(rectangleF.Y+ rectangleF.Height)) };
  165. Cv2.FillPoly(imageMask, arr, new Scalar(255));
  166. }
  167. OriginalPicture0.CopyTo(OriginalPicture1, imageMask);
  168. erzhi = OriginalPicture1;
  169. }
  170. else if (targetSelection == 1)
  171. { erzhi = OriginalPicture0; }
  172. else//targetSelection == 3
  173. { erzhi = OriginalPicture0; }
  174. Cv2.CvtColor(erzhi, erzhi, ColorConversionCodes.BGR2GRAY);
  175. Cv2.Threshold(erzhi, erzhi, 100, 255, ThresholdTypes.Binary);//##
  176. //Cv2.AdaptiveThreshold(erzhi, erzhi, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 99, 10);
  177. OpenCvSharp.Point[][] contours;
  178. HierarchyIndex[] hierarchy;
  179. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  180. List<OpenCvSharp.Point[]> pointss = new List<OpenCvSharp.Point[]>();
  181. foreach (OpenCvSharp.Point[] contour in contours)
  182. {
  183. if (contour.Length < 100)
  184. {
  185. foreach (OpenCvSharp.Point pointI in contour)
  186. {
  187. erzhi.Set<byte>(pointI.Y, pointI.X, 255);
  188. }
  189. pointss.Add(contour);
  190. }
  191. }
  192. Cv2.FillPoly(erzhi, pointss, new Scalar(255));
  193. Mat labelMat = new Mat();
  194. Mat stats = new Mat();
  195. Mat centroids = new Mat();
  196. // 连通域数量
  197. int nccomps = Cv2.ConnectedComponentsWithStats(erzhi, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  198. if (cal_StandardModel.currentLabelMat != null)
  199. {
  200. cal_StandardModel.currentLabelMat.Dispose();
  201. cal_StandardModel.currentLabelMat = null;
  202. }
  203. cal_StandardModel.currentLabelMat = labelMat.Clone();
  204. //存储矩阵数据
  205. if (cal_StandardModel.currentStats != null)
  206. {
  207. cal_StandardModel.currentStats.Dispose();
  208. cal_StandardModel.currentStats = null;
  209. }
  210. cal_StandardModel.currentStats = stats.Clone();
  211. // 连通域质点
  212. Dictionary<int, Point2d> keyValues = new Dictionary<int, Point2d>();
  213. cal_StandardModel.massLabelPoints.Clear();
  214. for (int label = 1; label < centroids.Height; label++)
  215. keyValues[label] = new Point2d((int)centroids.At<double>(label, 0), (int)centroids.At<double>(label, 1));
  216. cal_StandardModel.currentNccompSelect.Clear();
  217. // 矩形边线上的连通域
  218. ArrayList nccompSelect = new ArrayList();
  219. // 矩形四个角的连通域
  220. ArrayList rectSelect = new ArrayList();
  221. if (cal_RectFlag)
  222. {
  223. int label = labelMat.At<int>((int)rectangleF.Y, (int)rectangleF.X);//PointF pointF
  224. if (!rectSelect.Contains(label)) rectSelect.Add(label);
  225. label = labelMat.At<int>((int)rectangleF.Y, (int)(rectangleF.X + rectangleF.Width - 1));
  226. if (!rectSelect.Contains(label)) rectSelect.Add(label);
  227. label = labelMat.At<int>((int)(rectangleF.Y + rectangleF.Height - 1), (int)(rectangleF.X + rectangleF.Width - 1));
  228. if (!rectSelect.Contains(label)) rectSelect.Add(label);
  229. label = labelMat.At<int>((int)(rectangleF.Y + rectangleF.Height - 1), (int)rectangleF.X);
  230. if (!rectSelect.Contains(label)) rectSelect.Add(label);
  231. }
  232. if (targetSelection == 2 || targetSelection == 3)
  233. foreach (PointF pointF in pointFs)//切换目标选择
  234. {
  235. if (labelMat.Width <= pointF.X || labelMat.Height <= pointF.Y || pointF.X < 0 || pointF.Y < 0)
  236. continue;
  237. //判断是否在晶界上面
  238. Vec4b vec4B = analyzeMat.At<Vec4b>((int)pointF.Y, (int)pointF.X);
  239. if (analyzePhase && vec4B.Item3 != 0 || !analyzePhase && vec4B.Item0 == 0)
  240. continue;
  241. int label = labelMat.At<int>((int)pointF.Y, (int)pointF.X);
  242. ////去掉四个角的晶粒
  243. //if (cal_RectFlag && nccompSelect.Contains(label) && keyValues.ContainsKey(label) && keyValues[label] != null)
  244. //{
  245. // 标记和跳过的操作 待完善
  246. //}
  247. if (!rectSelect.Contains(label) && !nccompSelect.Contains(label) && keyValues.ContainsKey(label) && keyValues[label] != null)
  248. {
  249. nccompSelect.Add(label);
  250. cal_StandardModel.currentNccompSelect.Add(label);
  251. if (keyValues.ContainsKey(label) && keyValues[label] != null)
  252. cal_StandardModel.massLabelPoints.Add(label, new List<double>() { 0, 0, keyValues[label].X, keyValues[label].Y });
  253. }
  254. }
  255. analyzeMat.Dispose();
  256. analyzeMat = null;
  257. // 矩形内的连通域
  258. cal_StandardModel.within = 0;
  259. ArrayList inccompSelect = new ArrayList();
  260. GrainSizeGuideAreaMethodModel grainSizeGuideStyleModel = GuideClass.GrainSizeGuideAreaMethodModel;
  261. // 圆形
  262. if (grainSizeGuideStyleModel.circularGuideStyles != null)
  263. {
  264. foreach (var circularGuideStyle in grainSizeGuideStyleModel.circularGuideStyles)
  265. {
  266. if (cal_GuideModel.oldSelectItem/*currentGuideName*/.Equals(circularGuideStyle.tag))
  267. {
  268. foreach (var keyValue in keyValues)
  269. {
  270. //求点到圆心的距离
  271. double c = BasicCalculationHelper.GetDistance(new PointF((float)keyValue.Value.X, (float)keyValue.Value.Y), new PointF(rectangleF.X + rectangleF.Width / 2, rectangleF.Y + rectangleF.Height / 2), 10);
  272. if (!nccompSelect.Contains(keyValue.Key) && c < GuideClass.GetMat_lineDiameter(physical_Micron) / 2)
  273. {
  274. if (!inccompSelect.Contains(keyValue.Key))
  275. {
  276. cal_StandardModel.within++;
  277. inccompSelect.Add(keyValue.Key);
  278. cal_StandardModel.currentNccompSelect.Add(keyValue.Key);
  279. if (keyValue.Value != null)
  280. cal_StandardModel.massLabelPoints.Add(keyValue.Key, new List<double>() { 0, 1, keyValue.Value.X, keyValue.Value.Y });
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }
  287. // 矩形
  288. if (grainSizeGuideStyleModel.rectangleGuideStyles != null)
  289. {
  290. foreach (var rectangleGuideStyle in grainSizeGuideStyleModel.rectangleGuideStyles)
  291. {
  292. if (cal_GuideModel.oldSelectItem/*currentGuideName*/.Equals(rectangleGuideStyle.tag))
  293. {
  294. foreach (var keyValue in keyValues)
  295. {
  296. if (!rectSelect.Contains(keyValue.Key) && !nccompSelect.Contains(keyValue.Key) && keyValue.Value.X > rectangleF.X && keyValue.Value.X < rectangleF.Right
  297. && keyValue.Value.Y > rectangleF.Y && keyValue.Value.Y < rectangleF.Bottom)
  298. {
  299. if (!inccompSelect.Contains(keyValue.Key))
  300. {
  301. cal_StandardModel.within++;
  302. inccompSelect.Add(keyValue.Key);
  303. cal_StandardModel.currentNccompSelect.Add(keyValue.Key);
  304. if (keyValue.Value != null)
  305. cal_StandardModel.massLabelPoints.Add(keyValue.Key, new List<double>() { 0, 1, keyValue.Value.X, keyValue.Value.Y });
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. //根据等级设置晶粒颜色
  313. temp = this.getMat2ColorOfLabelMat(cal_StandardModel, nccomps, physical_Micron);
  314. return cal_StandardModel.within + (double)nccompSelect.Count / 2;
  315. }
  316. public GrainSizeGuideClass GuideClass { get; } = new GrainSizeGuideClass();
  317. public Mat[] CurrentStatsArray
  318. {
  319. get
  320. {
  321. return standardModel.currentStatsArray;
  322. }
  323. set
  324. {
  325. standardModel.currentStatsArray = value;
  326. }
  327. }
  328. public Mat[] CurrentLabelMatArray
  329. {
  330. get
  331. {
  332. return standardModel.currentLabelMatArray;
  333. }
  334. set
  335. {
  336. standardModel.currentLabelMatArray = value;
  337. }
  338. }
  339. /// <summary>
  340. /// 根据等级设置晶粒颜色
  341. /// </summary>
  342. /// <param name="nccomps">连通域数量</param>
  343. public Mat getMat2ColorOfLabelMat(GrainSizeStandardModel cal_StandardModel, int nccomps, double physical_Micron)
  344. {
  345. if (cal_StandardModel.currentLabelMat == null || cal_StandardModel.currentStats == null)
  346. return null;
  347. Mat temp = new Mat(cal_StandardModel.currentLabelMat.Size(), MatType.CV_8UC4);//##
  348. if (setColorOfGrades.Count == 0)
  349. {
  350. GrainColorsStyleModel colorsStyleModel = XmlSerializeHelper.DESerializer<GrainColorsStyleModel>(FileOperationHelper.ReadStringFromFile(Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\GrainSizeAnalyze\\GrainColorsStyleModel.xml", System.IO.FileMode.Open));
  351. if (colorsStyleModel == null)
  352. return null;
  353. Color color1 = Color.FromArgb(colorsStyleModel.ColorStyle1.pointColor);
  354. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  355. color1 = Color.FromArgb(colorsStyleModel.ColorStyle2.pointColor);
  356. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  357. color1 = Color.FromArgb(colorsStyleModel.ColorStyle3.pointColor);
  358. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  359. color1 = Color.FromArgb(colorsStyleModel.ColorStyle4.pointColor);
  360. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  361. color1 = Color.FromArgb(colorsStyleModel.ColorStyle5.pointColor);
  362. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  363. color1 = Color.FromArgb(colorsStyleModel.ColorStyle6.pointColor);
  364. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  365. color1 = Color.FromArgb(colorsStyleModel.ColorStyle7.pointColor);
  366. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  367. color1 = Color.FromArgb(colorsStyleModel.ColorStyle8.pointColor);
  368. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  369. color1 = Color.FromArgb(colorsStyleModel.ColorStyle9.pointColor);
  370. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  371. color1 = Color.FromArgb(colorsStyleModel.ColorStyle10.pointColor);
  372. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  373. color1 = Color.FromArgb(colorsStyleModel.ColorStyle11.pointColor);
  374. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  375. color1 = Color.FromArgb(colorsStyleModel.ColorStyle12.pointColor);
  376. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  377. color1 = Color.FromArgb(colorsStyleModel.ColorStyle13.pointColor);
  378. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  379. color1 = Color.FromArgb(colorsStyleModel.ColorStyle14.pointColor);
  380. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  381. color1 = Color.FromArgb(colorsStyleModel.ColorStyle15.pointColor);
  382. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  383. color1 = Color.FromArgb(colorsStyleModel.ColorStyle16.pointColor);
  384. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  385. color1 = Color.FromArgb(colorsStyleModel.ColorStyle17.pointColor);
  386. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  387. color1 = Color.FromArgb(colorsStyleModel.ColorStyle18.pointColor);
  388. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  389. color1 = Color.FromArgb(colorsStyleModel.ColorStyle19.pointColor);
  390. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  391. color1 = Color.FromArgb(colorsStyleModel.ColorStyle20.pointColor);
  392. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  393. color1 = Color.FromArgb(colorsStyleModel.ColorStyle21.pointColor);
  394. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  395. color1 = Color.FromArgb(colorsStyleModel.ColorStyle22.pointColor);
  396. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  397. color1 = Color.FromArgb(colorsStyleModel.ColorStyle23.pointColor);
  398. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  399. color1 = Color.FromArgb(colorsStyleModel.ColorStyle24.pointColor);
  400. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  401. color1 = Color.FromArgb(colorsStyleModel.ColorStyle25.pointColor);
  402. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  403. color1 = Color.FromArgb(colorsStyleModel.ColorStyle26.pointColor);
  404. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  405. color1 = Color.FromArgb(colorsStyleModel.ColorStyle27.pointColor);
  406. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  407. color1 = Color.FromArgb(colorsStyleModel.ColorStyle28.pointColor);
  408. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  409. color1 = Color.FromArgb(colorsStyleModel.ColorStyle29.pointColor);
  410. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  411. color1 = Color.FromArgb(colorsStyleModel.ColorStyle30.pointColor);
  412. setColorOfGrades.Add(new Vec4b(color1.B, color1.G, color1.R, color1.A));
  413. }
  414. //根据等级设置晶粒颜色
  415. if (setColorOfGrades.Count > 0)
  416. {
  417. int label;
  418. double aec;
  419. Decimal NA;
  420. ////1013###19377##面积法位置,使用晶粒的实际面积去标准中的表进行对应,获取截距后,再进行平均的计算
  421. double[] lengths = {3.88, 7.75 , 10.96, 15.50, 21.92, 31.0, 43.84, 62.0,
  422. 87.68, 124, 175.36, 248, 350.73, 496, 701.45, 992, 1402.9, 1984.0, 2805.8, 3968.0, 5611.6,
  423. 7936.0, 11223.2, 15872.0, 22446.4, 31744.1, 44892.9, 63488.1, 89785.8, 126976.3};
  424. //Dictionary<MeasurementUnit, double> measurementUnitDictionary = Startup.instance.rules;
  425. ////选择标尺的物理长度(1倍下的 微米/像素)
  426. //double physical_area_length_Micron = measurementUnitDictionary[MeasurementUnit.Micron];
  427. // 差值
  428. decimal differenceNew;
  429. decimal differenceOld;
  430. int first;
  431. for (int y = 0; y < temp.Rows; y++)
  432. {
  433. for (int x = 0; x < temp.Cols; x++)
  434. {
  435. label = cal_StandardModel.currentLabelMat.At<int>(y, x);
  436. if (!cal_StandardModel.currentNccompSelect.Contains(label))
  437. continue;
  438. aec = cal_StandardModel.currentStats.At<int>(label/*hcomp*/, 4) * physical_Micron * physical_Micron;//晶粒面积
  439. aec = aec * 0.001 * 0.001;
  440. NA = Convert.ToDecimal(1.0/*grainNumber*/ / aec);
  441. differenceOld = Math.Abs(Convert.ToDecimal(Math.Max((double)NA - 2.88, (double)NA - 126977.3)));
  442. first = 0;
  443. if ((double)NA > lengths[lengths.Length - 1])
  444. first = lengths.Length - 1;
  445. else
  446. foreach (double item in lengths)
  447. {
  448. differenceNew = Math.Abs(NA - Convert.ToDecimal(item));
  449. if (differenceNew < differenceOld)
  450. {
  451. differenceOld = Math.Abs(NA - Convert.ToDecimal(item));
  452. first++;
  453. }
  454. //else
  455. // break;
  456. }
  457. if (first >= lengths.Length)
  458. first = lengths.Length - 1;
  459. else if (first < 0)
  460. first = 0;
  461. temp.Set<Vec4b>(y, x, setColorOfGrades[first/*label*/]);
  462. }
  463. }
  464. return temp;
  465. }
  466. //随机设置晶粒颜色
  467. Random rd = new Random();
  468. List<Vec4b> colors = new List<Vec4b>(nccomps);
  469. colors.Add(new Vec4b(0, 0, 0, 0));
  470. for (int i = 1; i < nccomps; i++)
  471. {
  472. colors.Add(new Vec4b(((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), 255));
  473. }
  474. for (int y = 0; y < temp.Rows; y++)
  475. {
  476. for (int x = 0; x < temp.Cols; x++)
  477. {
  478. int label = cal_StandardModel.currentLabelMat.At<int>(y, x);
  479. if (cal_StandardModel.currentNccompSelect.Contains(label))
  480. temp.Set<Vec4b>(y, x, colors[label]);
  481. }
  482. }
  483. return temp;
  484. }
  485. }
  486. }