MethodAreaDialog.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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.YST347
  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 = "YST347(" + PdnResources.GetString("Menu.Areamethod.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. dgvr.Cells[4].Value = 3.3219 * Math.Log10(small_l) - 2.9542;
  256. this.dataGridView2.Rows.Add(dgvr);
  257. dataTable.TableName = tag;
  258. foreach (System.Data.DataTable dataTable1 in dataTables)
  259. {
  260. if (dataTable1.TableName.Equals(dataTable.TableName))
  261. {
  262. dataTables.Remove(dataTable1);
  263. break;
  264. }
  265. }
  266. foreach (DataGridViewColumn c in this.dataGridView2.Columns)
  267. {
  268. dataTable.Columns.Add(c.HeaderText);
  269. }
  270. for (int r = 0; r < this.dataGridView2.Rows.Count; r++)
  271. {
  272. DataRow dataRow = dataTable.NewRow();
  273. for (int c = 0; c < this.dataGridView2.Rows[r].Cells.Count; c++)
  274. {
  275. dataRow[this.dataGridView2.Columns[c].HeaderText] = this.dataGridView2.Rows[r].Cells[c].Value;
  276. }
  277. dataTable.Rows.Add(dataRow);
  278. }
  279. this.dataTables.Add(dataTable);
  280. this.RefreshDataGridView4();
  281. //保存处理后的图片
  282. List<Bitmap> tempBit = new List<Bitmap>();
  283. Bitmap originalBit = this.bitmap;// this.appWorkspace.DocumentWorkspaces[this.listView1.FocusedItem.Index].CompositionSurface.CreateAliasedBitmap();
  284. tempBit.Add(originalBit);
  285. Bitmap newBit = originalBit.Clone(new Rectangle(0, 0, originalBit.Width, originalBit.Height), originalBit.PixelFormat);
  286. Graphics graphics = Graphics.FromImage(newBit);
  287. //if (true)
  288. //{ }//0914###18810
  289. if (this.documentWorkspace.PhaseModels[0].choise && this.documentWorkspace.PhaseModels[0].mat != null)
  290. {
  291. Mat targetMat = this.documentWorkspace.PhaseModels[0].mat;
  292. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(targetMat), 0, 0, targetMat.Width, targetMat.Height);
  293. }
  294. if (this.documentWorkspace.PhaseModels[1].choise && this.documentWorkspace.PhaseModels[1].mat != null)
  295. {
  296. Mat targetMat = this.documentWorkspace.PhaseModels[1].mat;
  297. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(targetMat), 0, 0, targetMat.Width, targetMat.Height);
  298. }
  299. if (this.documentWorkspace.PhaseModels[2].choise && this.documentWorkspace.PhaseModels[2].mat != null)
  300. {
  301. Mat targetMat = this.documentWorkspace.PhaseModels[2].mat;
  302. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(targetMat), 0, 0, targetMat.Width, targetMat.Height);
  303. }
  304. graphics.SmoothingMode = SmoothingMode.AntiAlias;
  305. drawSubclass/*Draw*/(graphics);
  306. tempBit.Add(newBit);
  307. if (bitDic.ContainsKey(tag))
  308. bitDic[tag] = tempBit;
  309. else
  310. bitDic.Add(tag, tempBit);
  311. //拼接中间数据
  312. List<List<string>> dataList = new List<List<string>>();
  313. List<string> columnName = new List<string>();
  314. columnName.Add(PdnResources.GetString("Menu.Imagement.Measurementlist.ordernumber.text"));
  315. columnName.Add(PdnResources.GetString("Menu.Xoordinate.Text"));
  316. columnName.Add(PdnResources.GetString("Menu.Yoordinate.Text"));
  317. columnName.Add(PdnResources.GetString("Menu.Whethertheinternal.Text"));
  318. dataList.Add(columnName);
  319. if (massLabelPoints.Count > 0)
  320. {
  321. int index = 0;
  322. int outMaxIndex = massLabelPoints.Count - 0 - within;
  323. foreach (var item in massLabelPoints)
  324. {
  325. List<string> strList = new List<string>();
  326. strList.Add(("" + (++index)).ToString());
  327. strList.Add(item.X.ToString());
  328. strList.Add(item.Y.ToString());
  329. if (outMaxIndex < index)
  330. strList.Add(PdnResources.GetString("Menu.Yes.text"));
  331. else
  332. strList.Add(PdnResources.GetString("Menu.no.text"));
  333. dataList.Add(strList);
  334. }
  335. }
  336. bool isExist = false;//是否已存在进行替换
  337. int modelIndex = -1;//要替换的下标
  338. for (int j = 0; j < tempDataModel.Count; j++)
  339. {
  340. if (tempDataModel[j].tagName.Equals(tag))
  341. {
  342. isExist = true;
  343. modelIndex = j;
  344. break;
  345. }
  346. }
  347. if (isExist && modelIndex > -1)
  348. tempDataModel[modelIndex].dataList = dataList;
  349. else
  350. {
  351. Base.Functionodel.ExportProjectModel newModel = new Base.Functionodel.ExportProjectModel();
  352. newModel.tagName = tag;
  353. newModel.picName = imgName;
  354. newModel.dataList = dataList;
  355. tempDataModel.Add(newModel);
  356. }
  357. }
  358. }
  359. else
  360. {
  361. MessageBox.Show(PdnResources.GetString("Menu.Pleaseselectauxiliaryline.text")+"!");
  362. }
  363. }
  364. protected override void SubclassInitialize()
  365. {
  366. InitializeComponent();
  367. InitializeLanguageText();
  368. }
  369. //是否绘制辅助线
  370. protected override bool drawGuideLines()
  371. {
  372. return checkBox4_0.Checked;
  373. }
  374. //是否绘制截点
  375. protected override bool drawGuidePoints()
  376. {
  377. return (matOrg != null && this.checkBox5_0.Checked);
  378. }
  379. //是否绘制编号
  380. protected override bool drawMassLabelPoints()
  381. {
  382. return this.checkBox5_0.Checked;
  383. }
  384. //辅助线类型
  385. protected override string[] getTypeNames()
  386. {
  387. return new string[] {
  388. PdnResources.GetString("Menu.circular.Text")};
  389. }
  390. protected override string getTemplateName()
  391. {
  392. return "Template.Manager.item3.GrainSizeArea_YST347";
  393. }
  394. protected override string getProjectEngineeringName()
  395. {
  396. return "Menu.DedicatedAnalysis.NonferrousMetal.CopperAlloy.Text";
  397. }
  398. protected override string getModelName()
  399. {
  400. return "GrainGBT32461AreaMethodModel.xml";
  401. }
  402. #endregion
  403. /// <summary>
  404. /// 刷新晶粒编号
  405. /// </summary>
  406. /// <param name="sender"></param>
  407. /// <param name="e"></param>
  408. private void button4_Click(object sender, EventArgs e)
  409. {
  410. if (this.matOrg == null)
  411. {
  412. MessageBox.Show(PdnResources.GetString("Menu.Pleaseselectapicturefirst.text"));
  413. return;
  414. }
  415. Getrains(linePointList, guideClass.RectangleFLine);
  416. }
  417. private Double GetNumberOfGrains(List<PointF> pointFs, RectangleF rectangleF)
  418. {
  419. Mat OriginalPicture;// = new Mat(mat.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  420. Mat analyzeMat = new Mat();
  421. if (getGrainBoundryChecked())
  422. {
  423. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  424. Mat oldMat = this.documentWorkspace.PhaseModels[1].mat;
  425. int Rows = oldMat.Rows;
  426. int Cols = oldMat.Cols;
  427. int[] point = new int[2];
  428. //多通道图像的格式化
  429. for (point[0] = 0; point[0] < Rows; point[0]++)
  430. {
  431. for (point[1] = 0; point[1] < Cols; point[1]++)
  432. {
  433. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  434. {
  435. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  436. }
  437. }
  438. }
  439. Cv2.Erode(OriginalPicture, OriginalPicture, null, null, 1);
  440. }
  441. else if (getBinaryChecked())
  442. {
  443. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  444. Mat oldMat = this.documentWorkspace.PhaseModels[0].mat;
  445. int Rows = oldMat.Rows;
  446. int Cols = oldMat.Cols;
  447. int[] point = new int[2];
  448. //多通道图像的格式化
  449. for (point[0] = 0; point[0] < Rows; point[0]++)
  450. {
  451. for (point[1] = 0; point[1] < Cols; point[1]++)
  452. {
  453. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  454. {
  455. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  456. }
  457. }
  458. }
  459. }
  460. else
  461. {
  462. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  463. matOrg.CopyTo(OriginalPicture);
  464. }
  465. OriginalPicture.CopyTo(analyzeMat);
  466. Mat erzhi = OriginalPicture;
  467. Cv2.CvtColor(erzhi, erzhi, ColorConversionCodes.BGR2GRAY);
  468. Cv2.Threshold(erzhi, erzhi, 100, 255, ThresholdTypes.Binary);//#############
  469. //Cv2.AdaptiveThreshold(erzhi, erzhi, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 99, 10);
  470. OpenCvSharp.Point[][] contours;
  471. HierarchyIndex[] hierarchy;
  472. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  473. //Cv2.ImWrite("erzhi1.tif", erzhi.CvtColor(ColorConversionCodes.GRAY2BGR));
  474. List<OpenCvSharp.Point[]> pointss = new List<OpenCvSharp.Point[]>();
  475. foreach (OpenCvSharp.Point[] contour in contours)
  476. {
  477. if (contour.Length < 100)
  478. {
  479. foreach (OpenCvSharp.Point point in contour)
  480. {
  481. erzhi.Set<byte>(point.Y, point.X, 255);
  482. }
  483. pointss.Add(contour);
  484. }
  485. }
  486. Cv2.FillPoly(erzhi, pointss, new Scalar(255));
  487. // 二次轮廓
  488. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  489. // 计算所有轮廓质点
  490. List<Point2d> massPoints = new List<Point2d>();
  491. foreach (OpenCvSharp.Point[] contour in contours)
  492. {
  493. OpenCvSharp.Moments moments = new OpenCvSharp.Moments(contour, false);
  494. massPoints.Add(new Point2d(moments.M10 / moments.M00, moments.M01 / moments.M00));
  495. }
  496. Mat labelMat = new Mat();
  497. Mat stats = new Mat();
  498. Mat centroids = new Mat();
  499. // 连通域数量
  500. int nccomps = Cv2.ConnectedComponentsWithStats(erzhi, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  501. //Cv2.ImWrite("erzhi2.tif", labelMat);
  502. //Cv2.ImWrite("erzhi3.tif", stats);
  503. //Cv2.ImWrite("erzhi4.tif", centroids);
  504. // 连通域质点
  505. Dictionary<int, Point2d> keyValues = new Dictionary<int, Point2d>();
  506. massLabelPoints.Clear();
  507. foreach (var item in massPoints)
  508. {
  509. if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  510. {
  511. continue;
  512. }
  513. int label = labelMat.At<int>((int)item.Y, (int)item.X);
  514. if (0 <= label && label <= nccomps)
  515. {
  516. keyValues[label] = item;
  517. }
  518. }
  519. // 矩形边线上的连通域
  520. ArrayList nccompSelect = new ArrayList();
  521. foreach (var pointF in pointFs)
  522. {
  523. if (labelMat.Width <= pointF.X || labelMat.Height <= pointF.Y || pointF.X < 0 || pointF.Y < 0)
  524. {
  525. continue;
  526. }
  527. //判断是否在晶界上面
  528. Vec4b vec4B = analyzeMat.At<Vec4b>((int)pointF.Y, (int)pointF.X);
  529. if (getGrainBoundryChecked() && vec4B.Item3 != 0 || getBinaryChecked() && vec4B.Item3 != 0 || !getGrainBoundryChecked() && !getBinaryChecked() && vec4B.Item0 == 0)
  530. {
  531. //temp.Set<Vec4b>((int)pointF.Y, (int)pointF.X, new Vec4b(0, 255, 0, 255));
  532. continue;//0925###19106
  533. }
  534. int label = labelMat.At<int>((int)pointF.Y, (int)pointF.X);
  535. if (!nccompSelect.Contains(label) && keyValues.ContainsKey(label) && keyValues[label] != null)
  536. {
  537. nccompSelect.Add(label);
  538. if (keyValues.ContainsKey(label) && keyValues[label] != null)
  539. massLabelPoints.Add(keyValues[label]);
  540. // massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  541. //else
  542. // massLabelPoints.Add(new Point2d((int)pointF.Y, (int)pointF.Y/*-1, -1*/));
  543. }
  544. }
  545. // 矩形内的连通域
  546. within = 0;
  547. ArrayList inccompSelect = new ArrayList();
  548. GrainSizeGuideAreaMethodModel grainSizeGuideStyleModel = guideClass.GrainSizeGuideAreaMethodModel;
  549. // 圆形
  550. if (grainSizeGuideStyleModel.circularGuideStyles != null)
  551. {
  552. foreach (var circularGuideStyle in grainSizeGuideStyleModel.circularGuideStyles)
  553. {
  554. if (this.comboBox1.SelectedItem.Equals(circularGuideStyle.tag))
  555. {
  556. foreach (var keyValue in keyValues)
  557. {
  558. //求点到圆心的距离
  559. 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);
  560. if (!nccompSelect.Contains(keyValue.Key) && c < guideClass.Mat_lineDiameter / 2)
  561. {
  562. if (!inccompSelect.Contains(keyValue.Key))
  563. {
  564. within++;
  565. inccompSelect.Add(keyValue.Key);
  566. if (keyValue.Value != null)
  567. massLabelPoints.Add(keyValue.Value);
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }
  574. // 矩形
  575. if (grainSizeGuideStyleModel.rectangleGuideStyles != null)
  576. {
  577. foreach (var rectangleGuideStyle in grainSizeGuideStyleModel.rectangleGuideStyles)
  578. {
  579. if (this.comboBox1.SelectedItem.Equals(rectangleGuideStyle.tag))
  580. {
  581. foreach (var keyValue in keyValues)
  582. {
  583. if (!nccompSelect.Contains(keyValue.Key) && keyValue.Value.X > rectangleF.X && keyValue.Value.X < rectangleF.Right
  584. && keyValue.Value.Y > rectangleF.Y && keyValue.Value.Y < rectangleF.Bottom)
  585. {
  586. if (!inccompSelect.Contains(keyValue.Key))
  587. {
  588. within++;
  589. inccompSelect.Add(keyValue.Key);
  590. if (keyValue.Value != null)
  591. massLabelPoints.Add(keyValue.Value);
  592. }
  593. }
  594. }
  595. }
  596. }
  597. }
  598. Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4);
  599. //Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC1);
  600. Random rd = new Random();
  601. List<Vec4b> colors = new List<Vec4b>(nccomps);
  602. colors.Add(new Vec4b(0, 0, 0, 0));
  603. for (int i = 1; i < nccomps; i++)
  604. {
  605. //colors.Add(new Vec3b(255, 255, 255));
  606. colors.Add(new Vec4b(((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), 255));
  607. }
  608. for (int y = 0; y < temp.Rows; y++)
  609. {
  610. for (int x = 0; x < temp.Cols; x++)
  611. {
  612. int label = labelMat.At<int>(y, x);
  613. if (nccompSelect.Contains(label))
  614. temp.Set<Vec4b>(y, x, colors[label]);
  615. if (inccompSelect.Contains(label))
  616. temp.Set<Vec4b>(y, x, colors[label]);
  617. //temp.Set<Vec4b>(y, x, new Vec4b(127, 127, 127, 127));
  618. ////if (0 <= label && label <= nccomps)
  619. ////{
  620. //// temp.Set<Vec3b>(y, x, colors[label]);
  621. ////}
  622. }
  623. }
  624. //foreach (var item in massPoints)
  625. //{
  626. // if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  627. // {
  628. // continue;
  629. // }
  630. // temp.Set<Vec4b>((int)item.Y, (int)item.X, new Vec4b(0, 0, 255, 255));
  631. //}
  632. //Cv2.ImWrite("erzhi5.tif", temp);
  633. ////Cv2.ImShow("temp", temp);
  634. this.documentWorkspace.PhaseModels[2].mat = temp;
  635. this.documentWorkspace.Refresh();
  636. return within + (double)nccompSelect.Count / 2;
  637. }
  638. /// <summary>
  639. /// 刷新晶粒编号
  640. /// </summary>
  641. /// <param name="pointFs"></param>
  642. /// <param name="rectangleF"></param>
  643. private void Getrains(List<PointF> pointFs, RectangleF rectangleF)
  644. {
  645. Mat OriginalPicture;
  646. if (getGrainBoundryChecked())
  647. {
  648. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  649. Mat oldMat = this.documentWorkspace.PhaseModels[1].mat;
  650. int Rows = oldMat.Rows;
  651. int Cols = oldMat.Cols;
  652. int[] point = new int[2];
  653. //多通道图像的格式化
  654. for (point[0] = 0; point[0] < Rows; point[0]++)
  655. {
  656. for (point[1] = 0; point[1] < Cols; point[1]++)
  657. {
  658. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  659. {
  660. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  661. }
  662. }
  663. }
  664. Cv2.Erode(OriginalPicture, OriginalPicture, null, null, 1);
  665. }
  666. else if (getBinaryChecked())
  667. {
  668. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  669. Mat oldMat = this.documentWorkspace.PhaseModels[0].mat;
  670. int Rows = oldMat.Rows;
  671. int Cols = oldMat.Cols;
  672. int[] point = new int[2];
  673. //多通道图像的格式化
  674. for (point[0] = 0; point[0] < Rows; point[0]++)
  675. {
  676. for (point[1] = 0; point[1] < Cols; point[1]++)
  677. {
  678. if (oldMat.At<Vec4b>(point[0], point[1]).Item3 > 0)
  679. {
  680. OriginalPicture.Set<Vec4b>(point, new Vec4b(0, 0, 0, 255));
  681. }
  682. }
  683. }
  684. }
  685. else
  686. {
  687. OriginalPicture = new Mat(matOrg.Size(), MatType.CV_8UC4, new Scalar(255, 255, 255, 255));
  688. matOrg.CopyTo(OriginalPicture);
  689. }
  690. Mat analyzeMat = OriginalPicture.Clone();
  691. Mat erzhi = OriginalPicture;
  692. Cv2.CvtColor(erzhi, erzhi, ColorConversionCodes.BGR2GRAY);
  693. Cv2.Threshold(erzhi, erzhi, 100, 255, ThresholdTypes.Binary);//#############
  694. //Cv2.AdaptiveThreshold(erzhi, erzhi, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 99, 10);
  695. OpenCvSharp.Point[][] contours;
  696. HierarchyIndex[] hierarchy;
  697. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  698. List<OpenCvSharp.Point[]> pointss = new List<OpenCvSharp.Point[]>();
  699. foreach (OpenCvSharp.Point[] contour in contours)
  700. {
  701. if (contour.Length < 100)
  702. {
  703. foreach (OpenCvSharp.Point point in contour)
  704. {
  705. erzhi.Set<byte>(point.Y, point.X, 255);
  706. }
  707. pointss.Add(contour);
  708. }
  709. }
  710. Cv2.FillPoly(erzhi, pointss, new Scalar(255));
  711. // 二次轮廓
  712. Cv2.FindContours(erzhi, out contours, out hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxNone);
  713. // 计算所有轮廓质点
  714. List<Point2d> massPoints = new List<Point2d>();
  715. foreach (OpenCvSharp.Point[] contour in contours)
  716. {
  717. OpenCvSharp.Moments moments = new OpenCvSharp.Moments(contour, false);
  718. massPoints.Add(new Point2d(moments.M10 / moments.M00, moments.M01 / moments.M00));
  719. }
  720. Mat labelMat = new Mat();
  721. Mat stats = new Mat();
  722. Mat centroids = new Mat();
  723. // 连通域数量
  724. int nccomps = Cv2.ConnectedComponentsWithStats(erzhi, labelMat, stats, centroids, PixelConnectivity.Connectivity8);
  725. // 连通域质点
  726. Dictionary<int, Point2d> keyValues = new Dictionary<int, Point2d>();
  727. massLabelPoints.Clear();
  728. foreach (var item in massPoints)
  729. {
  730. if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  731. {
  732. continue;
  733. }
  734. int label = labelMat.At<int>((int)item.Y, (int)item.X);
  735. if (0 <= label && label <= nccomps)
  736. {
  737. keyValues[label] = item;
  738. }
  739. }
  740. Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4);//#########to debug and test
  741. // 矩形边线上的连通域
  742. ArrayList nccompSelect = new ArrayList();
  743. foreach (var pointF in pointFs)
  744. {
  745. if (labelMat.Width <= pointF.X || labelMat.Height <= pointF.Y || pointF.X < 0 || pointF.Y < 0)
  746. {
  747. continue;
  748. }
  749. //判断是否在晶界上面
  750. Vec4b vec4B = analyzeMat.At<Vec4b>((int)pointF.Y, (int)pointF.X);
  751. if (getGrainBoundryChecked() && vec4B.Item3 != 0 || getBinaryChecked() && vec4B.Item3 != 0 || !getGrainBoundryChecked() && !getBinaryChecked() && vec4B.Item0 == 0)
  752. {
  753. //temp.Set<Vec4b>((int)pointF.Y, (int)pointF.X, new Vec4b(0, 255, 0, 255));
  754. continue;//0925###19106
  755. }
  756. int label = labelMat.At<int>((int)pointF.Y, (int)pointF.X);
  757. if (!nccompSelect.Contains(label) && keyValues.ContainsKey(label) && keyValues[label] != null)
  758. {
  759. nccompSelect.Add(label);
  760. if (keyValues.ContainsKey(label) && keyValues[label] != null)
  761. massLabelPoints.Add(keyValues[label]);
  762. // massLabelPoints.Add(new Point2d((int)pointF.X, (int)pointF.Y/*-1, -1*/));
  763. //else
  764. // massLabelPoints.Add(new Point2d((int)pointF.Y, (int)pointF.Y/*-1, -1*/));
  765. }
  766. }
  767. // 矩形内的连通域
  768. within = 0;
  769. ArrayList inccompSelect = new ArrayList();
  770. GrainSizeGuideAreaMethodModel grainSizeGuideStyleModel = guideClass.GrainSizeGuideAreaMethodModel;
  771. // 圆形
  772. if (grainSizeGuideStyleModel.circularGuideStyles != null)
  773. {
  774. foreach (var circularGuideStyle in grainSizeGuideStyleModel.circularGuideStyles)
  775. {
  776. if (this.getCurrentGuideName().Equals(circularGuideStyle.tag))
  777. {
  778. foreach (var keyValue in keyValues)
  779. {
  780. //求点到圆心的距离
  781. 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);
  782. if (!nccompSelect.Contains(keyValue.Key) && c < guideClass.Mat_lineDiameter / 2)
  783. {
  784. if (!inccompSelect.Contains(keyValue.Key))
  785. {
  786. within++;
  787. inccompSelect.Add(keyValue.Key);
  788. if (keyValue.Value != null)
  789. massLabelPoints.Add(keyValue.Value);
  790. }
  791. }
  792. }
  793. }
  794. }
  795. }
  796. // 矩形
  797. if (grainSizeGuideStyleModel.rectangleGuideStyles != null)
  798. {
  799. foreach (var rectangleGuideStyle in grainSizeGuideStyleModel.rectangleGuideStyles)
  800. {
  801. if (this.getCurrentGuideName().Equals(rectangleGuideStyle.tag))
  802. {
  803. foreach (var keyValue in keyValues)
  804. {
  805. if (!nccompSelect.Contains(keyValue.Key) && keyValue.Value.X > rectangleF.X && keyValue.Value.X < rectangleF.Right
  806. && keyValue.Value.Y > rectangleF.Y && keyValue.Value.Y < rectangleF.Bottom)
  807. {
  808. if (!inccompSelect.Contains(keyValue.Key))
  809. {
  810. within++;
  811. inccompSelect.Add(keyValue.Key);
  812. if (keyValue.Value != null)
  813. massLabelPoints.Add(keyValue.Value);
  814. }
  815. }
  816. }
  817. }
  818. }
  819. }
  820. //Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC4);//###########
  821. ////Mat temp = new Mat(erzhi.Size(), MatType.CV_8UC1);
  822. Random rd = new Random();
  823. List<Vec4b> colors = new List<Vec4b>(nccomps);
  824. colors.Add(new Vec4b(0, 0, 0, 0));
  825. for (int i = 1; i < nccomps; i++)
  826. {
  827. //colors.Add(new Vec3b(255, 255, 255));
  828. colors.Add(new Vec4b(((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), ((byte)(rd.Next() % 256)), 255));
  829. }
  830. for (int y = 0; y < temp.Rows; y++)
  831. {
  832. for (int x = 0; x < temp.Cols; x++)
  833. {
  834. int label = labelMat.At<int>(y, x);
  835. if (nccompSelect.Contains(label))
  836. temp.Set<Vec4b>(y, x, colors[label]);
  837. if (inccompSelect.Contains(label))
  838. temp.Set<Vec4b>(y, x, colors[label]);
  839. //temp.Set<Vec4b>(y, x, new Vec4b(127, 127, 127, 127));
  840. ////if (0 <= label && label <= nccomps)
  841. ////{
  842. //// temp.Set<Vec3b>(y, x, colors[label]);
  843. ////}
  844. }
  845. }
  846. //foreach (var item in massPoints)
  847. //{
  848. // if (double.IsNaN(item.Y) || double.IsNaN(item.X))
  849. // {
  850. // continue;
  851. // }
  852. // temp.Set<Vec4b>((int)item.Y, (int)item.X, new Vec4b(0, 0, 255, 255));
  853. //}
  854. ////Cv2.ImShow("temp", temp);
  855. this.documentWorkspace.PhaseModels[2].mat = temp;
  856. this.documentWorkspace.Refresh();
  857. }
  858. /// <summary>
  859. /// 是否显示晶界
  860. /// </summary>
  861. /// <param name="sender"></param>
  862. /// <param name="e"></param>
  863. private void checkBox3_0_CheckedChanged(object sender, EventArgs e)
  864. {
  865. if (!showOrimat() && getGrainBoundryChecked() && checkBox3_0.Checked)
  866. {
  867. this.documentWorkspace.PhaseModels[1].choise = true;
  868. }
  869. else
  870. {
  871. this.documentWorkspace.PhaseModels[1].choise = false;
  872. }
  873. this.documentWorkspace.Refresh();
  874. }
  875. private void checkBox4_0_CheckedChanged(object sender, EventArgs e)
  876. {
  877. this.documentWorkspace.Refresh();
  878. }
  879. private void checkBox11_CheckedChanged(object sender, EventArgs e)
  880. {
  881. if (!showOrimat() && checkBox11.Checked)
  882. {
  883. this.documentWorkspace.PhaseModels[2].choise = true;
  884. }
  885. else
  886. {
  887. this.documentWorkspace.PhaseModels[2].choise = false;
  888. }
  889. this.documentWorkspace.Refresh();
  890. }
  891. private void checkBox5_0_CheckedChanged(object sender, EventArgs e)
  892. {
  893. this.documentWorkspace.Refresh();
  894. }
  895. }
  896. }