using OpenCvSharp; using OpenCvSharp.Extensions; using PaintDotNet.Annotation; using PaintDotNet.Annotation.Enum; using PaintDotNet.Annotation.FieldView; using PaintDotNet.Base.DedicatedAnalysis.Porosity; using PaintDotNet.Base.DedicatedAnalysis.Porosity.Standard.TypesOfAssessment; using PaintDotNet.Base.Functionodel; using PaintDotNet.GeneralAnalysis; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PaintDotNet.DedicatedAnalysis.Porosity.GBT37787 { partial class GBT37787Dialog : PorosityStandardDialog { private GroupBox groupBox1; private DataGridView dataGridView1; private DataGridViewTextBoxColumn Column11; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3; private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4; public GBT37787Dialog(AppWorkspace appWorkspace, string dialogText, PdnMenuItem menuItem) : base(appWorkspace, dialogText, menuItem) { InitializeComponent(); InitializeLanguageText(); } private void InitializeLanguageText() { this.dataGridViewTextBoxColumn2.HeaderText = PdnResources.GetString("Menu.Phasearea.text"); this.dataGridViewTextBoxColumn1.HeaderText = PdnResources.GetString("Menu.Phasename.text"); this.Column11.HeaderText = PdnResources.GetString("Menu.view.text"); this.groupBox1.Text = PdnResources.GetString("Menu.reckoning.text"); this.dataGridViewTextBoxColumn3.HeaderText = PdnResources.GetString("Menu.proportion.text"); this.dataGridViewTextBoxColumn4.HeaderText = PdnResources.GetString("Menu.Phasecontent.text"); this.Text = "GBT37787_2019(" + PdnResources.GetString("Menu.Porosity.text") + ")"; } private void InitializeComponent() { System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.Column11 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown_lwRatioLimit)).BeginInit(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.dataGridView1); this.groupBox1.Location = new System.Drawing.Point(178, 370); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(356, 186); this.groupBox1.TabIndex = 35; this.groupBox1.TabStop = false; // // dataGridView1 // this.dataGridView1.AllowUserToAddRows = false; this.dataGridView1.BackgroundColor = System.Drawing.SystemColors.ControlLightLight; dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control; dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText; dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight; dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText; dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True; this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1; this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column11, this.dataGridViewTextBoxColumn1, this.dataGridViewTextBoxColumn2, this.dataGridViewTextBoxColumn3, this.dataGridViewTextBoxColumn4}); this.dataGridView1.Location = new System.Drawing.Point(8, 20); this.dataGridView1.MultiSelect = false; this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.ReadOnly = true; this.dataGridView1.RowHeadersVisible = false; this.dataGridView1.RowTemplate.Height = 23; this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; this.dataGridView1.Size = new System.Drawing.Size(340, 157); this.dataGridView1.TabIndex = 1; // // Column11 // dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; this.Column11.DefaultCellStyle = dataGridViewCellStyle2; this.Column11.Name = "Column11"; this.Column11.ReadOnly = true; this.Column11.Width = 80; // // dataGridViewTextBoxColumn1 // dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; this.dataGridViewTextBoxColumn1.DefaultCellStyle = dataGridViewCellStyle3; this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1"; this.dataGridViewTextBoxColumn1.ReadOnly = true; this.dataGridViewTextBoxColumn1.Width = 70; // // dataGridViewTextBoxColumn2 // dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; this.dataGridViewTextBoxColumn2.DefaultCellStyle = dataGridViewCellStyle4; this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2"; this.dataGridViewTextBoxColumn2.ReadOnly = true; this.dataGridViewTextBoxColumn2.Width = 70; // // dataGridViewTextBoxColumn3 // dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; this.dataGridViewTextBoxColumn3.DefaultCellStyle = dataGridViewCellStyle5; this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3"; this.dataGridViewTextBoxColumn3.ReadOnly = true; this.dataGridViewTextBoxColumn3.Width = 60; // // dataGridViewTextBoxColumn4 // dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; this.dataGridViewTextBoxColumn4.DefaultCellStyle = dataGridViewCellStyle6; this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4"; this.dataGridViewTextBoxColumn4.ReadOnly = true; this.dataGridViewTextBoxColumn4.Width = 70; // // GBT37787Dialog // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.ClientSize = new System.Drawing.Size(1177, 761); this.Controls.Add(this.groupBox1); this.Name = "GBT37787Dialog"; this.Controls.SetChildIndex(this.groupBox1, 0); ((System.ComponentModel.ISupportInitialize)(this.numericUpDown_lwRatioLimit)).EndInit(); this.groupBox1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } #region 需继承方法 protected override void SubclassInitialize() { InitializeComponent(); } protected override MethodOfAssessment getMethodOfAssessment() { return new MethodOfGBT37787(this.imgPxPerUnit); } protected override void initColorSetting() { //this.radioButton_Black.CheckedChanged += new System.EventHandler(colorSettingRadioButton); //this.radioButton_Gray.CheckedChanged += new System.EventHandler(colorSettingRadioButton); //this.radioButton_LightBlue.CheckedChanged += new System.EventHandler(colorSettingRadioButton); //this.radioButton_DarkGray.CheckedChanged += new System.EventHandler(colorSettingRadioButton); } protected override void initDisplaySetting() { //this.panel_displaySetting_SS.BackColor = Color.FromArgb(MethodOfAssessment.activeMethod.inclusionsStandard.globalSettings.typeDics["SS"].showColor); //this.panel_displaySetting_OS.BackColor = Color.FromArgb(MethodOfAssessment.activeMethod.inclusionsStandard.globalSettings.typeDics["OS"].showColor); //this.panel_displaySetting_OA.BackColor = Color.FromArgb(MethodOfAssessment.activeMethod.inclusionsStandard.globalSettings.typeDics["OA"].showColor); //this.panel_displaySetting_OG.BackColor = Color.FromArgb(MethodOfAssessment.activeMethod.inclusionsStandard.globalSettings.typeDics["OG"].showColor); } protected override void list_add_data() { } #endregion 需继承方法 /// /// 获取视场即相相关参数 /// protected override void getAllView() { double pxPerUnit = Startup.instance.rules[MeasurementUnit.Micron]; if (this.documentWorkspace.phaseModels.Count == 0) { MessageBox.Show(PdnResources.GetString("Menu.Thereisnophasetoanalyze.text")+"!"); return; } dataGridView1.Rows.Clear(); if (this.lstView_img.FocusedItem != null) { //总面积 double fullArea = AnalysisTools.GetPointsFromMat(this.imageMat.Clone()) * pxPerUnit * pxPerUnit; int num = 2; if (this.domainUpDown_resultPrecision.SelectedIndex != -1) { num = Convert.ToInt32(this.domainUpDown_resultPrecision.Items[this.domainUpDown_resultPrecision.SelectedIndex]); } if (this.documentWorkspace.GraphicsList.IsExsitView()) { int m = 0; for (int k = 0; k < this.documentWorkspace.GraphicsList.Count; k++) { if (this.documentWorkspace.GraphicsList[k].objectType != DrawClass.View || (this.documentWorkspace.GraphicsList[k].Rectangle.Width == 0 && this.documentWorkspace.GraphicsList[k].Rectangle.Height == 0)) { continue; } for (int i = 0; i < this.originalPhaseModels.Count; i++) { PhaseModel pModel = this.originalPhaseModels[i]; if (pModel.mat == null) { pModel.mat = this.documentWorkspace.phaseModels[i].mat; } // TODO 用于在一张图中扣出所有视场的Mat Bitmap bitmap = this.appWorkspace.DocumentWorkspaces[this.lstView_img.FocusedItem.Index].GetCutSizeWithColorWhiteOrTransparentColor(false); Mat oldMapabc = PaintDotNet.Camera.Tools.ToMat(bitmap); int X = (int)this.documentWorkspace.GraphicsList[k].Rectangle.X; int Y = (int)this.documentWorkspace.GraphicsList[k].Rectangle.Y; int reWidth = (int)this.documentWorkspace.GraphicsList[k].Rectangle.Width; int reHeight = (int)this.documentWorkspace.GraphicsList[k].Rectangle.Height; if (this.documentWorkspace.GraphicsList[k].Rectangle.Contains(new RectangleF(0, 0, bitmap.Width, bitmap.Height))) { X = 0; Y = 0; reWidth = bitmap.Width; reHeight = bitmap.Height; } else { if (this.documentWorkspace.GraphicsList[k].Rectangle.IntersectsWith(new RectangleF(0, 0, bitmap.Width, bitmap.Height))) { RectangleF rectangleF = new RectangleF(this.documentWorkspace.GraphicsList[k].Rectangle.X, this.documentWorkspace.GraphicsList[k].Rectangle.Y, this.documentWorkspace.GraphicsList[k].Rectangle.Width, this.documentWorkspace.GraphicsList[k].Rectangle.Height); rectangleF.Intersect(new RectangleF(0, 0, bitmap.Width, bitmap.Height)); X = (int)rectangleF.X; Y = (int)rectangleF.Y; reWidth = (int)rectangleF.Width; reHeight = (int)rectangleF.Height; } } Mat oldMap = new Mat(oldMapabc, new Rect(X, Y, reWidth, reHeight)); //寻找并标记区域个数 Mat labelMat = new Mat(); Mat stats = new Mat(); Mat centroids = new Mat(); int nonenum = Cv2.ConnectedComponentsWithStats(oldMap.CvtColor(ColorConversionCodes.BGR2GRAY), labelMat, stats, centroids, PixelConnectivity.Connectivity8); for (int h = 1; h < centroids.Cols; h++) { int x = stats.At(h, 0); int y = stats.At(h, 1); int width = stats.At(h, 2); int height = stats.At(h, 3); int area = stats.At(h, 4); //if (x < 0 || y < 0 || width > bitmap.Width || height > bitmap.Height) //{ // continue; //} Mat ImageROI1 = new Mat(pModel.mat, new Rect(X, Y, reWidth, reHeight)); Mat[] contours = null; Mat hierarchy = new Mat(); Mat mat = Mat.FromImageData(ImageROI1.ToBytes(), ImreadModes.Grayscale); Cv2.FindContours(mat, out contours, hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone); CounterResult result = new CounterResult(); result.GraphName = lstView_img.FocusedItem.Text; result.PolyphaseName = pModel.name; result.Count = contours.Count(); //视场面积 int viewArea = (int)(this.documentWorkspace.GraphicsList[k].Rectangle.Width * this.documentWorkspace.GraphicsList[k].Rectangle.Height * pxPerUnit * pxPerUnit); //相面积 double phaseArea = AnalysisTools.GetPointsFromMat(ImageROI1) * pxPerUnit * pxPerUnit; result.Area = Convert.ToDouble(Math.Round(Convert.ToDecimal(phaseArea), num)); // 比例 result.AreaContent = viewArea > 0 ? (double)(((double)phaseArea) / ((double)viewArea)) * 100 : 0; result.AreaContent = Convert.ToDouble(Math.Round(Convert.ToDecimal(result.AreaContent), num)); //相含量 result.AreaPercent = fullArea > 0 ? (double)(result.Area / fullArea) * 100 : 0; result.AreaPercent = Convert.ToDouble(Math.Round(Convert.ToDecimal(result.AreaPercent), num)); dataGridView1.Rows.Add(PdnResources.GetString("Menu.view.text") + m + "-相" + (h -1), result.PolyphaseName, result.Area, result.AreaContent, result.AreaPercent); m++; } } } } else { for (int i = 0; i < this.originalPhaseModels.Count; i++) { PhaseModel pModel = this.originalPhaseModels[i]; if (pModel.mat == null) { pModel.mat = this.documentWorkspace.phaseModels[i].mat; } Mat[] contours = null; Mat hierarchy = new Mat(); Mat mat = Mat.FromImageData(pModel.mat.ToBytes(), ImreadModes.Grayscale); Cv2.FindContours(mat, out contours, hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxNone); CounterResult result = new CounterResult(); result.GraphName = lstView_img.FocusedItem.Text; result.PolyphaseName = pModel.name; result.Count = contours.Count(); //视场面积 double viewArea = this.imageMat.Width * this.imageMat.Height * pxPerUnit * pxPerUnit; //相面积 double phaseArea = AnalysisTools.GetPointsFromMat(pModel.mat) * pxPerUnit * pxPerUnit; result.Area = Convert.ToDouble(Math.Round(Convert.ToDecimal(phaseArea), num)); // 比例 result.AreaContent = viewArea > 0 ? (double)(((double)phaseArea) / ((double)viewArea)) * 100 : 0; result.AreaContent = Convert.ToDouble(Math.Round(Convert.ToDecimal(result.AreaContent), num)); //相含量 result.AreaPercent = fullArea > 0 ? (double)(result.Area / fullArea) * 100 : 0; result.AreaPercent = Convert.ToDouble(Math.Round(Convert.ToDecimal(result.AreaPercent), num)); dataGridView1.Rows.Add(PdnResources.GetString("Menu.view.text")+ "1", result.PolyphaseName, result.Area, result.AreaContent, result.AreaPercent); } } } } protected override void clearAllView() { dataGridView1.Rows.Clear(); } protected override void clearOneView() { if (dataGridView1.Rows.Count > 0) { dataGridView1.Rows.RemoveAt(this.delIndex); } } protected override void changeOneViewColor() { if (dataGridView1.Rows.Count > 0) { dataGridView1.Rows[this.delIndex].DefaultCellStyle.BackColor = this.documentItems[this.lstView_img.FocusedItem.Index].drawGraphicsList[this.delIndex].Color; } } } }