123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562 |
- using System;
- using System.Drawing;
- using System.Threading;
- using System.Windows.Forms;
- using OpenCvSharp;
- using PaintDotNet.Camera;
- using StageController;
- namespace PaintDotNet.ImageCollect.CameraEDOF
- {
- /// <summary>
- /// 自动景深扩展
- /// </summary>
- internal class CameraAutomaticDepth : FloatingToolForm
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- 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.groupBox2.Text = PdnResources.GetString("Menu.paramssettings.text");
- this.rdoMod2.Text = PdnResources.GetString("Menu.Model2.Text");
- this.rdoMod1.Text = PdnResources.GetString("Menu.imagecapture.Autoeldexpansion.patternone.text");
- this.groupBox4.Text = PdnResources.GetString("Menu.ImageCollection.AutomaticDepth.Text");
- this.label1.Text = PdnResources.GetString("Menu.imagecapture.Autoeldexpansion.Shootingmode.text") + ":";
- this.Text = PdnResources.GetString("Menu.ImageCollection.AutomaticDepth.Text");
- this.groupBox3.Text = PdnResources.GetString("Menu.Preview.text");
- }
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.btnZClear = new System.Windows.Forms.Button();
- this.btnCreatImgFile = new System.Windows.Forms.Button();
- this.btnWorking = new System.Windows.Forms.Button();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.controlPanel = new System.Windows.Forms.Panel();
- this.rdoMod2 = new System.Windows.Forms.RadioButton();
- this.rdoMod1 = new System.Windows.Forms.RadioButton();
- this.label1 = new System.Windows.Forms.Label();
- this.splitContainer1 = new System.Windows.Forms.SplitContainer();
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.picPreview = new System.Windows.Forms.PictureBox();
- this.groupBox4 = new System.Windows.Forms.GroupBox();
- this.picDepth = new System.Windows.Forms.PictureBox();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
- this.splitContainer1.Panel1.SuspendLayout();
- this.splitContainer1.Panel2.SuspendLayout();
- this.splitContainer1.SuspendLayout();
- this.groupBox3.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.picPreview)).BeginInit();
- this.groupBox4.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.picDepth)).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.btnZClear);
- this.groupBox1.Controls.Add(this.btnCreatImgFile);
- this.groupBox1.Controls.Add(this.btnWorking);
- this.groupBox1.Location = new System.Drawing.Point(9, 10);
- this.groupBox1.Margin = new System.Windows.Forms.Padding(2);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Padding = new System.Windows.Forms.Padding(2);
- this.groupBox1.Size = new System.Drawing.Size(935, 70);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "操作";
- //
- // btnZClear
- //
- this.btnZClear.Location = new System.Drawing.Point(661, 24);
- this.btnZClear.Name = "btnZClear";
- this.btnZClear.Size = new System.Drawing.Size(75, 23);
- this.btnZClear.TabIndex = 2;
- this.btnZClear.Text = "Z Clear";
- this.btnZClear.UseVisualStyleBackColor = true;
- this.btnZClear.Click += new System.EventHandler(this.btnZClear_Click);
- //
- // 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(852, 22);
- this.btnCreatImgFile.Margin = new System.Windows.Forms.Padding(2);
- this.btnCreatImgFile.Name = "btnCreatImgFile";
- this.btnCreatImgFile.Size = new System.Drawing.Size(69, 27);
- 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(758, 22);
- this.btnWorking.Margin = new System.Windows.Forms.Padding(2);
- this.btnWorking.Name = "btnWorking";
- this.btnWorking.Size = new System.Drawing.Size(67, 27);
- this.btnWorking.TabIndex = 0;
- this.btnWorking.Text = "开始";
- this.btnWorking.UseVisualStyleBackColor = true;
- this.btnWorking.Click += new System.EventHandler(this.btnWorking_Click);
- //
- // groupBox2
- //
- this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)));
- this.groupBox2.Controls.Add(this.controlPanel);
- this.groupBox2.Controls.Add(this.rdoMod2);
- this.groupBox2.Controls.Add(this.rdoMod1);
- this.groupBox2.Controls.Add(this.label1);
- this.groupBox2.Location = new System.Drawing.Point(9, 94);
- this.groupBox2.Margin = new System.Windows.Forms.Padding(2);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Padding = new System.Windows.Forms.Padding(2);
- this.groupBox2.Size = new System.Drawing.Size(295, 420);
- this.groupBox2.TabIndex = 1;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "参数设置";
- //
- // controlPanel
- //
- this.controlPanel.Location = new System.Drawing.Point(4, 59);
- this.controlPanel.Margin = new System.Windows.Forms.Padding(2);
- this.controlPanel.Name = "controlPanel";
- this.controlPanel.Size = new System.Drawing.Size(281, 350);
- this.controlPanel.TabIndex = 3;
- //
- // rdoMod2
- //
- this.rdoMod2.AutoSize = true;
- this.rdoMod2.Location = new System.Drawing.Point(164, 30);
- this.rdoMod2.Margin = new System.Windows.Forms.Padding(2);
- this.rdoMod2.Name = "rdoMod2";
- this.rdoMod2.Size = new System.Drawing.Size(14, 13);
- this.rdoMod2.TabIndex = 2;
- this.rdoMod2.UseVisualStyleBackColor = true;
- this.rdoMod2.Visible = false;
- this.rdoMod2.CheckedChanged += new System.EventHandler(this.rdoMod2_CheckedChanged);
- //
- // rdoMod1
- //
- this.rdoMod1.AutoSize = true;
- this.rdoMod1.Checked = true;
- this.rdoMod1.Location = new System.Drawing.Point(90, 29);
- this.rdoMod1.Margin = new System.Windows.Forms.Padding(2);
- this.rdoMod1.Name = "rdoMod1";
- this.rdoMod1.Size = new System.Drawing.Size(14, 13);
- this.rdoMod1.TabIndex = 1;
- this.rdoMod1.TabStop = true;
- this.rdoMod1.UseVisualStyleBackColor = true;
- this.rdoMod1.CheckedChanged += new System.EventHandler(this.rdoMod1_CheckedChanged);
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(16, 30);
- this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(0, 12);
- this.label1.TabIndex = 0;
- //
- // splitContainer1
- //
- this.splitContainer1.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.splitContainer1.Location = new System.Drawing.Point(309, 91);
- this.splitContainer1.Margin = new System.Windows.Forms.Padding(2);
- this.splitContainer1.Name = "splitContainer1";
- //
- // splitContainer1.Panel1
- //
- this.splitContainer1.Panel1.Controls.Add(this.groupBox3);
- //
- // splitContainer1.Panel2
- //
- this.splitContainer1.Panel2.Controls.Add(this.groupBox4);
- this.splitContainer1.Size = new System.Drawing.Size(636, 427);
- this.splitContainer1.SplitterDistance = 279;
- this.splitContainer1.SplitterWidth = 3;
- this.splitContainer1.TabIndex = 4;
- //
- // groupBox3
- //
- this.groupBox3.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.groupBox3.Controls.Add(this.picPreview);
- this.groupBox3.Location = new System.Drawing.Point(2, 2);
- this.groupBox3.Margin = new System.Windows.Forms.Padding(2);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Padding = new System.Windows.Forms.Padding(2);
- this.groupBox3.Size = new System.Drawing.Size(274, 422);
- 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.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.picPreview.Location = new System.Drawing.Point(15, 59);
- this.picPreview.Margin = new System.Windows.Forms.Padding(2);
- this.picPreview.Name = "picPreview";
- this.picPreview.Size = new System.Drawing.Size(247, 338);
- 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.picDepth);
- this.groupBox4.Location = new System.Drawing.Point(2, 2);
- this.groupBox4.Margin = new System.Windows.Forms.Padding(2);
- this.groupBox4.Name = "groupBox4";
- this.groupBox4.Padding = new System.Windows.Forms.Padding(2);
- this.groupBox4.Size = new System.Drawing.Size(355, 422);
- this.groupBox4.TabIndex = 4;
- this.groupBox4.TabStop = false;
- this.groupBox4.Text = "自动景深扩展";
- //
- // picDepth
- //
- this.picDepth.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.picDepth.Location = new System.Drawing.Point(13, 59);
- this.picDepth.Margin = new System.Windows.Forms.Padding(2);
- this.picDepth.Name = "picDepth";
- this.picDepth.Size = new System.Drawing.Size(327, 338);
- this.picDepth.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
- this.picDepth.TabIndex = 1;
- this.picDepth.TabStop = false;
- //
- // CameraAutomaticDepth
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(953, 523);
- this.Controls.Add(this.splitContainer1);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox1);
- this.Margin = new System.Windows.Forms.Padding(2);
- this.Name = "CameraAutomaticDepth";
- this.Text = "自动景深扩展";
- this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraAutomaticDepth_FormClosing);
- this.Load += new System.EventHandler(this.CameraAutomaticDepth_Load);
- this.Controls.SetChildIndex(this.groupBox1, 0);
- this.Controls.SetChildIndex(this.groupBox2, 0);
- this.Controls.SetChildIndex(this.splitContainer1, 0);
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.groupBox2.PerformLayout();
- this.splitContainer1.Panel1.ResumeLayout(false);
- this.splitContainer1.Panel2.ResumeLayout(false);
- ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
- this.splitContainer1.ResumeLayout(false);
- this.groupBox3.ResumeLayout(false);
- ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit();
- this.groupBox4.ResumeLayout(false);
- ((System.ComponentModel.ISupportInitialize)(this.picDepth)).EndInit();
- this.ResumeLayout(false);
- }
- #endregion
- #region 控件
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.Button btnCreatImgFile;
- private System.Windows.Forms.Button btnWorking;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.RadioButton rdoMod2;
- private System.Windows.Forms.RadioButton rdoMod1;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Panel controlPanel;
- private System.Windows.Forms.SplitContainer splitContainer1;
- private System.Windows.Forms.GroupBox groupBox3;
- private System.Windows.Forms.PictureBox picPreview;
- private System.Windows.Forms.GroupBox groupBox4;
- private System.Windows.Forms.PictureBox picDepth;
- #endregion
- private AppWorkspace m_appWorkspace;
- // 已移动次数
- private int m_moveTimes;
- // 当前位置
- private double m_currentStep => m_Stage.Z;
- private ICamera m_camera => CameraManager.CurrentCamera;
- private AxisController m_Stage;
- private ParameterOneControl m_parameterOne;
- private bool m_isWorking;
- private Mat m_matClone;
- private Mat[] Coll_Ori = new Mat[2];
- private Mat m_merge;
- private Button btnZClear;
- public CameraAutomaticDepth(AppWorkspace appWorkspace)
- {
- this.m_appWorkspace = appWorkspace;
- // this.DoubleBuffered = true;//设置本窗体
- //SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
- //SetStyle(ControlStyles.UserPaint, true);
- //SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
- //SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
- InitializeComponent();
- InitializeLanguageText();
- }
- /// <summary>
- /// 加载数据
- /// </summary>
- public void InitializeData()
- {
- try
- {
- m_Stage = AxisController.GetInstance();
- CameraManager.FrameCallback += CallbackDraw;
- // 默认选中显示模式一
- rdoMod1.Checked = true;
- controlPanel.Controls.Clear();
- m_parameterOne = new ParameterOneControl(m_Stage);
- controlPanel.Controls.Add(m_parameterOne);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void CameraAutomaticDepth_Load(object sender, EventArgs e)
- {
- InitializeData();
- }
- object obj = new object();
- /// <summary>
- /// 绘制
- /// </summary>
- /// <param name="pBuf"></param>
- /// <param name="obj"></param>
- public void CallbackDraw(Bitmap frame)
- {
- if (frame == null)
- return;
- m_matClone = PaintDotNet.Camera.Tools.ToMat(frame);
- picPreview.Image = (Image)frame.Clone();
- }
- private void rdoMod1_CheckedChanged(object sender, EventArgs e)
- {
- controlPanel.Controls.Clear();
- ParameterOneControl parameterOne = new ParameterOneControl(m_Stage);
- controlPanel.Controls.Add(parameterOne);
- }
- private void rdoMod2_CheckedChanged(object sender, EventArgs e)
- {
- controlPanel.Controls.Clear();
- ParameterTwoControl parameterTwo = new ParameterTwoControl();
- controlPanel.Controls.Add(parameterTwo);
- }
- private void CameraAutomaticDepth_FormClosing(object sender, FormClosingEventArgs e)
- {
- CameraManager.FrameCallback -= CallbackDraw;
- }
- private void btnWorking_Click(object sender, EventArgs e)
- {
- m_merge = null;
- try
- {
- if (!m_camera.IsOpen())
- {
- MessageBox.Show(PdnResources.GetString("Menu.merafoundpleaseconfirthatthecameraisconne.Text"));
- return;
- }
- if (m_isWorking)
- {
- StopWorking();
- return;
- }
- if (m_parameterOne.StopPos == 0 || m_parameterOne.StartPos == 0)
- {
- MessageBox.Show(PdnResources.GetString("Menu.Pleasesetthestartandstoppositions2.Text"));
- return;
- }
- // 重新自动计算,防止没有手动点击计算
- m_parameterOne.Calculate();
- var zscanparm = m_parameterOne.ZScanParameter;
- m_Stage.ZScan(zscanparm.Start, zscanparm.Track, zscanparm.Times, Shooting, null, StopWorking);
- m_isWorking = true;
- btnWorking.Text = PdnResources.GetString("Menu.stop.text");
- m_moveTimes = 0;
- // 禁用控件
- m_parameterOne.Enabled = false;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void StopWorking()
- {
- m_Stage.FreeZ();
- m_isWorking = false;
- this.Invoke(new Action(() =>
- {
- // 更改控件状态
- m_parameterOne.Enabled = true;
- btnWorking.Text = PdnResources.GetString("Menu.Started.text");
- }));
- }
- int m_captureWait = 200;
- private void Shooting()
- {
- Thread.Sleep(m_captureWait);
- // 修改拍摄高度
- this.Invoke(new Action(() =>
- {
- m_parameterOne.updatelblHeight(m_currentStep);
- }));
- if (m_merge == null)
- {
- m_merge = m_matClone;
- }
- else
- {
- Coll_Ori[1] = m_matClone;
- Coll_Ori[0] = m_merge;
- m_merge = Merge.GetMergeMatForCamera(Coll_Ori);
- }
- picDepth.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_merge);
- GC.Collect();
- ++m_moveTimes;
- if (m_moveTimes == m_parameterOne.Times)
- {
- // StopWorking();
- return;
- }
- }
- #region 等待机制
- bool _waitFlag = false;
- private void ResetWait()
- { _waitFlag = false; }
- private void Wait()
- {
- _waitFlag = true;
- while (_waitFlag)
- {
- Thread.Sleep(100);
- }
- }
- #endregion
- private void btnCreatImgFile_Click(object sender, EventArgs e)
- {
- if (m_merge == null)
- {
- MessageBox.Show(PdnResources.GetString("Menu.ofimageshavenotyetbeengene.Text"));
- return;
- }
- DocumentWorkspace dw = m_appWorkspace.AddNewDocumentWorkspace();
- Document document = Document.FromImageMat(m_merge.Clone());
- dw.Document = document;
- dw.xmlSaveModel = Startup.instance.ruleDB;
- dw.InitRulerInfo();
- m_appWorkspace.ActiveDocumentWorkspace = dw;
- }
- #region StageEvents
- public void OnUpdatePosition()
- {
- try //可能出现窗口销毁后回调
- {
- if (!this.IsDisposed)
- this.Invoke(new Action(() =>
- {
- m_parameterOne.PositionZ = m_Stage.Z;
- }));
- }
- catch { }
- }
- public void OnTimeoutConnect()
- {
- StopWorking();
- MessageBox.Show(PdnResources.GetString("Menu.Theconsoleresponsetimeout.Text"));
- }
- public void OnErrorSend()
- {
- StopWorking();
- MessageBox.Show(PdnResources.GetString("Menu.Replydataarsingerror.Text"));
- }
- #endregion
- private void btnZClear_Click(object sender, EventArgs e)
- {
- m_Stage.ClearPosZ();
- }
- }
- }
|