using OpenCvSharp; using PaintDotNet.Annotation.Enum; using PaintDotNet.CustomControl; using PaintDotNet.Data.Param; using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace PaintDotNet.Instrument { /// /// 工具-划痕处理 /// internal partial class ScratchTreatmentDialog : FloatingToolForm/*PdnBaseForm*/ { /// /// 主工作空间 /// private AppWorkspace AppWorkspace; /// /// 当前处理的程序 /// private ParamObject action; /// /// 原图片备份 /// private Mat oldmat; /// /// 实现撤销的功能,将直线线宽、起点X Y、终点X Y的集合放入一个List /// private List OperationsList = new List(); #region 控件相关 private Button button3; private Button button4; private Button button5; /// /// 操作区控件 /// private GroupBox groupBox1; private GroupBox groupBox2; private Label label1; private Label label2; private RadioButton checkBox1; private RadioButton checkBox2; private RadioButton checkBox3; private RadioButton checkBox4; private UserTextBox textBox1;//痕宽:只能输入数字 private CustomControl.SelectButton s12Button; private CustomControl.SelectButton s9Button; private CustomControl.SelectButton s6Button; private CustomControl.SelectButton logButton; private List listButton = new List(); private void InitializeLanguageText() { this.button3.Text = PdnResources.GetString("Menu.application.text"); this.button4.Text = PdnResources.GetString("Menu.ensure.text"); this.button5.Text = PdnResources.GetString("CommonAction.Undo"); this.button5.Enabled = false; this.label1.Text = PdnResources.GetString("Menu.tool.Stainprocessing.Shapesettings.text") + ":"; this.label2.Text = PdnResources.GetString("Menu.tool.scratching.Tracewidth.text") + ":"; this.checkBox1.Text = PdnResources.GetString("Menu.LabelAction.DrawRectangle.Text"); this.checkBox2.Text = PdnResources.GetString("Menu.LabelAction.DrawPolygon.Text"); this.checkBox3.Text = PdnResources.GetString("Menu.circular.Text"); this.checkBox4.Text = PdnResources.GetString("Menu.LabelAction.DrawEllipse.Text"); this.listButton.Add(this.s12Button); this.listButton.Add(this.s9Button); this.listButton.Add(this.s6Button); this.listButton.Add(this.logButton); } private void InitializeComponent() { this.groupBox2 = new System.Windows.Forms.GroupBox(); this.label2 = new System.Windows.Forms.Label(); this.logButton = new PaintDotNet.CustomControl.SelectButton(); this.s6Button = new PaintDotNet.CustomControl.SelectButton(); this.s9Button = new PaintDotNet.CustomControl.SelectButton(); this.s12Button = new PaintDotNet.CustomControl.SelectButton(); this.textBox1 = new PaintDotNet.CustomControl.UserTextBox(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.checkBox1 = new System.Windows.Forms.RadioButton(); this.checkBox2 = new System.Windows.Forms.RadioButton(); this.checkBox3 = new System.Windows.Forms.RadioButton(); this.checkBox4 = new System.Windows.Forms.RadioButton(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // groupBox2 // this.groupBox2.Controls.Add(this.label2); this.groupBox2.Controls.Add(this.logButton); this.groupBox2.Controls.Add(this.s6Button); this.groupBox2.Controls.Add(this.s9Button); this.groupBox2.Controls.Add(this.s12Button); this.groupBox2.Controls.Add(this.textBox1); this.groupBox2.Location = new System.Drawing.Point(12, 6); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(271, 80); this.groupBox2.TabIndex = 4; this.groupBox2.TabStop = false; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(5, 24); this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(41, 12); this.label2.TabIndex = 10; this.label2.Text = "痕宽:"; // // logButton // this.logButton.BackColor = System.Drawing.SystemColors.ControlDark; this.logButton.BtnSelect = false; this.logButton.BtnText = "3"; this.logButton.Location = new System.Drawing.Point(54, 50); this.logButton.Name = "logButton"; this.logButton.Size = new System.Drawing.Size(31, 21); this.logButton.TabIndex = 21; this.logButton.Click += new System.EventHandler(this.skipButton_Click); // // s6Button // this.s6Button.BackColor = System.Drawing.SystemColors.ControlDark; this.s6Button.BtnSelect = false; this.s6Button.BtnText = "6"; this.s6Button.Location = new System.Drawing.Point(110, 50); this.s6Button.Name = "s6Button"; this.s6Button.Size = new System.Drawing.Size(31, 21); this.s6Button.TabIndex = 20; this.s6Button.Click += new System.EventHandler(this.skipButton_Click); // // s9Button // this.s9Button.BackColor = System.Drawing.SystemColors.ControlDark; this.s9Button.BtnSelect = false; this.s9Button.BtnText = "9"; this.s9Button.Location = new System.Drawing.Point(166, 50); this.s9Button.Name = "s9Button"; this.s9Button.Size = new System.Drawing.Size(31, 21); this.s9Button.TabIndex = 30; this.s9Button.Click += new System.EventHandler(this.skipButton_Click); // // s12Button // this.s12Button.BackColor = System.Drawing.SystemColors.ControlDark; this.s12Button.BtnSelect = false; this.s12Button.BtnText = "12"; this.s12Button.Location = new System.Drawing.Point(222, 50); this.s12Button.Name = "s12Button"; this.s12Button.Size = new System.Drawing.Size(31, 21); this.s12Button.TabIndex = 31; this.s12Button.Click += new System.EventHandler(this.skipButton_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(54, 18); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(199, 21); this.textBox1.TabIndex = 4; // // button3 // this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.button3.Location = new System.Drawing.Point(210, 108); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(65, 23); this.button3.TabIndex = 5; this.button3.Text = "应用"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.button4.Location = new System.Drawing.Point(70, 108); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(65, 23); this.button4.TabIndex = 6; this.button4.Text = "确定"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); // // button5 // this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.button5.Location = new System.Drawing.Point(140, 108); this.button5.Name = "button5"; this.button5.Size = new System.Drawing.Size(65, 23); this.button5.TabIndex = 7; this.button5.Text = "撤销"; this.button5.UseVisualStyleBackColor = true; this.button5.Click += new System.EventHandler(this.button5_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 18); this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(82, 15); this.label1.TabIndex = 9; // // checkBox1 // this.checkBox1.Anchor = System.Windows.Forms.AnchorStyles.Left; this.checkBox1.AutoSize = true; this.checkBox1.Checked = true; this.checkBox1.Location = new System.Drawing.Point(84, 17); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(72, 16); this.checkBox1.TabIndex = 5; this.checkBox1.TabStop = true; this.checkBox1.UseVisualStyleBackColor = true; // // checkBox2 // this.checkBox2.Anchor = System.Windows.Forms.AnchorStyles.Left; this.checkBox2.AutoSize = true; this.checkBox2.Location = new System.Drawing.Point(180, 17); this.checkBox2.Name = "checkBox2"; this.checkBox2.Size = new System.Drawing.Size(72, 16); this.checkBox2.TabIndex = 16; this.checkBox2.UseVisualStyleBackColor = true; // // checkBox3 // this.checkBox3.Anchor = System.Windows.Forms.AnchorStyles.Left; this.checkBox3.AutoSize = true; this.checkBox3.Location = new System.Drawing.Point(84, 51); this.checkBox3.Name = "checkBox3"; this.checkBox3.Size = new System.Drawing.Size(72, 16); this.checkBox3.TabIndex = 17; this.checkBox3.UseVisualStyleBackColor = true; // // checkBox4 // this.checkBox4.Anchor = System.Windows.Forms.AnchorStyles.Left; this.checkBox4.AutoSize = true; this.checkBox4.Location = new System.Drawing.Point(180, 51); this.checkBox4.Name = "checkBox4"; this.checkBox4.Size = new System.Drawing.Size(72, 16); this.checkBox4.TabIndex = 18; this.checkBox4.UseVisualStyleBackColor = true; // // ScratchTreatmentDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(295, 144); this.Controls.Add(this.groupBox2); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Location = new System.Drawing.Point(0, 0); this.Margin = new System.Windows.Forms.Padding(6); this.Name = "ScratchTreatmentDialog"; this.Text = "划痕处理"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ScratchTreatmentDialog_FormClosing); this.Controls.SetChildIndex(this.button3, 0); this.Controls.SetChildIndex(this.button4, 0); this.Controls.SetChildIndex(this.button5, 0); this.Controls.SetChildIndex(this.groupBox2, 0); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); this.ResumeLayout(false); } #endregion public ScratchTreatmentDialog(AppWorkspace AppWorkspace) { this.AppWorkspace = AppWorkspace; this.ShowInTaskbar = false; InitializeComponent(); InitializeLanguageText(); this.Text = PdnResources.GetString("Menu.Tools.ScratchTreatment.Text");/*;//待修改中文资源-scc备注 20200709*/ action = new Data.Action.Action1214(); this.AppWorkspace.ActiveDocumentWorkspaceChanging += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanging); this.AppWorkspace.ActiveDocumentWorkspaceChanged += new EventHandler(this.ActiveDocumentWorkspaceChanged); this.VisibleChanged += new EventHandler(this.ScratchTreatmentDialog_FormClosed); } private void ScratchTreatmentDialog_FormClosed(object sender, EventArgs/*FormClosedEventArgs*/ e) { if (this.Visible) return; if (AppWorkspace.ActiveDocumentWorkspace != null) { AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawScratchTreatmentLine); if (AppWorkspace.ActiveDocumentWorkspace.AppWorkspace != null) AppWorkspace.ActiveDocumentWorkspace.AppWorkspace.RefreshLabelListDialog(); AppWorkspace.ActiveDocumentWorkspace.Refresh(); } if (AppWorkspace.ActiveDocumentWorkspace != null) { AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.Pointer; } } private void ActiveDocumentWorkspaceChanged(object sender, EventArgs e) { if (!this.Visible) return; if (this.AppWorkspace.ActiveDocumentWorkspace != null) { AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawScratchTreatmentLine; } //this.PhaseModelsBackUp = this.AppWorkspace.ActiveDocumentWorkspace.PhaseModelsForCopy; } /// /// 图片切换的处理方法 //#20193 /// /// /// private void AppWorkspace_ActiveDocumentWorkspaceChanging(object sender, EventArgs e) { if (!this.Visible) return; if (oldmat != null) { if (MessageBox.Show("是否保存当前图片的处理结果" + "?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (oldmat != null) { ////Document doc = Document.FromMat(oldmat); ////AppWorkspace.ActiveDocumentWorkspace.Document = doc; //AppWorkspace.ActiveDocumentWorkspace.Refresh(); oldmat.Dispose(); oldmat = null; } } else { Document doc = Document.FromMat(oldmat.Clone()); AppWorkspace.ActiveDocumentWorkspace.Document = doc; AppWorkspace.ActiveDocumentWorkspace/*this.documentWorkspace*/.Refresh(); oldmat.Dispose(); oldmat = null; } } if (AppWorkspace.ActiveDocumentWorkspace != null) { AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawScratchTreatmentLine); if (AppWorkspace.ActiveDocumentWorkspace.AppWorkspace != null) AppWorkspace.ActiveDocumentWorkspace.AppWorkspace.RefreshLabelListDialog(); AppWorkspace.ActiveDocumentWorkspace.Refresh(); } if (AppWorkspace.ActiveDocumentWorkspace != null) { AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.Pointer; } //this.Close(); } /// /// 应用按钮点击方法 /// /// /// private void button3_Click(object sender, EventArgs e) { if (oldmat != null) { AppWorkspace.ActiveDocumentWorkspace.Refresh(); oldmat.Dispose(); oldmat = null; } this.Close(); //if (this.AppWorkspace.ActiveDocumentWorkspace == null) //{ // MessageBox.Show("请打开图片"); // return; //} //PaintDotNet.Annotation.ISurfaceBox drawArea = AppWorkspace.ActiveDocumentWorkspace; //if (drawArea != null && drawArea.GraphicsList != null && drawArea.GraphicsList.Count > 0) //{ // for (int i = drawArea.GraphicsList.Count - 1; i >= 0; i--) // { // if (drawArea.GraphicsList[i].drawToolType == DrawToolType.DrawScratchTreatmentLine/*drawArea.GraphicsList[i].Selected == true*/) // { // if (applyImp(drawArea.GraphicsList[i].startPoint, drawArea.GraphicsList[i].endPoint)) // { // this.Close(); // } // break; // } // } //} } /// /// 应用方法实现 /// /// /// private bool applyImp(PointF startP, PointF endP, bool undoAction, int paramWidth) { if (action.MenuId == ActionType.ScratchTreatment)//划痕处理 { if (oldmat == null) oldmat = AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone(); //痕宽 if (undoAction || int.TryParse(textBox1.Text, out paramWidth)) { if (paramWidth < 1) { MessageBox.Show(PdnResources.GetString("Menu.Pleaseinpuanumbergreaterthan0.Text")); return false; } } else { MessageBox.Show(PdnResources.GetString("Menu.Pleaseinpuanumbergreaterthan0.Text")); return false; } if (undoAction) { OperationsList.RemoveAt(OperationsList.Count - 1); this.button5.Enabled = OperationsList.Count > 0; OpenCvSharp.Mat matUndo = new Mat(); for (int i = 0; i < OperationsList.Count; i++) { int[] operationItem = OperationsList[i]; Mat imageMaskUndo = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0)); try { Cv2.Line(imageMaskUndo, operationItem[1], operationItem[2], operationItem[3], operationItem[4], new Scalar(255), operationItem[0]); matUndo = action.PerformMaskCal((i == 0) ? oldmat : matUndo, imageMaskUndo); } catch (Exception) { matUndo = oldmat.Clone(); } finally { //if (mat != null) mat.Dispose(); if (imageMaskUndo != null) imageMaskUndo.Dispose(); GC.Collect(); } } if (OperationsList.Count == 0) matUndo = oldmat.Clone(); //Cv2.ImShow("test", mat); Document doc = Document.FromMat(matUndo); AppWorkspace.ActiveDocumentWorkspace.Document = doc; //移除 AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawScratchTreatmentLine); //刷新 AppWorkspace.ActiveDocumentWorkspace.Refresh(); AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawScratchTreatmentLine; return true; } else OperationsList.Add(new int[] { paramWidth, (int)startP.X, (int)startP.Y, (int)endP.X, (int)endP.Y }); this.button5.Enabled = OperationsList.Count > 0; OpenCvSharp.Mat mat = new Mat(); Mat imageMask = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0)); try { ////通过阈值处理生成Mask //Cv2.Threshold(imageGray, imageMask, 120, 255, ThresholdTypes.Binary); Cv2.Line(imageMask, (int)startP.X, (int)startP.Y, (int)endP.X, (int)endP.Y, new Scalar(255), paramWidth); Mat calmat = undoAction ? oldmat : AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone(); mat = action.PerformMaskCal(calmat, imageMask); //Cv2.ImShow("test", mat); Document doc = Document.FromMat(mat); AppWorkspace.ActiveDocumentWorkspace.Document = doc; //移除 AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawScratchTreatmentLine); //刷新 AppWorkspace.ActiveDocumentWorkspace.Refresh(); AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawScratchTreatmentLine; } catch (Exception) { } finally { //if (mat != null) mat.Dispose(); if (imageMask != null) imageMask.Dispose(); GC.Collect(); } return true; } return false; } /// /// 确定按钮点击方法 /// /// /// private void button4_Click(object sender, EventArgs e) { ////测试景深融合程序 //List ImagePaths = new List(); //foreach (string Path in System.IO.Directory.GetFiles(@"C:\Users\win10SSD\Desktop\工作目录\显微镜\最新版景深融合翻译\11\")) //{ // ImagePaths.Add(Path); //} //// //Mat[] Coll_Ori = new Mat[ImagePaths.Count]; //// //for (int index = 0; index < ImagePaths.Count; index++) //{ // Mat Src = Cv2.ImRead(ImagePaths[index]); // //原始图 // Coll_Ori[index] = Src; //} //Mat mat = Merge.GetMergeMat(Coll_Ori); //Document doc = Document.FromMat(mat); //AppWorkspace.ActiveDocumentWorkspace.Document = doc; //AppWorkspace.ActiveDocumentWorkspace/*this.documentWorkspace*/.Refresh(); //foreach (Mat src1 in Coll_Ori) //{ // src1.Dispose(); //} //mat.Dispose(); //GC.Collect(); //return; if (this.AppWorkspace.ActiveDocumentWorkspace == null) { MessageBox.Show(PdnResources.GetString("Menu.Pleaseopenthepicture.Text")); return; } //痕宽 int paramWidth; if (int.TryParse(textBox1.Text, out paramWidth)) { if (paramWidth < 1) { MessageBox.Show(PdnResources.GetString("Menu.Pleaseinpuanumbergreaterthan0.Text")); return; } } else { MessageBox.Show(PdnResources.GetString("Menu.Pleaseinpuanumbergreaterthan0.Text")); return; } //读取参数信息 for (int i = 0; i < action.lists.Count; i++) { Base.Args args = action.lists[i]; switch (args.Key) { case "InpaintRadius": args.Value = paramWidth/*inpaintRadius*/ + ""; break; } } PaintDotNet.Annotation.ISurfaceBox drawArea = AppWorkspace.ActiveDocumentWorkspace; if (drawArea != null && drawArea.GraphicsList != null && drawArea.GraphicsList.Count > 0) { for (int i = drawArea.GraphicsList.Count - 1; i >= 0; i--) { if (drawArea.GraphicsList[i].drawToolType == DrawToolType.DrawScratchTreatmentLine/*drawArea.GraphicsList[i].Selected == true*/) { applyImp(drawArea.GraphicsList[i].startPoint, drawArea.GraphicsList[i].endPoint, false, -1); break; } } } } protected override void WndProc(ref Message m) { if (m.Msg == 0x0112/*WM_SYSCOMMAND*/) { //if (m.WParam.ToInt32() == SC_MINIMIZE) //是否点击最小化 //{ // //这里写操作代码 // this.Visible = false; //隐藏窗体 // return; //} //if (m.WParam.ToInt32() == SC_MAXIMIZE) //是否点击最大化 //{ // //..................... //} if (m.WParam.ToInt32() == 0xf060/*NativeConstants.SC_CLOSE*/) //是否点击关闭 { //关闭时发生的事件 if (oldmat != null) { Document doc = Document.FromMat(oldmat.Clone()); AppWorkspace.ActiveDocumentWorkspace.Document = doc; AppWorkspace.ActiveDocumentWorkspace/*this.documentWorkspace*/.Refresh(); oldmat.Dispose(); oldmat = null; } } } base.WndProc(ref m); //if (this.formEx == null) //{ // base.WndProc(ref m); //} //else if (!this.formEx.HandleParentWndProc(ref m)) //{ // OurWndProc(ref m); //} } /// /// 撤销按钮点击方法 /// /// /// private void button5_Click(object sender, EventArgs e) { if (oldmat != null && OperationsList.Count > 0) { int[] operationItem = OperationsList[OperationsList.Count - 1]; applyImp(new PointF(operationItem[1], operationItem[2])/*drawArea.GraphicsList[i].startPoint*/, new PointF(operationItem[3], operationItem[4])/*drawArea.GraphicsList[i].endPoint*/, true, operationItem[0]); //Document doc = Document.FromMat(oldmat); //AppWorkspace.ActiveDocumentWorkspace.Document = doc; //AppWorkspace.ActiveDocumentWorkspace.Refresh(); //oldmat.Dispose(); //oldmat = null; } else this.button5.Enabled = false; //this.Close(); } /// /// skip按钮 /// 显示直方图时,忽略黑色的灰度或颜色值。 /// 这使您可以为背景为黑色的图像实现有意义的直方图显示。 /// /// /// private void skipButton_Click(object sender, EventArgs e) { //if (this.AppWorkspace.ActiveDocumentWorkspace == null) //{ // MessageBox.Show("请打开图片"); // return; //} foreach (SelectButton selectBtn in this.listButton) { if (selectBtn == sender) { //设置按钮的选中/非选择的状态 s6Button.BtnSelect = false;// true; } else { s6Button.BtnSelect = false; } this.textBox1.Text = ((SelectButton)sender).BtnText; } ////设置按钮的选中/非选择的状态 //s6Button.BtnSelect = !s6Button.BtnSelect; //this.AppWorkspace.ActiveDocumentWorkspace.HistogramSkipEnabled = s6Button.BtnSelect; } private void ScratchTreatmentDialog_FormClosing(object sender, FormClosingEventArgs e) { this.AppWorkspace.toolBar.RefreshBtnSelect(false, "ScratchTreatment"); this.AppWorkspace.toolsPanel.RefreshBtnSelect(false, "ScratchTreatment"); } } }