MethodAreaDialog.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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.GBT32461
  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 = "GBT32461(" + 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/*this.gbtType*/);
  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/*2.22*/};//###这块逻辑多认证认证
  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(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("X"+ PdnResources.GetString("Menu.coordinates.Text"));
  352. columnName.Add("Y"+ PdnResources.GetString("Menu.coordinates.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. //辅助线类型
  421. protected override string[] getTypeNames()
  422. {
  423. return new string[] {
  424. PdnResources.GetString("Menu.circular.Text")};
  425. }
  426. protected override string getTemplateName()
  427. {
  428. return "Template.Manager.item3.GrainSizeArea_GBT32461";
  429. }
  430. protected override string getProjectEngineeringName()
  431. {
  432. return "Menu.DedicatedAnalysis.NonferrousMetal.AluminiumAlloy.Text";
  433. }
  434. protected override string getModelName()
  435. {
  436. return "GrainGBT32461AreaMethodModel.xml";
  437. }
  438. #endregion
  439. /// <summary>
  440. /// 刷新晶粒编号
  441. /// </summary>
  442. /// <param name="sender"></param>
  443. /// <param name="e"></param>
  444. private void button4_Click(object sender, EventArgs e)
  445. {
  446. if (this.matOrg == null)
  447. {
  448. MessageBox.Show(PdnResources.GetString("Menu.Pleaseselectapicturefirst.text"));
  449. return;
  450. }
  451. Getrains(linePointList, guideClass.RectangleFLine);
  452. }
  453. private Double GetNumberOfGrains(List<PointF> pointFs, RectangleF rectangleF)
  454. {
  455. Mat OriginalPicture;
  456. Mat analyzeMat = new Mat();
  457. if (getGrainBoundryChecked())
  458. {
  459. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  460. Mat oldMat = this.documentWorkspace.PhaseModels[1].mat;
  461. int Rows = oldMat.Rows;
  462. int Cols = oldMat.Cols;
  463. int[] point = new int[2];
  464. //多通道图像的格式化
  465. for (point[0] = 0; point[0] < Rows; point[0]++)
  466. {
  467. for (point[1] = 0; point[1] < Cols; point[1]++)
  468. {
  469. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  470. {
  471. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  472. }
  473. }
  474. }
  475. Cv2.Erode(OriginalPicture, OriginalPicture, null, null, 1);
  476. OriginalPicture.CopyTo(analyzeMat);
  477. }
  478. else if (getBinaryChecked())
  479. {
  480. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  481. Mat oldMat = this.documentWorkspace.PhaseModels[0].mat;
  482. int Rows = oldMat.Rows;
  483. int Cols = oldMat.Cols;
  484. int[] point = new int[2];
  485. //多通道图像的格式化
  486. for (point[0] = 0; point[0] < Rows; point[0]++)
  487. {
  488. for (point[1] = 0; point[1] < Cols; point[1]++)
  489. {
  490. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  491. {
  492. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  493. }
  494. }
  495. }
  496. oldMat.CopyTo(analyzeMat);
  497. }
  498. else
  499. {
  500. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  501. matOrg.CopyTo(OriginalPicture);
  502. matOrg.CopyTo(analyzeMat);
  503. }
  504. Mat erzhi = OriginalPicture;
  505. Cv2.CvtColor(erzhi, erzhi, ColorConversionCodes.BGRA2GRAY);
  506. Cv2.Threshold(erzhi, erzhi, 100, 255, ThresholdTypes.Binary);//#############
  507. //Cv2.AdaptiveThreshold(erzhi, erzhi, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 99, 10);
  508. OpenCvSharp.Point[][] contours;
  509. HierarchyIndex[] hierarchy;
  510. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  511. //Cv2.ImWrite("erzhi1.tif", erzhi.CvtColor(ColorConversionCodes.GRAY2BGR));
  512. List<OpenCvSharp.Point[]> pointss = new List<OpenCvSharp.Point[]>();
  513. foreach (OpenCvSharp.Point[] contour in contours)
  514. {
  515. if (contour.Length < 100)
  516. {
  517. //foreach (OpenCvSharp.Point point in contour)
  518. //{
  519. // erzhi.Set<byte>(point.Y, point.X, 255);
  520. //}
  521. pointss.Add(contour);
  522. }
  523. }
  524. Cv2.FillPoly(erzhi, pointss, new Scalar(255));
  525. // 二次轮廓
  526. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  527. // 计算所有轮廓质点
  528. List<Point2d> massPoints = new List<Point2d>();
  529. foreach (OpenCvSharp.Point[] contour in contours)
  530. {
  531. OpenCvSharp.Moments moments = new OpenCvSharp.Moments(contour, false);
  532. massPoints.Add(new Point2d(moments.M10 / moments.M00, moments.M01 / moments.M00));
  533. }
  534. Mat labelMat = new Mat();
  535. Mat stats = new Mat();
  536. Mat centroids = new Mat();
  537. // 连通域数量
  538. int nccomps = Cv2.ConnectedComponentsWithStats(erzhi, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  539. //Cv2.ImWrite("erzhi2.tif", labelMat);
  540. //Cv2.ImWrite("erzhi3.tif", stats);
  541. //Cv2.ImWrite("erzhi4.tif", centroids);
  542. // 连通域质点
  543. Dictionary<int, Point2d> keyValues = new Dictionary<int, Point2d>();
  544. massLabelPoints.Clear();
  545. foreach (var item in massPoints)
  546. {
  547. if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  548. {
  549. continue;
  550. }
  551. int label = labelMat.At<int>((int)item.Y, (int)item.X);
  552. if (0 <= label && label <= nccomps)
  553. {
  554. keyValues[label] = item;
  555. }
  556. }
  557. Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4, new Scalar(0, 0, 0, 0));
  558. // 矩形边线上的连通域
  559. ArrayList nccompSelect = new ArrayList();
  560. foreach (var pointF in pointFs)
  561. {
  562. if (labelMat.Width <= pointF.X || labelMat.Height <= pointF.Y || pointF.X < 0 || pointF.Y < 0)
  563. {
  564. continue;
  565. }
  566. //判断是否在晶界上面
  567. Vec4b vec4B = analyzeMat.At<Vec4b>((int)pointF.Y, (int)pointF.X);
  568. if (getGrainBoundryChecked() && vec4B.Item0 == 0 || getBinaryChecked() && vec4B.Item3 != 0 || !getGrainBoundryChecked() && !getBinaryChecked() && vec4B.Item0 == 0)
  569. {
  570. //temp.Set<Vec4b>((int)pointF.Y, (int)pointF.X, new Vec4b(0, 255, 0, 255));
  571. continue;//0925###19106
  572. }
  573. int label = labelMat.At<int>((int)pointF.Y, (int)pointF.X);
  574. if (!nccompSelect.Contains(label) && keyValues.ContainsKey(label) && keyValues[label] != null)
  575. {
  576. nccompSelect.Add(label);
  577. if (keyValues.ContainsKey(label) && keyValues[label] != null)
  578. massLabelPoints.Add(keyValues[label]);
  579. // massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  580. //else
  581. // massLabelPoints.Add(new Point2d((int)pointF.Y, (int)pointF.Y/*-1, -1*/));
  582. }
  583. else if (keyValues.ContainsKey(label) && keyValues[label] != null)
  584. {
  585. int iii = 1;
  586. }
  587. else if (!nccompSelect.Contains(label))
  588. {
  589. int iiii = 1;
  590. nccompSelect.Add(label);
  591. massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  592. }
  593. else
  594. {
  595. int iiii = 1;
  596. }
  597. }
  598. // 矩形内的连通域
  599. within = 0;
  600. ArrayList inccompSelect = new ArrayList();
  601. GrainSizeGuideAreaMethodModel grainSizeGuideStyleModel = guideClass.GrainSizeGuideAreaMethodModel;
  602. // 圆形
  603. if (grainSizeGuideStyleModel.circularGuideStyles != null)
  604. {
  605. foreach (var circularGuideStyle in grainSizeGuideStyleModel.circularGuideStyles)
  606. {
  607. if (this.comboBox1.SelectedItem.Equals(circularGuideStyle.tag))
  608. {
  609. foreach (var keyValue in keyValues)
  610. {
  611. //求点到圆心的距离
  612. 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);
  613. if (!nccompSelect.Contains(keyValue.Key) && c < guideClass.Mat_lineDiameter / 2)
  614. {
  615. if (!inccompSelect.Contains(keyValue.Key))
  616. {
  617. within++;
  618. inccompSelect.Add(keyValue.Key);
  619. if (keyValue.Value != null)
  620. massLabelPoints.Add(keyValue.Value);
  621. }
  622. }
  623. }
  624. }
  625. }
  626. }
  627. // 矩形
  628. if (grainSizeGuideStyleModel.rectangleGuideStyles != null)
  629. {
  630. foreach (var rectangleGuideStyle in grainSizeGuideStyleModel.rectangleGuideStyles)
  631. {
  632. if (this.comboBox1.SelectedItem.Equals(rectangleGuideStyle.tag))
  633. {
  634. foreach (var keyValue in keyValues)
  635. {
  636. if (!nccompSelect.Contains(keyValue.Key) && keyValue.Value.X > rectangleF.X && keyValue.Value.X < rectangleF.Right
  637. && keyValue.Value.Y > rectangleF.Y && keyValue.Value.Y < rectangleF.Bottom)
  638. {
  639. if (!inccompSelect.Contains(keyValue.Key))
  640. {
  641. within++;
  642. inccompSelect.Add(keyValue.Key);
  643. if (keyValue.Value != null)
  644. massLabelPoints.Add(keyValue.Value);
  645. }
  646. }
  647. }
  648. }
  649. }
  650. }
  651. //Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4);
  652. ////Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC1);
  653. Random rd = new Random();
  654. List<Vec4b> colors = new List<Vec4b>(nccomps);
  655. colors.Add(new Vec4b(0, 0, 0, 0));
  656. for (int i = 0; i < nccomps; i++)
  657. {
  658. //colors.Add(new Vec3b(255, 255, 255));
  659. colors.Add(new Vec4b(((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), 255));
  660. }
  661. for (int y = 0; y < temp.Rows; y++)
  662. {
  663. for (int x = 0; x < temp.Cols; x++)
  664. {
  665. int label = labelMat.At<int>(y, x);
  666. if (nccompSelect.Contains(label))
  667. temp.Set<Vec4b>(y, x, colors[label + 1]);
  668. if (inccompSelect.Contains(label))
  669. temp.Set<Vec4b>(y, x, colors[label + 1]);
  670. //temp.Set<Vec4b>(y, x, new Vec4b(127, 127, 127, 127));
  671. ////if (0 <= label && label <= nccomps)
  672. ////{
  673. //// temp.Set<Vec3b>(y, x, colors[label]);
  674. ////}
  675. }
  676. }
  677. //foreach (var item in massPoints)
  678. //{
  679. // if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  680. // {
  681. // continue;
  682. // }
  683. // temp.Set<Vec4b>((int)item.Y, (int)item.X, new Vec4b(0, 0, 255, 255));
  684. //}
  685. //Cv2.ImWrite("erzhi5.tif", temp);
  686. ////Cv2.ImShow("temp", temp);
  687. this.documentWorkspace.PhaseModels[2].mat = temp;
  688. this.documentWorkspace.Refresh();
  689. return within + (double)nccompSelect.Count / 2;
  690. }
  691. /// <summary>
  692. /// 刷新晶粒编号
  693. /// </summary>
  694. /// <param name="pointFs"></param>
  695. /// <param name="rectangleF"></param>
  696. private void Getrains(List<PointF> pointFs, RectangleF rectangleF)
  697. {
  698. Mat OriginalPicture;
  699. Mat analyzeMat = new Mat();
  700. if (getGrainBoundryChecked())
  701. {
  702. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  703. Mat oldMat = this.documentWorkspace.PhaseModels[1].mat;
  704. int Rows = oldMat.Rows;
  705. int Cols = oldMat.Cols;
  706. int[] point = new int[2];
  707. //多通道图像的格式化
  708. for (point[0] = 0; point[0] < Rows; point[0]++)
  709. {
  710. for (point[1] = 0; point[1] < Cols; point[1]++)
  711. {
  712. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  713. {
  714. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  715. }
  716. }
  717. }
  718. Cv2.Erode(OriginalPicture, OriginalPicture, null, null, 1);
  719. OriginalPicture.CopyTo(analyzeMat);
  720. }
  721. else if (getBinaryChecked())
  722. {
  723. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  724. Mat oldMat = this.documentWorkspace.PhaseModels[0].mat;
  725. int Rows = oldMat.Rows;
  726. int Cols = oldMat.Cols;
  727. int[] point = new int[2];
  728. //多通道图像的格式化
  729. for (point[0] = 0; point[0] < Rows; point[0]++)
  730. {
  731. for (point[1] = 0; point[1] < Cols; point[1]++)
  732. {
  733. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  734. {
  735. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  736. }
  737. }
  738. }
  739. oldMat.CopyTo(analyzeMat);
  740. }
  741. else
  742. {
  743. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  744. matOrg.CopyTo(OriginalPicture);
  745. matOrg.CopyTo(analyzeMat);
  746. }
  747. Mat erzhi = OriginalPicture;
  748. Cv2.CvtColor(erzhi, erzhi, ColorConversionCodes.BGRA2GRAY);
  749. Cv2.Threshold(erzhi, erzhi, 100, 255, ThresholdTypes.Binary);//#############
  750. //Cv2.AdaptiveThreshold(erzhi, erzhi, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 99, 10);
  751. OpenCvSharp.Point[][] contours;
  752. HierarchyIndex[] hierarchy;
  753. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  754. List<OpenCvSharp.Point[]> pointss = new List<OpenCvSharp.Point[]>();
  755. foreach (OpenCvSharp.Point[] contour in contours)
  756. {
  757. if (contour.Length < 100)
  758. {
  759. //foreach (OpenCvSharp.Point point in contour)
  760. //{
  761. // erzhi.Set<byte>(point.Y, point.X, 255);
  762. //}
  763. pointss.Add(contour);
  764. }
  765. }
  766. Cv2.FillPoly(erzhi, pointss, new Scalar(255));
  767. // 二次轮廓
  768. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  769. // 计算所有轮廓质点
  770. List<Point2d> massPoints = new List<Point2d>();
  771. foreach (OpenCvSharp.Point[] contour in contours)
  772. {
  773. OpenCvSharp.Moments moments = new OpenCvSharp.Moments(contour, false);
  774. massPoints.Add(new Point2d(moments.M10 / moments.M00, moments.M01 / moments.M00));
  775. }
  776. //Cv2.ImShow("erzhi", erzhi);
  777. Mat labelMat = new Mat();
  778. Mat stats = new Mat();
  779. Mat centroids = new Mat();
  780. // 连通域数量
  781. int nccomps = Cv2.ConnectedComponentsWithStats(erzhi, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  782. // 连通域质点
  783. Dictionary<int, Point2d> keyValues = new Dictionary<int, Point2d>();
  784. massLabelPoints.Clear();
  785. foreach (var item in massPoints)
  786. {
  787. if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  788. {
  789. continue;
  790. }
  791. int label = labelMat.At<int>((int)item.Y, (int)item.X);
  792. if (0 <= label && label <= nccomps)
  793. {
  794. keyValues[label] = item;
  795. }
  796. }
  797. Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4, new Scalar(0, 0, 0, 0));//#########to debug and test
  798. // 矩形边线上的连通域
  799. ArrayList nccompSelect = new ArrayList();
  800. foreach (var pointF in pointFs)
  801. {
  802. if (labelMat.Width <= pointF.X || labelMat.Height <= pointF.Y || pointF.X < 0 || pointF.Y < 0)
  803. {
  804. continue;
  805. }
  806. //判断是否在晶界上面
  807. Vec4b vec4B = analyzeMat.At<Vec4b>((int)pointF.Y, (int)pointF.X);
  808. if (getGrainBoundryChecked() && vec4B.Item0 == 0 || getBinaryChecked() && vec4B.Item3 != 0 || !getGrainBoundryChecked() && !getBinaryChecked() && vec4B.Item0 == 0)
  809. {
  810. //temp.Set<Vec4b>((int)pointF.Y, (int)pointF.X, new Vec4b(0, 255, 0, 255));
  811. continue;//0925###19106
  812. }
  813. int label = labelMat.At<int>((int)pointF.Y, (int)pointF.X);
  814. if (!nccompSelect.Contains(label) && keyValues.ContainsKey(label) && keyValues[label] != null)
  815. {
  816. nccompSelect.Add(label);
  817. if (keyValues.ContainsKey(label) && keyValues[label] != null)
  818. massLabelPoints.Add(keyValues[label]);
  819. // massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  820. //else
  821. // massLabelPoints.Add(new Point2d((int)pointF.Y, (int)pointF.Y/*-1, -1*/));
  822. }
  823. else if (keyValues.ContainsKey(label) && keyValues[label] != null)
  824. {
  825. int iii = 1;
  826. }
  827. else if (!nccompSelect.Contains(label))
  828. {
  829. int iiii = 1;
  830. nccompSelect.Add(label);
  831. massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  832. }
  833. else
  834. {
  835. int iiii = 1;
  836. }
  837. }
  838. // 矩形内的连通域
  839. within = 0;
  840. ArrayList inccompSelect = new ArrayList();
  841. GrainSizeGuideAreaMethodModel grainSizeGuideStyleModel = guideClass.GrainSizeGuideAreaMethodModel;
  842. // 圆形
  843. if (grainSizeGuideStyleModel.circularGuideStyles != null)
  844. {
  845. foreach (var circularGuideStyle in grainSizeGuideStyleModel.circularGuideStyles)
  846. {
  847. if (this.getCurrentGuideName().Equals(circularGuideStyle.tag))
  848. {
  849. foreach (var keyValue in keyValues)
  850. {
  851. //求点到圆心的距离
  852. 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);
  853. if (!nccompSelect.Contains(keyValue.Key) && c < guideClass.Mat_lineDiameter / 2)
  854. {
  855. if (!inccompSelect.Contains(keyValue.Key))
  856. {
  857. within++;
  858. inccompSelect.Add(keyValue.Key);
  859. if (keyValue.Value != null)
  860. massLabelPoints.Add(keyValue.Value);
  861. }
  862. }
  863. }
  864. }
  865. }
  866. }
  867. // 矩形
  868. if (grainSizeGuideStyleModel.rectangleGuideStyles != null)
  869. {
  870. foreach (var rectangleGuideStyle in grainSizeGuideStyleModel.rectangleGuideStyles)
  871. {
  872. if (this.getCurrentGuideName().Equals(rectangleGuideStyle.tag))
  873. {
  874. foreach (var keyValue in keyValues)
  875. {
  876. if (!nccompSelect.Contains(keyValue.Key) && keyValue.Value.X > rectangleF.X && keyValue.Value.X < rectangleF.Right
  877. && keyValue.Value.Y > rectangleF.Y && keyValue.Value.Y < rectangleF.Bottom)
  878. {
  879. if (!inccompSelect.Contains(keyValue.Key))
  880. {
  881. within++;
  882. inccompSelect.Add(keyValue.Key);
  883. if (keyValue.Value != null)
  884. massLabelPoints.Add(keyValue.Value);
  885. }
  886. }
  887. }
  888. }
  889. }
  890. }
  891. //Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4);//###########
  892. ////Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC1);
  893. Random rd = new Random();
  894. List<Vec4b> colors = new List<Vec4b>(nccomps);
  895. colors.Add(new Vec4b(0, 0, 0, 0));
  896. for (int i = 0/*1*/; i < nccomps; i++)
  897. {
  898. //colors.Add(new Vec3b(255, 255, 255));
  899. colors.Add(new Vec4b(((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), 255));
  900. }
  901. for (int y = 0; y < temp.Rows; y++)
  902. {
  903. for (int x = 0; x < temp.Cols; x++)
  904. {
  905. int label = labelMat.At<int>(y, x);
  906. if (nccompSelect.Contains(label))
  907. temp.Set<Vec4b>(y, x, colors[label + 1]);
  908. if (inccompSelect.Contains(label))
  909. temp.Set<Vec4b>(y, x, colors[label + 1]);
  910. //temp.Set<Vec4b>(y, x, new Vec4b(127, 127, 127, 127));
  911. ////if (0 <= label && label <= nccomps)
  912. ////{
  913. //// temp.Set<Vec3b>(y, x, colors[label]);
  914. ////}
  915. }
  916. }
  917. //foreach (var item in massPoints)
  918. //{
  919. // if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  920. // {
  921. // continue;
  922. // }
  923. // temp.Set<Vec4b>((int)item.Y, (int)item.X, new Vec4b(0, 0, 255, 255));
  924. //}
  925. ////Cv2.ImShow("temp", temp);
  926. this.documentWorkspace.PhaseModels[2].mat = temp;
  927. this.documentWorkspace.Refresh();
  928. }
  929. /// <summary>
  930. /// 是否显示晶界
  931. /// </summary>
  932. /// <param name="sender"></param>
  933. /// <param name="e"></param>
  934. private void checkBox3_0_CheckedChanged(object sender, EventArgs e)
  935. {
  936. if (!showOrimat() && getGrainBoundryChecked() && checkBox3_0.Checked)
  937. {
  938. this.documentWorkspace.PhaseModels[1].choise = true;
  939. }
  940. else
  941. {
  942. this.documentWorkspace.PhaseModels[1].choise = false;
  943. }
  944. this.documentWorkspace.Refresh();
  945. }
  946. private void checkBox4_0_CheckedChanged(object sender, EventArgs e)
  947. {
  948. this.documentWorkspace.Refresh();
  949. }
  950. private void checkBox11_CheckedChanged(object sender, EventArgs e)
  951. {
  952. if (!showOrimat() && checkBox11.Checked)
  953. {
  954. this.documentWorkspace.PhaseModels[2].choise = true;
  955. }
  956. else
  957. {
  958. this.documentWorkspace.PhaseModels[2].choise = false;
  959. }
  960. this.documentWorkspace.Refresh();
  961. }
  962. private void checkBox5_0_CheckedChanged(object sender, EventArgs e)
  963. {
  964. this.documentWorkspace.Refresh();
  965. }
  966. }
  967. }