MethodAreaDialog.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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.Data;
  8. using System.Drawing;
  9. using System.Drawing.Drawing2D;
  10. using System.Windows.Forms;
  11. namespace PaintDotNet.DedicatedAnalysis.GrainSize.GBT4296
  12. {
  13. internal class MethodAreaDialog : GrainSizeStandardDialog
  14. {
  15. #region 组件
  16. private GroupBox groupBox11;
  17. private Button button4;
  18. private CheckBox checkBox11;
  19. private CheckBox checkBox5_0;
  20. private CheckBox checkBox4_0;
  21. private CheckBox checkBox3_0;
  22. private RadioButton radioButton_DarkGray;
  23. #endregion 组件
  24. #region 构造函数
  25. public MethodAreaDialog(AppWorkspace appWorkspace) : base(appWorkspace, true, true)
  26. {
  27. }
  28. #endregion 构造函数
  29. #region 初始化组件
  30. private void InitializeLanguageText()
  31. {
  32. this.radioButton_DarkGray.Text = PdnResources.GetString("Menu.Darkgray.text");
  33. this.groupBox11.Text = PdnResources.GetString("Menu.Displayparamssettings.text");
  34. this.button4.Text = PdnResources.GetString("Menu.Refresh.text");
  35. this.checkBox11.Text = PdnResources.GetString("Menu.Thegrainsareshownindifferentcolors.text");
  36. this.checkBox5_0.Text = PdnResources.GetString("Menu.Displaygrainnumber.text");
  37. this.checkBox4_0.Text = PdnResources.GetString("Menu.displaygrid.text");
  38. this.checkBox3_0.Text = PdnResources.GetString("Menu.displaygrainboundaries.text");
  39. this.Text = "GBT4296(" + PdnResources.GetString("Menu.Dedicatedanalysis.Nmetals.Averagegraimethod.text") + ")";
  40. }
  41. private void InitializeComponent()
  42. {
  43. this.radioButton_DarkGray = new System.Windows.Forms.RadioButton();
  44. this.groupBox11 = new System.Windows.Forms.GroupBox();
  45. this.button4 = new System.Windows.Forms.Button();
  46. this.checkBox11 = new System.Windows.Forms.CheckBox();
  47. this.checkBox5_0 = new System.Windows.Forms.CheckBox();
  48. this.checkBox4_0 = new System.Windows.Forms.CheckBox();
  49. this.checkBox3_0 = new System.Windows.Forms.CheckBox();
  50. this.groupBox_analysisResult1.SuspendLayout();
  51. this.groupBox11.SuspendLayout();
  52. this.SuspendLayout();
  53. //
  54. // radioButton_DarkGray
  55. //
  56. this.radioButton_DarkGray.AutoSize = true;
  57. this.radioButton_DarkGray.Location = new System.Drawing.Point(178, 20);
  58. this.radioButton_DarkGray.Name = "radioButton_DarkGray";
  59. this.radioButton_DarkGray.Size = new System.Drawing.Size(59, 16);
  60. this.radioButton_DarkGray.TabIndex = 38;
  61. this.radioButton_DarkGray.TabStop = true;
  62. this.radioButton_DarkGray.UseVisualStyleBackColor = true;
  63. //
  64. // groupBox11
  65. //
  66. this.groupBox11.Controls.Add(this.button4);
  67. this.groupBox11.Controls.Add(this.checkBox11);
  68. this.groupBox11.Controls.Add(this.checkBox5_0);
  69. this.groupBox11.Controls.Add(this.checkBox4_0);
  70. this.groupBox11.Controls.Add(this.checkBox3_0);
  71. this.groupBox11.Location = new System.Drawing.Point(179, 553);
  72. this.groupBox11.Name = "groupBox11";
  73. this.groupBox11.Size = new System.Drawing.Size(356, 71);
  74. this.groupBox11.TabIndex = 38;
  75. this.groupBox11.TabStop = false;
  76. //
  77. // button4
  78. //
  79. this.button4.Anchor = System.Windows.Forms.AnchorStyles.Right;
  80. this.button4.Location = new System.Drawing.Point(259, 18);
  81. this.button4.Name = "button4";
  82. this.button4.Size = new System.Drawing.Size(75, 23);
  83. this.button4.TabIndex = 18;
  84. this.button4.UseVisualStyleBackColor = true;
  85. this.button4.Click += new System.EventHandler(this.button4_Click);
  86. //
  87. // checkBox11
  88. //
  89. this.checkBox11.AutoSize = true;
  90. this.checkBox11.Checked = true;
  91. this.checkBox11.CheckState = System.Windows.Forms.CheckState.Checked;
  92. this.checkBox11.Location = new System.Drawing.Point(139, 44);
  93. this.checkBox11.Name = "checkBox11";
  94. this.checkBox11.Size = new System.Drawing.Size(132, 16);
  95. this.checkBox11.TabIndex = 3;
  96. this.checkBox11.UseVisualStyleBackColor = true;
  97. this.checkBox11.CheckedChanged += new System.EventHandler(this.checkBox11_CheckedChanged);
  98. //
  99. // checkBox5_0
  100. //
  101. this.checkBox5_0.AutoSize = true;
  102. this.checkBox5_0.Checked = true;
  103. this.checkBox5_0.CheckState = System.Windows.Forms.CheckState.Checked;
  104. this.checkBox5_0.Location = new System.Drawing.Point(140, 22);
  105. this.checkBox5_0.Name = "checkBox5_0";
  106. this.checkBox5_0.Size = new System.Drawing.Size(96, 16);
  107. this.checkBox5_0.TabIndex = 2;
  108. this.checkBox5_0.UseVisualStyleBackColor = true;
  109. this.checkBox5_0.CheckedChanged += new System.EventHandler(this.checkBox5_0_CheckedChanged);
  110. //
  111. // checkBox4_0
  112. //
  113. this.checkBox4_0.AutoSize = true;
  114. this.checkBox4_0.Checked = true;
  115. this.checkBox4_0.CheckState = System.Windows.Forms.CheckState.Checked;
  116. this.checkBox4_0.Location = new System.Drawing.Point(15, 44);
  117. this.checkBox4_0.Name = "checkBox4_0";
  118. this.checkBox4_0.Size = new System.Drawing.Size(72, 16);
  119. this.checkBox4_0.TabIndex = 1;
  120. this.checkBox4_0.UseVisualStyleBackColor = true;
  121. this.checkBox4_0.CheckedChanged += new System.EventHandler(this.checkBox4_0_CheckedChanged);
  122. //
  123. // checkBox3_0
  124. //
  125. this.checkBox3_0.AutoSize = true;
  126. this.checkBox3_0.Checked = true;
  127. this.checkBox3_0.CheckState = System.Windows.Forms.CheckState.Checked;
  128. this.checkBox3_0.Location = new System.Drawing.Point(15, 22);
  129. this.checkBox3_0.Name = "checkBox3_0";
  130. this.checkBox3_0.Size = new System.Drawing.Size(72, 16);
  131. this.checkBox3_0.TabIndex = 0;
  132. this.checkBox3_0.UseVisualStyleBackColor = true;
  133. this.checkBox3_0.CheckedChanged += new System.EventHandler(this.checkBox3_0_CheckedChanged);
  134. //
  135. // MethodAreaDialog
  136. //
  137. this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  138. this.ClientSize = new System.Drawing.Size(1127, 830);
  139. this.Controls.Add(this.groupBox11);
  140. this.Name = "MethodAreaDialog";
  141. this.Controls.SetChildIndex(this.groupBox_img, 0);
  142. this.Controls.SetChildIndex(this.groupBox_review, 0);
  143. this.Controls.SetChildIndex(this.groupBox_analysisResult1, 0);
  144. this.Controls.SetChildIndex(this.groupBox6groupBox_analysisResult2, 0);
  145. this.Controls.SetChildIndex(this.groupBox_report, 0);
  146. this.Controls.SetChildIndex(this.groupBox11, 0);
  147. this.groupBox_analysisResult1.ResumeLayout(false);
  148. this.groupBox11.ResumeLayout(false);
  149. this.groupBox11.PerformLayout();
  150. this.ResumeLayout(false);
  151. }
  152. #endregion 初始化组件
  153. #region 需继承方法
  154. protected override Boolean toshowPhoseModelAt(int index)
  155. {
  156. if (index == 1)
  157. {
  158. return (!showOrimat() && getGrainBoundryChecked() && checkBox3_0.Checked);
  159. }
  160. return true;
  161. }
  162. protected override void refreshWhileImgChanged()
  163. {
  164. if (this.bitmap != null && this.documentWorkspace.PhaseModels[2].mat != null && linePointList.Count > 0)
  165. {
  166. try
  167. {
  168. Getrains(linePointList, guideClass.RectangleFLine);
  169. }
  170. catch (Exception)
  171. {
  172. }
  173. }
  174. else
  175. {
  176. this.documentWorkspace.PhaseModels[2].mat = null;//清空前一张图的信息不遗留显示##效果考虑优化
  177. massLabelPoints.Clear();//1013###19098(切换图片的时候前一张图的信息不遗留显示)}
  178. this.documentWorkspace.Refresh();
  179. }
  180. }
  181. protected override void refreshPhaseModelChoise()
  182. {
  183. if (!showOrimat() && checkBox11.Checked)
  184. {
  185. this.documentWorkspace.PhaseModels[2].choise = true;
  186. }
  187. else
  188. {
  189. this.documentWorkspace.PhaseModels[2].choise = false;
  190. }
  191. }
  192. /// <summary>
  193. /// 保存结果
  194. /// </summary>
  195. protected override void buildListBox_analysisResult()
  196. {
  197. if (this.matOrg == null)
  198. {
  199. MessageBox.Show(PdnResources.GetString("Menu.Pleaseselectapicture.text")+"!");
  200. return;
  201. }
  202. if (this.comboBox1.SelectedItem != null && !PdnResources.GetString("Menu.Pleaseselecttheauxiliarylineused.text").Equals(this.comboBox1.SelectedItem))
  203. {
  204. string imgName = getCurrentWorkspace().GetFriendlyName();
  205. string tag = getCurrentWorkTag();
  206. System.Data.DataTable dataTable = new System.Data.DataTable();
  207. bool replace = false;
  208. int add = 0;
  209. foreach (ListViewItem item in this.listView2.Items)
  210. {
  211. if (item.Tag.Equals(tag))
  212. {
  213. add++;
  214. DialogResult dr = MessageBox.Show(PdnResources.GetString("Menu.Theresuethertoreplace.text")+"?", PdnResources.GetString("Menu.hint.text"), MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  215. if (dr == DialogResult.OK)
  216. {
  217. replace = true;
  218. }
  219. break;
  220. }
  221. }
  222. if (replace || this.listView2.Items.Count == 0 || add == 0)
  223. {
  224. if (!replace)
  225. {
  226. ListViewItem listViewItem = new ListViewItem();
  227. listViewItem.SubItems[0].Text = imgName;
  228. listViewItem.Tag = "" + tag;
  229. if (this.listView2.Items.Count == 0)
  230. this.listView2.Items.Add(listViewItem);
  231. else
  232. this.listView2.Items.Insert(0, listViewItem);
  233. }
  234. this.dataGridView2.Rows.Clear();
  235. DataGridViewRow dgvr = new DataGridViewRow();
  236. foreach (DataGridViewColumn c in this.dataGridView2.Columns)
  237. {
  238. dgvr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);
  239. }
  240. dgvr.Cells[0].Value = imgName;
  241. dgvr.Cells[1].Value = this.comboBox1.SelectedItem;
  242. // 是否矩形辅助线
  243. int isRect;
  244. double aec = guideClass.guideArea(this.comboBox1.SelectedItem, out isRect, 0);
  245. dgvr.Cells[2].Value = aec;
  246. aec = aec * 0.001 * 0.001;
  247. double grainNumber = GetNumberOfGrains(linePointList, guideClass.RectangleFLine);
  248. if (grainNumber > (int)grainNumber)
  249. {
  250. grainNumber = grainNumber + 0.5;
  251. }
  252. dgvr.Cells[3].Value = grainNumber;
  253. decimal M = 1;// this.getGainMultiple();
  254. double small_l = (grainNumber * (double)M * (double)M) / aec;
  255. decimal A = Convert.ToDecimal(small_l * 1);
  256. double[] lengths = {3.88, 7.75 , 11.0, 15.50, 21.9, 25.0, 31.0, 43.8, 62.0, 69.4,
  257. 87.7, 100, 124, 175, 204, 248, 278, 351, 400, 496, 625, 701, 816,
  258. 992, 1111 , 1403, 1600, 1980, 2500, 2810, 3970, 4440, 5610,
  259. 7940, 10000, 11220, 12350, 15870, 20410, 22450, 27780, 31710, 40000, 44900, 63500, 89800
  260. , 111100, 127000, 160000};
  261. // 差值
  262. decimal differenceNew = 0;
  263. decimal differenceOld = Math.Abs(Convert.ToDecimal(Math.Max((double)A - 2.88, (double)A - 160001)));//Convert.ToDecimal(double.MaxValue);//Math.Abs(A - 160000);// Convert.ToDecimal((double)A - 2.22);
  264. double selectArea = 0;
  265. int first = -1;
  266. foreach (double item in lengths)
  267. {
  268. selectArea = item;
  269. differenceNew = Math.Abs(A - Convert.ToDecimal(item));
  270. if (differenceNew < differenceOld)
  271. {
  272. differenceOld = Math.Abs(A - Convert.ToDecimal(item));
  273. first++;
  274. }
  275. else
  276. break;
  277. }
  278. string[] valueG = {"00A", "0" , "0.5", "1.0", "1.5", "1.5", "2.0", "2.5", "3.0", "3.0",
  279. "3.5", "3.5", "4.0", "4.5", "4.5", "5.0", "5.0", "5.5", "5.5", "6.0", "6.0", "6.5", "6.5",
  280. "7.0", "7.0" , "7.5", "7.5", "8.0", "8.0", "8.5", "9.0", "9.0", "9.5",
  281. "10.0", "10.0", "10.5", "10.5", "11.0", "11.0", "11.5", "11.5", "12.0", "12.0", "12.5", "13.0", "13.5"
  282. , "13.5", "14.0", "14.0"};
  283. if (first >= valueG.Length)
  284. {
  285. first = valueG.Length - 1;
  286. }
  287. else if (first < 0)
  288. {
  289. first = 0;
  290. }
  291. dgvr.Cells[4].Value = valueG[first];
  292. this.dataGridView2.Rows.Add(dgvr);
  293. dataTable.TableName = tag;
  294. foreach (System.Data.DataTable dataTable1 in dataTables)
  295. {
  296. if (dataTable1.TableName.Equals(dataTable.TableName))
  297. {
  298. dataTables.Remove(dataTable1);
  299. break;
  300. }
  301. }
  302. foreach (DataGridViewColumn c in this.dataGridView2.Columns)
  303. {
  304. dataTable.Columns.Add(c.HeaderText);
  305. }
  306. for (int r = 0; r < this.dataGridView2.Rows.Count; r++)
  307. {
  308. DataRow dataRow = dataTable.NewRow();
  309. for (int c = 0; c < this.dataGridView2.Rows[r].Cells.Count; c++)
  310. {
  311. dataRow[this.dataGridView2.Columns[c].HeaderText] = this.dataGridView2.Rows[r].Cells[c].Value;
  312. }
  313. dataTable.Rows.Add(dataRow);
  314. }
  315. this.dataTables.Add(dataTable);
  316. this.RefreshDataGridView4();
  317. //保存处理后的图片
  318. List<Bitmap> tempBit = new List<Bitmap>();
  319. Bitmap originalBit = this.bitmap;// this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
  320. tempBit.Add(originalBit);
  321. Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
  322. Graphics graphics = Graphics.FromImage(newBit);
  323. //if (true)
  324. //{ }//0914###18810
  325. if (this.documentWorkspace.PhaseModels[0].choise && this.documentWorkspace.PhaseModels[0].mat != null)
  326. {
  327. Mat targetMat = this.documentWorkspace.PhaseModels[0].mat;
  328. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(targetMat), 0, 0, targetMat.Width, targetMat.Height);
  329. }
  330. if (this.documentWorkspace.PhaseModels[1].choise && this.documentWorkspace.PhaseModels[1].mat != null)
  331. {
  332. Mat targetMat = this.documentWorkspace.PhaseModels[1].mat;
  333. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(targetMat), 0, 0, targetMat.Width, targetMat.Height);
  334. }
  335. if (this.documentWorkspace.PhaseModels[2].choise && this.documentWorkspace.PhaseModels[2].mat != null)
  336. {
  337. Mat targetMat = this.documentWorkspace.PhaseModels[2].mat;
  338. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(targetMat), 0, 0, targetMat.Width, targetMat.Height);
  339. }
  340. graphics.SmoothingMode = SmoothingMode.AntiAlias;
  341. drawSubclass/*Draw*/(graphics);
  342. tempBit.Add(newBit);
  343. if (bitDic.ContainsKey(tag))
  344. bitDic[tag] = tempBit;
  345. else
  346. bitDic.Add(tag, tempBit);
  347. //拼接中间数据
  348. List<List<string>> dataList = new List<List<string>>();
  349. List<string> columnName = new List<string>();
  350. columnName.Add(PdnResources.GetString("Menu.Imagement.Measurementlist.ordernumber.text"));
  351. columnName.Add(PdnResources.GetString("Menu.Xoordinate.Text"));
  352. columnName.Add(PdnResources.GetString("Menu.Yoordinate.Text"));
  353. columnName.Add(PdnResources.GetString("Menu.Whethertheinternal.Text"));
  354. dataList.Add(columnName);
  355. if (massLabelPoints.Count > 0)
  356. {
  357. int index = 0;
  358. int outMaxIndex = massLabelPoints.Count - 0 - within;
  359. foreach (var item in massLabelPoints)
  360. {
  361. List<string> strList = new List<string>();
  362. strList.Add(("" + (++index)).ToString());
  363. strList.Add(item.X.ToString());
  364. strList.Add(item.Y.ToString());
  365. if (outMaxIndex < index)
  366. strList.Add(PdnResources.GetString("Menu.Yes.text"));
  367. else
  368. strList.Add(PdnResources.GetString("Menu.no.text"));
  369. dataList.Add(strList);
  370. }
  371. }
  372. bool isExist = false;//是否已存在进行替换
  373. int modelIndex = -1;//要替换的下标
  374. for (int j = 0; j < tempDataModel.Count; j++)
  375. {
  376. if (tempDataModel[j].tagName.Equals(tag))
  377. {
  378. isExist = true;
  379. modelIndex = j;
  380. break;
  381. }
  382. }
  383. if (isExist && modelIndex > -1)
  384. tempDataModel[modelIndex].dataList = dataList;
  385. else
  386. {
  387. Base.Functionodel.ExportProjectModel newModel = new Base.Functionodel.ExportProjectModel();
  388. newModel.tagName = tag;
  389. newModel.picName = imgName;
  390. newModel.dataList = dataList;
  391. tempDataModel.Add(newModel);
  392. }
  393. }
  394. }
  395. else
  396. {
  397. MessageBox.Show(PdnResources.GetString("Menu.Pleaseselectauxiliaryline.text")+"!");
  398. }
  399. }
  400. protected override void SubclassInitialize()
  401. {
  402. InitializeComponent();
  403. InitializeLanguageText();
  404. }
  405. //是否绘制辅助线
  406. protected override bool drawGuideLines()
  407. {
  408. return checkBox4_0.Checked;
  409. }
  410. //是否绘制截点
  411. protected override bool drawGuidePoints()
  412. {
  413. return (matOrg != null && this.checkBox5_0.Checked);
  414. }
  415. //是否绘制编号
  416. protected override bool drawMassLabelPoints()
  417. {
  418. return this.checkBox5_0.Checked;
  419. }
  420. protected override string[] getTypeNames()
  421. {
  422. return new string[] {
  423. PdnResources.GetString("Menu.circular.Text")};
  424. }
  425. protected override string getTemplateName()
  426. {
  427. return "Template.Manager.item3.GrainSizeArea_GBT4296";
  428. }
  429. protected override string getProjectEngineeringName()
  430. {
  431. return "Menu.DedicatedAnalysis.NonferrousMetal.MagnesiumAlloy.Text";
  432. }
  433. protected override string getModelName()
  434. {
  435. return "GrainGBT32461AreaMethodModel.xml";
  436. }
  437. #endregion
  438. /// <summary>
  439. /// 刷新晶粒编号
  440. /// </summary>
  441. /// <param name="sender"></param>
  442. /// <param name="e"></param>
  443. private void button4_Click(object sender, EventArgs e)
  444. {
  445. if (this.matOrg == null)
  446. {
  447. MessageBox.Show(PdnResources.GetString("Menu.Pleaseselectapicturefirst.text"));
  448. return;
  449. }
  450. Getrains(linePointList, guideClass.RectangleFLine);
  451. }
  452. private Double GetNumberOfGrains(List<PointF> pointFs, RectangleF rectangleF)
  453. {
  454. Mat OriginalPicture;
  455. Mat analyzeMat = new Mat();
  456. if (getGrainBoundryChecked())
  457. {
  458. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  459. Mat oldMat = this.documentWorkspace.PhaseModels[1].mat;
  460. int Rows = oldMat.Rows;
  461. int Cols = oldMat.Cols;
  462. int[] point = new int[2];
  463. //多通道图像的格式化
  464. for (point[0] = 0; point[0] < Rows; point[0]++)
  465. {
  466. for (point[1] = 0; point[1] < Cols; point[1]++)
  467. {
  468. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  469. {
  470. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  471. }
  472. }
  473. }
  474. Cv2.Erode(OriginalPicture, OriginalPicture, null, null, 1);
  475. OriginalPicture.CopyTo(analyzeMat);
  476. }
  477. else if (getBinaryChecked())
  478. {
  479. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  480. Mat oldMat = this.documentWorkspace.PhaseModels[0].mat;
  481. int Rows = oldMat.Rows;
  482. int Cols = oldMat.Cols;
  483. int[] point = new int[2];
  484. //多通道图像的格式化
  485. for (point[0] = 0; point[0] < Rows; point[0]++)
  486. {
  487. for (point[1] = 0; point[1] < Cols; point[1]++)
  488. {
  489. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  490. {
  491. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  492. }
  493. }
  494. }
  495. oldMat.CopyTo(analyzeMat);
  496. }
  497. else
  498. {
  499. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  500. matOrg.CopyTo(OriginalPicture);
  501. matOrg.CopyTo(analyzeMat);
  502. }
  503. Mat erzhi = OriginalPicture;
  504. Cv2.CvtColor(erzhi, erzhi, ColorConversionCodes.BGRA2GRAY);
  505. Cv2.Threshold(erzhi, erzhi, 100, 255, ThresholdTypes.Binary);//#############
  506. //Cv2.AdaptiveThreshold(erzhi, erzhi, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 99, 10);
  507. OpenCvSharp.Point[][] contours;
  508. HierarchyIndex[] hierarchy;
  509. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  510. //Cv2.ImWrite("erzhi1.tif", erzhi.CvtColor(ColorConversionCodes.GRAY2BGR));
  511. List<OpenCvSharp.Point[]> pointss = new List<OpenCvSharp.Point[]>();
  512. foreach (OpenCvSharp.Point[] contour in contours)
  513. {
  514. if (contour.Length < 100)
  515. {
  516. //foreach (OpenCvSharp.Point point in contour)
  517. //{
  518. // erzhi.Set<byte>(point.Y, point.X, 255);
  519. //}
  520. pointss.Add(contour);
  521. }
  522. }
  523. Cv2.FillPoly(erzhi, pointss, new Scalar(255));
  524. // 二次轮廓
  525. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  526. // 计算所有轮廓质点
  527. List<Point2d> massPoints = new List<Point2d>();
  528. foreach (OpenCvSharp.Point[] contour in contours)
  529. {
  530. OpenCvSharp.Moments moments = new OpenCvSharp.Moments(contour, false);
  531. massPoints.Add(new Point2d(moments.M10 / moments.M00, moments.M01 / moments.M00));
  532. }
  533. Mat labelMat = new Mat();
  534. Mat stats = new Mat();
  535. Mat centroids = new Mat();
  536. // 连通域数量
  537. int nccomps = Cv2.ConnectedComponentsWithStats(erzhi, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  538. //Cv2.ImWrite("erzhi2.tif", labelMat);
  539. //Cv2.ImWrite("erzhi3.tif", stats);
  540. //Cv2.ImWrite("erzhi4.tif", centroids);
  541. // 连通域质点
  542. Dictionary<int, Point2d> keyValues = new Dictionary<int, Point2d>();
  543. massLabelPoints.Clear();
  544. foreach (var item in massPoints)
  545. {
  546. if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  547. {
  548. continue;
  549. }
  550. int label = labelMat.At<int>((int)item.Y, (int)item.X);
  551. if (0 <= label && label <= nccomps)
  552. {
  553. keyValues[label] = item;
  554. }
  555. }
  556. Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
  557. // 矩形边线上的连通域
  558. ArrayList nccompSelect = new ArrayList();
  559. foreach (var pointF in pointFs)
  560. {
  561. if (labelMat.Width <= pointF.X || labelMat.Height <= pointF.Y || pointF.X < 0 || pointF.Y < 0)
  562. {
  563. continue;
  564. }
  565. //判断是否在晶界上面
  566. Vec4b vec4B = analyzeMat.At<Vec4b>((int)pointF.Y, (int)pointF.X);
  567. if (getGrainBoundryChecked() && vec4B.Item0 == 0 || getBinaryChecked() && vec4B.Item3 != 0 || !getGrainBoundryChecked() && !getBinaryChecked() && vec4B.Item0 == 0)
  568. {
  569. //temp.Set<Vec4b>((int)pointF.Y, (int)pointF.X, new Vec4b(0, 255, 0, 255));
  570. continue;//0925###19106
  571. }
  572. int label = labelMat.At<int>((int)pointF.Y, (int)pointF.X);
  573. if (!nccompSelect.Contains(label) && keyValues.ContainsKey(label) && keyValues[label] != null)
  574. {
  575. nccompSelect.Add(label);
  576. if (keyValues.ContainsKey(label) && keyValues[label] != null)
  577. massLabelPoints.Add(keyValues[label]);
  578. // massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  579. //else
  580. // massLabelPoints.Add(new Point2d((int)pointF.Y, (int)pointF.Y/*-1, -1*/));
  581. }
  582. else if (keyValues.ContainsKey(label) && keyValues[label] != null)
  583. {
  584. int iii = 1;
  585. }
  586. else if (!nccompSelect.Contains(label))
  587. {
  588. int iiii = 1;
  589. nccompSelect.Add(label);
  590. massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  591. }
  592. else
  593. {
  594. int iiii = 1;
  595. }
  596. }
  597. // 矩形内的连通域
  598. within = 0;
  599. ArrayList inccompSelect = new ArrayList();
  600. GrainSizeGuideAreaMethodModel grainSizeGuideStyleModel = guideClass.GrainSizeGuideAreaMethodModel;
  601. // 圆形
  602. if (grainSizeGuideStyleModel.circularGuideStyles != null)
  603. {
  604. foreach (var circularGuideStyle in grainSizeGuideStyleModel.circularGuideStyles)
  605. {
  606. if (this.comboBox1.SelectedItem.Equals(circularGuideStyle.tag))
  607. {
  608. foreach (var keyValue in keyValues)
  609. {
  610. //求点到圆心的距离
  611. 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);
  612. if (!nccompSelect.Contains(keyValue.Key) && c < guideClass.Mat_lineDiameter / 2)
  613. {
  614. if (!inccompSelect.Contains(keyValue.Key))
  615. {
  616. within++;
  617. inccompSelect.Add(keyValue.Key);
  618. if (keyValue.Value != null)
  619. massLabelPoints.Add(keyValue.Value);
  620. }
  621. }
  622. }
  623. }
  624. }
  625. }
  626. // 矩形
  627. if (grainSizeGuideStyleModel.rectangleGuideStyles != null)
  628. {
  629. foreach (var rectangleGuideStyle in grainSizeGuideStyleModel.rectangleGuideStyles)
  630. {
  631. if (this.comboBox1.SelectedItem.Equals(rectangleGuideStyle.tag))
  632. {
  633. foreach (var keyValue in keyValues)
  634. {
  635. if (!nccompSelect.Contains(keyValue.Key) && keyValue.Value.X > rectangleF.X && keyValue.Value.X < rectangleF.Right
  636. && keyValue.Value.Y > rectangleF.Y && keyValue.Value.Y < rectangleF.Bottom)
  637. {
  638. if (!inccompSelect.Contains(keyValue.Key))
  639. {
  640. within++;
  641. inccompSelect.Add(keyValue.Key);
  642. if (keyValue.Value != null)
  643. massLabelPoints.Add(keyValue.Value);
  644. }
  645. }
  646. }
  647. }
  648. }
  649. }
  650. //Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4);
  651. ////Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC1);
  652. Random rd = new Random();
  653. List<Vec4b> colors = new List<Vec4b>(nccomps);
  654. colors.Add(new Vec4b(0, 0, 0, 0));
  655. for (int i = 0; i < nccomps; i++)
  656. {
  657. //colors.Add(new Vec3b(255, 255, 255));
  658. colors.Add(new Vec4b(((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), 255));
  659. }
  660. for (int y = 0; y < temp.Rows; y++)
  661. {
  662. for (int x = 0; x < temp.Cols; x++)
  663. {
  664. int label = labelMat.At<int>(y, x);
  665. if (nccompSelect.Contains(label))
  666. temp.Set<Vec4b>(y, x, colors[label + 1]);
  667. if (inccompSelect.Contains(label))
  668. temp.Set<Vec4b>(y, x, colors[label + 1]);
  669. //temp.Set<Vec4b>(y, x, new Vec4b(127, 127, 127, 127));
  670. ////if (0 <= label && label <= nccomps)
  671. ////{
  672. //// temp.Set<Vec3b>(y, x, colors[label]);
  673. ////}
  674. }
  675. }
  676. //foreach (var item in massPoints)
  677. //{
  678. // if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  679. // {
  680. // continue;
  681. // }
  682. // temp.Set<Vec4b>((int)item.Y, (int)item.X, new Vec4b(0, 0, 255, 255));
  683. //}
  684. //Cv2.ImWrite("erzhi5.tif", temp);
  685. ////Cv2.ImShow("temp", temp);
  686. this.documentWorkspace.PhaseModels[2].mat = temp;
  687. this.documentWorkspace.Refresh();
  688. return within + (double)nccompSelect.Count / 2;
  689. }
  690. /// <summary>
  691. /// 刷新晶粒编号
  692. /// </summary>
  693. /// <param name="pointFs"></param>
  694. /// <param name="rectangleF"></param>
  695. private void Getrains(List<PointF> pointFs, RectangleF rectangleF)
  696. {
  697. Mat OriginalPicture;
  698. Mat analyzeMat = new Mat();
  699. if (getGrainBoundryChecked())
  700. {
  701. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  702. Mat oldMat = this.documentWorkspace.PhaseModels[1].mat;
  703. int Rows = oldMat.Rows;
  704. int Cols = oldMat.Cols;
  705. int[] point = new int[2];
  706. //多通道图像的格式化
  707. for (point[0] = 0; point[0] < Rows; point[0]++)
  708. {
  709. for (point[1] = 0; point[1] < Cols; point[1]++)
  710. {
  711. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  712. {
  713. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  714. }
  715. }
  716. }
  717. Cv2.Erode(OriginalPicture, OriginalPicture, null, null, 1);
  718. OriginalPicture.CopyTo(analyzeMat);
  719. }
  720. else if (getBinaryChecked())
  721. {
  722. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  723. Mat oldMat = this.documentWorkspace.PhaseModels[0].mat;
  724. int Rows = oldMat.Rows;
  725. int Cols = oldMat.Cols;
  726. int[] point = new int[2];
  727. //多通道图像的格式化
  728. for (point[0] = 0; point[0] < Rows; point[0]++)
  729. {
  730. for (point[1] = 0; point[1] < Cols; point[1]++)
  731. {
  732. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  733. {
  734. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  735. }
  736. }
  737. }
  738. oldMat.CopyTo(analyzeMat);
  739. }
  740. else
  741. {
  742. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  743. matOrg.CopyTo(OriginalPicture);
  744. matOrg.CopyTo(analyzeMat);
  745. }
  746. Mat erzhi = OriginalPicture.Clone();
  747. Cv2.CvtColor(erzhi, erzhi, ColorConversionCodes.BGRA2GRAY);
  748. Cv2.Threshold(erzhi, erzhi, 100, 255, ThresholdTypes.Binary);//#############
  749. //Cv2.AdaptiveThreshold(erzhi, erzhi, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 99, 10);
  750. OpenCvSharp.Point[][] contours;
  751. HierarchyIndex[] hierarchy;
  752. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  753. List<OpenCvSharp.Point[]> pointss = new List<OpenCvSharp.Point[]>();
  754. foreach (OpenCvSharp.Point[] contour in contours)
  755. {
  756. if (contour.Length < 100)
  757. {
  758. //foreach (OpenCvSharp.Point point in contour)
  759. //{
  760. // erzhi.Set<byte>(point.Y, point.X, 255);
  761. //}
  762. pointss.Add(contour);
  763. }
  764. }
  765. Cv2.FillPoly(erzhi, pointss, new Scalar(255));
  766. // 二次轮廓
  767. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  768. // 计算所有轮廓质点
  769. List<Point2d> massPoints = new List<Point2d>();
  770. foreach (OpenCvSharp.Point[] contour in contours)
  771. {
  772. OpenCvSharp.Moments moments = new OpenCvSharp.Moments(contour, false);
  773. massPoints.Add(new Point2d(moments.M10 / moments.M00, moments.M01 / moments.M00));
  774. }
  775. Mat labelMat = new Mat();
  776. Mat stats = new Mat();
  777. Mat centroids = new Mat();
  778. // 连通域数量
  779. int nccomps = Cv2.ConnectedComponentsWithStats(erzhi, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  780. // 连通域质点
  781. Dictionary<int, Point2d> keyValues = new Dictionary<int, Point2d>();
  782. massLabelPoints.Clear();
  783. foreach (var item in massPoints)
  784. {
  785. if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  786. {
  787. continue;
  788. }
  789. int label = labelMat.At<int>((int)item.Y, (int)item.X);
  790. if (0 <= label && label <= nccomps)
  791. {
  792. keyValues[label] = item;
  793. }
  794. }
  795. Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
  796. // 矩形边线上的连通域
  797. ArrayList nccompSelect = new ArrayList();
  798. foreach (var pointF in pointFs)
  799. {
  800. if (labelMat.Width <= pointF.X || labelMat.Height <= pointF.Y || pointF.X < 0 || pointF.Y < 0)
  801. {
  802. continue;
  803. }
  804. //判断是否在晶界上面
  805. Vec4b vec4B = analyzeMat.At<Vec4b>((int)pointF.Y, (int)pointF.X);
  806. if (getGrainBoundryChecked() && vec4B.Item0 == 0 || getBinaryChecked() && vec4B.Item3 != 0 || !getGrainBoundryChecked() && !getBinaryChecked() && vec4B.Item0 == 0)
  807. {
  808. //temp.Set<Vec4b>((int)pointF.Y, (int)pointF.X, new Vec4b(0, 255, 0, 255));
  809. continue;//0925###19106
  810. }
  811. int label = labelMat.At<int>((int)pointF.Y, (int)pointF.X);
  812. if (!nccompSelect.Contains(label) && keyValues.ContainsKey(label) && keyValues[label] != null)
  813. {
  814. nccompSelect.Add(label);
  815. if (keyValues.ContainsKey(label) && keyValues[label] != null)
  816. massLabelPoints.Add(keyValues[label]);
  817. // massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  818. //else
  819. // massLabelPoints.Add(new Point2d((int)pointF.Y, (int)pointF.Y/*-1, -1*/));
  820. }
  821. else if (keyValues.ContainsKey(label) && keyValues[label] != null)
  822. {
  823. int iii = 1;
  824. }
  825. else if (!nccompSelect.Contains(label))
  826. {
  827. int iiii = 1;
  828. nccompSelect.Add(label);
  829. massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  830. }
  831. else
  832. {
  833. int iiii = 1;
  834. }
  835. }
  836. // 矩形内的连通域
  837. within = 0;
  838. ArrayList inccompSelect = new ArrayList();
  839. GrainSizeGuideAreaMethodModel grainSizeGuideStyleModel = guideClass.GrainSizeGuideAreaMethodModel;
  840. // 圆形
  841. if (grainSizeGuideStyleModel.circularGuideStyles != null)
  842. {
  843. foreach (var circularGuideStyle in grainSizeGuideStyleModel.circularGuideStyles)
  844. {
  845. if (this.getCurrentGuideName().Equals(circularGuideStyle.tag))
  846. {
  847. foreach (var keyValue in keyValues)
  848. {
  849. //求点到圆心的距离
  850. 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);
  851. if (!nccompSelect.Contains(keyValue.Key) && c < guideClass.Mat_lineDiameter / 2)
  852. {
  853. if (!inccompSelect.Contains(keyValue.Key))
  854. {
  855. within++;
  856. inccompSelect.Add(keyValue.Key);
  857. if (keyValue.Value != null)
  858. massLabelPoints.Add(keyValue.Value);
  859. }
  860. }
  861. }
  862. }
  863. }
  864. }
  865. // 矩形
  866. if (grainSizeGuideStyleModel.rectangleGuideStyles != null)
  867. {
  868. foreach (var rectangleGuideStyle in grainSizeGuideStyleModel.rectangleGuideStyles)
  869. {
  870. if (this.getCurrentGuideName().Equals(rectangleGuideStyle.tag))
  871. {
  872. foreach (var keyValue in keyValues)
  873. {
  874. if (!nccompSelect.Contains(keyValue.Key) && keyValue.Value.X > rectangleF.X && keyValue.Value.X < rectangleF.Right
  875. && keyValue.Value.Y > rectangleF.Y && keyValue.Value.Y < rectangleF.Bottom)
  876. {
  877. if (!inccompSelect.Contains(keyValue.Key))
  878. {
  879. within++;
  880. inccompSelect.Add(keyValue.Key);
  881. if (keyValue.Value != null)
  882. massLabelPoints.Add(keyValue.Value);
  883. }
  884. }
  885. }
  886. }
  887. }
  888. }
  889. //Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4);
  890. ////Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC1);
  891. Random rd = new Random();
  892. List<Vec4b> colors = new List<Vec4b>(nccomps);
  893. colors.Add(new Vec4b(0, 0, 0, 0));
  894. for (int i = 0; i < nccomps; i++)
  895. {
  896. //colors.Add(new Vec3b(255, 255, 255));
  897. colors.Add(new Vec4b(((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), 255));
  898. }
  899. for (int y = 0; y < temp.Rows; y++)
  900. {
  901. for (int x = 0; x < temp.Cols; x++)
  902. {
  903. int label = labelMat.At<int>(y, x);
  904. if (nccompSelect.Contains(label))
  905. temp.Set<Vec4b>(y, x, colors[label + 1]);
  906. if (inccompSelect.Contains(label))
  907. temp.Set<Vec4b>(y, x, colors[label + 1]);
  908. //temp.Set<Vec4b>(y, x, new Vec4b(127, 127, 127, 127));
  909. ////if (0 <= label && label <= nccomps)
  910. ////{
  911. //// temp.Set<Vec3b>(y, x, colors[label]);
  912. ////}
  913. }
  914. }
  915. //foreach (var item in massPoints)
  916. //{
  917. // if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  918. // {
  919. // continue;
  920. // }
  921. // temp.Set<Vec4b>((int)item.Y, (int)item.X, new Vec4b(0, 0, 255, 255));
  922. //}
  923. ////Cv2.ImShow("temp", temp);
  924. this.documentWorkspace.PhaseModels[2].mat = temp;
  925. this.documentWorkspace.Refresh();
  926. }
  927. /// <summary>
  928. /// 是否显示晶界
  929. /// </summary>
  930. /// <param name="sender"></param>
  931. /// <param name="e"></param>
  932. private void checkBox3_0_CheckedChanged(object sender, EventArgs e)
  933. {
  934. if (!showOrimat() && getGrainBoundryChecked() && checkBox3_0.Checked)
  935. {
  936. this.documentWorkspace.PhaseModels[1].choise = true;
  937. }
  938. else
  939. {
  940. this.documentWorkspace.PhaseModels[1].choise = false;
  941. }
  942. this.documentWorkspace.Refresh();
  943. }
  944. private void checkBox4_0_CheckedChanged(object sender, EventArgs e)
  945. {
  946. this.documentWorkspace.Refresh();
  947. }
  948. private void checkBox11_CheckedChanged(object sender, EventArgs e)
  949. {
  950. if (!showOrimat() && checkBox11.Checked)
  951. {
  952. this.documentWorkspace.PhaseModels[2].choise = true;
  953. }
  954. else
  955. {
  956. this.documentWorkspace.PhaseModels[2].choise = false;
  957. }
  958. this.documentWorkspace.Refresh();
  959. }
  960. private void checkBox5_0_CheckedChanged(object sender, EventArgs e)
  961. {
  962. this.documentWorkspace.Refresh();
  963. }
  964. }
  965. }