using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using OpenCvSharp; using OpenCvSharp.Extensions; using PaintDotNet.Adjust; using PaintDotNet.Adjust.BaseImage; using PaintDotNet.Annotation.Measure; using PaintDotNet.Base.SettingModel; using PaintDotNet.Camera; namespace PaintDotNet.ImageCollect { internal class CameraRealTimeStitchingDialog : FloatingToolForm { #region /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.btnCreatImgFile.Text = PdnResources.GetString("Menu.Createpicture.text"); this.btnWorking.Text = PdnResources.GetString("Menu.Started.text"); this.groupBox3.Text = PdnResources.GetString("Menu.Preview.text"); this.groupBox4.Text = PdnResources.GetString("Menu.Puzzleeffect.text"); this.Text = PdnResources.GetString("Menu.imagecapture.Reatimldexpansion.Reaestitching.text"); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.btnCreatImgFile = new System.Windows.Forms.Button(); this.btnWorking = new System.Windows.Forms.Button(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.picPreview = new System.Windows.Forms.PictureBox(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.picMatch = new DocumentStitchWindow();// System.Windows.Forms.PictureBox(); this.groupBox1.SuspendLayout(); this.groupBox3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.picPreview)).BeginInit(); this.groupBox4.SuspendLayout(); //((System.ComponentModel.ISupportInitialize)(this.picMatch)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox1.Controls.Add(this.btnCreatImgFile); this.groupBox1.Controls.Add(this.btnWorking); this.groupBox1.Location = new System.Drawing.Point(12, 12); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(1217, 87); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "操作"; // // btnCreatImgFile // this.btnCreatImgFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnCreatImgFile.Location = new System.Drawing.Point(1106, 28); this.btnCreatImgFile.Name = "btnCreatImgFile"; this.btnCreatImgFile.Size = new System.Drawing.Size(92, 34); this.btnCreatImgFile.TabIndex = 1; this.btnCreatImgFile.Text = "创建图片"; this.btnCreatImgFile.UseVisualStyleBackColor = true; this.btnCreatImgFile.Click += new System.EventHandler(this.btnCreatImgFile_Click); // // btnWorking // this.btnWorking.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnWorking.Location = new System.Drawing.Point(993, 28); this.btnWorking.Name = "btnWorking"; this.btnWorking.Size = new System.Drawing.Size(89, 34); this.btnWorking.TabIndex = 0; this.btnWorking.Text = "开始"; this.btnWorking.UseVisualStyleBackColor = true; this.btnWorking.Click += new System.EventHandler(this.btnWorking_Click); // // groupBox3 // this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); this.groupBox3.Controls.Add(this.picPreview); this.groupBox3.Location = new System.Drawing.Point(12, 105); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(466, 517); this.groupBox3.TabIndex = 3; this.groupBox3.TabStop = false; this.groupBox3.Text = "预览"; // // picPreview // this.picPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.picPreview.Location = new System.Drawing.Point(20, 74); this.picPreview.Name = "picPreview"; this.picPreview.Size = new System.Drawing.Size(429, 364); this.picPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.picPreview.TabIndex = 0; this.picPreview.TabStop = false; // // groupBox4 // this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.groupBox4.Controls.Add(this.picMatch); this.groupBox4.Location = new System.Drawing.Point(484, 105); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new System.Drawing.Size(745, 517); this.groupBox4.TabIndex = 4; this.groupBox4.TabStop = false; this.groupBox4.Text = "拼图效果"; // // picMatch // this.picMatch.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.picMatch.Location = new System.Drawing.Point(17, 74); this.picMatch.Name = "picMatch"; this.picMatch.Size = new System.Drawing.Size(709, 416); //this.picMatch.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.picMatch.TabIndex = 1; this.picMatch.TabStop = false; // // CameraRealTimeStitchingDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1241, 634); this.Controls.Add(this.groupBox4); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox1); this.Name = "CameraRealTimeStitchingDialog"; this.Text = "图像实时拼接"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraRealTimeStitchingDialog_FormClosing); this.Load += new System.EventHandler(this.CameraRealTimeStitchingDialog_Load); this.Controls.SetChildIndex(this.groupBox1, 0); this.Controls.SetChildIndex(this.groupBox3, 0); this.Controls.SetChildIndex(this.groupBox4, 0); this.groupBox1.ResumeLayout(false); this.groupBox3.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit(); this.groupBox4.ResumeLayout(false); //((System.ComponentModel.ISupportInitialize)(this.picMatch)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btnCreatImgFile; private System.Windows.Forms.Button btnWorking; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.PictureBox picPreview; private System.Windows.Forms.GroupBox groupBox4; private /*System.Windows.Forms.PictureBox*/DocumentStitchWindow picMatch; #endregion private AppWorkspace m_appWorkspace; private ICamera m_camera; private CameraParamModel m_cameraParamModel; private Mat m_mat; private Thread m_stitchingThread; private bool is_depth; private Mat[] Coll_Ori = new Mat[2]; private int newOriX = 10000; private int newOriY = 10000; private Mat m_merge; private string m_filePath = Application.StartupPath + "\\Picture\\"; private bool m_isWorking; private bool _isWorkingDone; public CameraRealTimeStitchingDialog(AppWorkspace appWorkspace) { m_appWorkspace = appWorkspace; InitializeComponent(); InitializeLanguageText(); } private void CameraRealTimeStitchingDialog_Load(object sender, EventArgs e) { m_camera = CameraManager.CurrentCamera; m_cameraParamModel = Startup.instance.cameraParamModel; CameraConfigs cameraConfig = CameraConfigs.GetInstance(); cameraConfig.CameraParamInit(); CameraManager.FrameCallback += CallbackDraw; } private byte[] m_pbuf; /// /// 绘制 /// /// /// public void CallbackDraw(Bitmap bitmap) { if (bitmap == null) return; m_mat = Tools.ToMat(bitmap); picPreview.Image = (Image)bitmap.Clone(); } private void btnWorking_Click(object sender, EventArgs e) { if (m_isWorking) { m_isWorking = false; m_stitchingThread.Abort(); m_stitchingThread.DisableComObjectEagerCleanup(); btnWorking.Text = PdnResources.GetString("Menu.Started.text"); new Task(() => { Thread.Sleep(500); this.Invoke(new Action(() => { btnWorking.Text = PdnResources.GetString("Menu.Started.text"); })); }).Start(); return; } if (m_merge != null) { newOriX = 10000; newOriY = 10000; picMatch.StitchBounds = new Rectangle(newOriX, newOriY, 0, 0); picMatch.StitchDocument = null; picMatch.Refresh(); m_merge.Dispose(); m_merge = null; this.picMatch.Visible = false; this.groupBox4.Controls.Remove(this.picMatch); this.picMatch = new DocumentStitchWindow(); this.groupBox4.Controls.Add(this.picMatch); // picMatch this.picMatch.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.picMatch.Location = new System.Drawing.Point(17, 74); this.picMatch.Name = "picMatch"; this.picMatch.Size = new System.Drawing.Size(709, 416); this.picMatch.TabIndex = 1; this.picMatch.TabStop = false; } m_isWorking = true; btnWorking.Text = PdnResources.GetString("Menu.stop.text"); lastRect = new Rect(-10000, -10000, 0, 0); m_merge = null; m_stitchingThread = new Thread(new ThreadStart(WaitForStitchingThreadEntry)); m_stitchingThread.Start(); } private Rect lastRect = new Rect(-10000, -10000, 0, 0); /// /// 图像拼接 /// private void WaitForStitchingThreadEntry() { while (m_isWorking) { if (m_mat == null) { Thread.Sleep(100); continue; } Mat mat = m_mat;//.Clone(); if (m_merge == null) { m_merge = new Mat(20000 + newOriY, 20000 + newOriX, MatType.CV_8UC3); picMatch.StitchDocument = StitchDocument.FromMat(m_merge/*tmp*/); picMatch.StitchBounds = new Rectangle(newOriX, newOriY, 0, 0); picMatch.StitchDocument.surface.stitchBackColor = picMatch.BackColor; picMatch.ActiveTool = Annotation.Enum.DrawToolType.MoveMode; AdjustIntent.InitMatchPicOneByOne(true, newOriX, newOriY, m_merge); } Rect rect; var result = AdjustIntent.MatchPicRealtime(mat, m_merge, out bool error, out rect); if (!error) { if (result != null) m_merge = result; } else { Console.WriteLine("拼图失败"); } picMatch.GraphicsList.Clear(); MeasureRectangle rectObject = new MeasureRectangle(picMatch, rect.X, rect.Y, rect.Width, rect.Height); picMatch.GraphicsList.Add(rectObject); //var tmp = m_merge.Clone(); //Cv2.Rectangle(tmp, rect, Scalar.Lime, 5, LineTypes.Link4, 4); ////picMatch.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(tmp); ////picMatch.Invalidate(); int disX = Math.Abs(rect.X - lastRect.X); int disY = Math.Abs(rect.Y - lastRect.Y); lastRect = rect; if (picMatch.StitchBounds.Width == 0) { picMatch.StitchDocument.surface.stitchBounds.Width = rect.Width;// matNew.Width; picMatch.StitchDocument.surface.stitchBounds.Height = rect.Height;// matNew.Height; } else { int x = Math.Min(picMatch.StitchBounds.X, rect.X); int y = Math.Min(picMatch.StitchBounds.Y, rect.Y); int x2 = Math.Max(picMatch.StitchBounds.Right, rect.Right); int y2 = Math.Max(picMatch.StitchBounds.Bottom, rect.Bottom); picMatch.StitchBounds = new Rectangle(x, y, x2 - x, y2 - y); //picMatch.StitchDocument.surface.stitchBounds.Width += stitchStep; //picMatch.StitchDocument.surface.stitchBounds.Height += stitchStep; } if (!m_isWorking) return; this.Invoke(new Action(() => { //PointF centerPt = picMatch.DocumentCenterPointF; ////PointF centerPt = new PointF(picMatch.picRectWidth/*(newOriX + matNew.Width)*/ * 0.5f, picMatch.picRectHeight/*(newOriY + matNew.Height)*/ * 0.5f);// picMatch.DocumentCenterPointF; if (disX > 500 || disY > 500) { picMatch.ZoomBasis = ZoomBasis.ScaleFactor; picMatch.ZoomBasis = ZoomBasis.FitToWindow; picMatch.ZoomBasis = ZoomBasis.ScaleFactor; picMatch.panel.HorizontalScroll.Value = (int)(picMatch.panel.HorizontalScroll.Maximum * picMatch.StitchBounds.X / (picMatch.StitchWidth));// (int)(picMatch.ScaleFactor.Ratio * 440);// (int)(picMatch.panel.HorizontalScroll.LargeChange * 0.5);// picMatch.ScaleFactor.Ratio * 225.0 / (picMatch.picRectWidth/*20006*/ - 0/*picMatch.picRectWidth*/)); picMatch.panel.VerticalScroll.Value = (int)(picMatch.panel.VerticalScroll.Maximum * picMatch.StitchBounds.Y / picMatch.StitchHeight);// (int)(picMatch.ScaleFactor.Ratio * 150);// (int)(picMatch.panel.VerticalScroll.LargeChange * 0.5);// * picMatch.ScaleFactor.Ratio * 150.0 / (picMatch.picRectHeight/*20006*/ - 0/*picMatch.picRectHeight*/)); } picMatch.Refresh(); })); GC.Collect(); } } private void btnCreatImgFile_Click(object sender, EventArgs e) { if (picMatch.StitchMat/*m_merge*/ == null) { MessageBox.Show(PdnResources.GetString("Menu.mageMosaicimagehanotbeengeneratedyet.Text")); return; } //Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_merge); DocumentWorkspace dw = m_appWorkspace.AddNewDocumentWorkspace(); Document document = Document.FromMat(picMatch.StitchMat/*m_merge*/.Clone())/*FromImage(bitmap)*/; dw.Document = document; dw.xmlSaveModel = Startup.instance.ruleDB; dw.InitRulerInfo(); dw.fileText = "1"; dw.fileText = string.Format("{0}-{1:D3}", "实时拼图", Startup.instance.step_length++); m_appWorkspace.ActiveDocumentWorkspace = dw; } private void CameraRealTimeStitchingDialog_FormClosing(object sender, FormClosingEventArgs e) { if (m_stitchingThread != null) { m_isWorking = false; //m_stitchingThread.Join(); //m_stitchingThread.Abort(); //m_stitchingThread.DisableComObjectEagerCleanup(); } CameraManager.FrameCallback -= CallbackDraw; if (m_mat != null && !m_mat.IsDisposed) { m_mat.Dispose(); } if (m_merge != null && !m_merge.IsDisposed) { m_merge.Dispose(); } GC.Collect(); } } }