using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using PaintDotNet.Base.SettingModel; using System.Collections; using PaintDotNet.ImageCollect.CameraManager; using TUCAMAPI; using TUCamera; using OpenCvSharp; using System.Threading; using OpenCvSharp.Extensions; namespace PaintDotNet.Setting.LabelComponent { /// /// 相机设置,调节界面 /// public class AdjustCameraControl : UserControl { #region 控件 /// /// 增益值下拉选 /// string[] gainArray = new string[] { PdnResources.GetString("Menu.Highgai.Text"), PdnResources.GetString("Menu.Lowgain.Text") }; /// /// 相机参数的Model /// private CameraParamModel m_cameraParamModel; private bool m_immediately; private bool m_toAddExtraButtons = false; private TUCamera.TUCamera m_camera; private System.Timers.Timer m_aeTimer; #endregion public AdjustCameraControl(CameraParamModel model, bool immediately, bool toAddExtraButtons = false) { m_cameraParamModel = model; m_immediately = immediately; m_toAddExtraButtons = toAddExtraButtons; m_camera = TUCameraManager.GetInstance().GetCurrentCamera(); InitializeComponent(); InitializeLanguageText(); InitColorAdjustRange(); InitializeControlData(); } public void ResetCameraParamModel(CameraParamModel model) { m_cameraParamModel = model; InitColorAdjustRange(); InitializeControlData(); } int resolution_width = 2448; int resolution_height = 2048; private void InitColorAdjustRange() { if (m_camera.IsOpen()) { // Get red channel range int minVal = 0; int maxVal = 0; m_camera.GetColorChannelRang(ColorChannel.RED, ref minVal, ref maxVal); redChannelTB.SetRange(minVal, maxVal); // Get green channel range m_camera.GetColorChannelRang(ColorChannel.GREEN, ref minVal, ref maxVal); greenChannelTB.SetRange(minVal, maxVal); // Get blue channel range m_camera.GetColorChannelRang(ColorChannel.BLUE, ref minVal, ref maxVal); blueChannelTB.SetRange(minVal, maxVal); var range = m_camera.GetGlobalGainRange(); trbGain.SetRange((int)range.Min, (int)range.Max); lblGainMin.Text = trbGain.Minimum.ToString(); lblGainMax.Text = trbGain.Maximum.ToString(); // 饱和度 minVal = 0; maxVal = 0; m_camera.GetSaturationRange(ref minVal, ref maxVal); baoheduTBar.SetRange(minVal, maxVal); lblSaturationMinVal.Text = ((int)minVal).ToString(); lblSaturationMaxVal.Text = ((int)maxVal).ToString(); var bitmap = m_camera.OneShoot(); if (bitmap == null) return; showHistImg(BitmapConverter.ToMat(bitmap)); } } Mat[] oldHists = new Mat[] { new Mat(), new Mat(), new Mat() }; private void showHistImg(Mat BoxMat) { Mat[] mats = Cv2.Split(BoxMat);//一张图片,将panda拆分成3个图片装进mat if (mats.Count() == 1) { Mat[] mats011 = new Mat[] { mats[0] };//panda的第一个通道,也就是B int[] channels011 = new int[] { 0 };//一个通道,初始化为通道0,这些东西可以共用设置一个就行 int[] histsize11 = new int[] { 256 };//一个通道,初始化为256箱子 Rangef[] range11 = new Rangef[1];//一个通道,范围 range11[0] = new Rangef(0.0F, 256.0F); Mat mask11 = new Mat();//不做掩码 Cv2.CalcHist(mats011, channels011, mask11, oldHists[0], 1, histsize11, range11);//对被拆分的图片单独进行计算 return; } Mat[] mats0 = new Mat[] { mats[0] };//panda的第一个通道,也就是B Mat[] mats1 = new Mat[] { mats[1] };//panda的第二个通道,也就是G Mat[] mats2 = new Mat[] { mats[2] };//panda的第三个通道,也就是R int[] channels0 = new int[] { 0 };//一个通道,初始化为通道0,这些东西可以共用设置一个就行 int[] channels1 = new int[] { 0 }; int[] channels2 = new int[] { 0 }; int[] histsize = new int[] { 256 };//一个通道,初始化为256箱子 Rangef[] range = new Rangef[1];//一个通道,范围 range[0] = new Rangef(0.0F, 256.0F); Mat mask = new Mat();//不做掩码 Cv2.CalcHist(mats0, channels0, mask, oldHists[0], 1, histsize, range);//对被拆分的图片单独进行计算 Cv2.CalcHist(mats1, channels1, mask, oldHists[1], 1, histsize, range);//对被拆分的图片单独进行计算 Cv2.CalcHist(mats2, channels2, mask, oldHists[2], 1, histsize, range);//对被拆分的图片单独进行计算 } private string UpdateExposureTime(UInt64 exposureTime) { string str = ""; int sec = 0; int msec = 0; int usec = 0; m_camera.UpdateExposureTime(ref sec, ref msec, ref usec, exposureTime); if (sec > 0) { str += sec + "s"; } else if (msec > 0) { str += msec + "ms"; } else if (usec > 0) { str += usec + "μs"; } return str; } /// /// 设置下拉等数据源 /// private void InitializeControlData() { // 曝光时间 int atExpValue = (int)(m_cameraParamModel.parame.LNExposure); NewMethod(atExpValue); // 增益值调整 int gainValue = m_cameraParamModel.parame.GlobalGain; if (gainValue >= this.trbGain.Minimum && gainValue <= this.trbGain.Maximum) { this.trbGain.Value = gainValue; } // 颜色值 int redChannelValue = (int)m_cameraParamModel.parame.RedChannel; if (redChannelValue >= this.redChannelTB.Minimum && redChannelValue <= this.redChannelTB.Maximum) { this.redChannelTB.Value = redChannelValue; } int greenChannelValue = (int)m_cameraParamModel.parame.GreenChannel; if (greenChannelValue >= this.greenChannelTB.Minimum && greenChannelValue <= this.greenChannelTB.Maximum) { this.greenChannelTB.Value = greenChannelValue; } int blueChannelValue = (int)m_cameraParamModel.parame.BlueChannel; if (blueChannelValue >= this.blueChannelTB.Minimum && blueChannelValue <= this.blueChannelTB.Maximum) { this.blueChannelTB.Value = blueChannelValue; } // 白平衡 if (m_cameraParamModel.parame.WhiteBalance == 1) { AutoWhiteBalance(true); } else { if (m_cameraParamModel.parame.FMExposure == 1) { m_camera.SetColorTemperatureByString("3200K"); } else if (m_cameraParamModel.parame.FMExposure == 2) { m_camera.SetColorTemperatureByString("5500K"); } AutoWhiteBalance(false); } // 显示颜色值通道 if (m_cameraParamModel.parame.ShowColorPBoxFlag == 1) { this.xianshiColourCheckBox.Checked = true; } else { this.xianshiColourCheckBox.Checked = false; } // 饱和度 int baoheduTBarValue = this.m_cameraParamModel.parame.Saturation; if (baoheduTBarValue >= this.baoheduTBar.Minimum && baoheduTBarValue <= this.baoheduTBar.Maximum) { this.baoheduTBar.Value = this.m_cameraParamModel.parame.Saturation; } if (m_toAddExtraButtons) { this.button6.Visible = true; this.button7.Visible = true; } } private void InitializeLanguageText() { this.groupBox2.Text = PdnResources.GetString("Menu.Gainvalueadjustment.text"); this.groupBox3.Text = PdnResources.GetString("Menu.Image.WhiteBalance.Text"); this.button1.Text = PdnResources.GetString("Menu.Primarywhitebalance.Text"); this.label12.Text = PdnResources.GetString("Menu.Blue.text"); this.label11.Text = PdnResources.GetString("Menu.green.text"); this.label10.Text = PdnResources.GetString("Menu.red.text"); this.label9.Text = PdnResources.GetString("Menu.yellow.text"); this.label8.Text = PdnResources.GetString("Menu.Magenta.text"); this.label7.Text = PdnResources.GetString("Menu.aqua.text"); this.xianshiColourCheckBox.Text = PdnResources.GetString("Menu.Displaycolorvaluechannel.text"); this.button3.Text = PdnResources.GetString("Menu.auto.text"); this.button2.Text = PdnResources.GetString("Menu.Manual.text"); this.groupBox4.Text = PdnResources.GetString("Menu.Colorandsaturation.Text"); this.label13.Text = PdnResources.GetString("Menu.saturation.text") + ":"; } private void InitializeComponent() { this.SuspendLayout(); // // AdjustCameraControl // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Name = "AdjustCameraControl"; this.Size = new System.Drawing.Size(490, 605); this.ResumeLayout(false); } private void TBar_MouseWheel(object sender, MouseEventArgs e) { ((HandledMouseEventArgs)e).Handled = true; } /// /// 手动白平衡 /// /// /// private void button2_Click(object sender, EventArgs e) { m_cameraParamModel.parame.FMExposure = 0; AutoWhiteBalance(false); Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).m_use = false; ((ImageCollect.CameraPreviewDialog)form).o_use = false; } } /// /// 自动白平衡按钮点击 /// /// /// private void button3_Click(object sender, EventArgs e) { AutoWhiteBalance(true); Thread.Sleep(1500); UpdateColorTemperature(); Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).m_use = false; ((ImageCollect.CameraPreviewDialog)form).o_use = false; } } /// /// 自动白平衡一次 /// /// /// private void button1_Click(object sender, EventArgs e) { AutoWhiteBalance(true); Thread.Sleep(1500); m_cameraParamModel.parame.FMExposure = 0; AutoWhiteBalance(false); Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).m_use = false; ((ImageCollect.CameraPreviewDialog)form).o_use = false; } } /// /// 色温3200K /// /// /// private void button4_Click(object sender, EventArgs e) { m_cameraParamModel.parame.FMExposure = 1; m_camera.SetColorTemperatureByString("3200K"); AutoWhiteBalance(false); Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).m_use = false; ((ImageCollect.CameraPreviewDialog)form).o_use = false; } } /// /// 色温5500K /// /// /// private void button5_Click(object sender, EventArgs e) { m_cameraParamModel.parame.FMExposure = 2; m_camera.SetColorTemperatureByString("5500K"); AutoWhiteBalance(false); Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).m_use = false; ((ImageCollect.CameraPreviewDialog)form).o_use = false; } } private void UpdateColorTemperature() { button2.Enabled = true; button3.Enabled = true; button4.Enabled = true; button5.Enabled = true; uint cct = 0; double redChannel = 0; double greenChannel = 0; double blueChannel = 0; m_camera.GetColorTemperature(ref redChannel, ref greenChannel, ref blueChannel, ref cct); m_cameraParamModel.parame.RedChannel = redChannel; m_cameraParamModel.parame.GreenChannel = greenChannel; m_cameraParamModel.parame.BlueChannel = blueChannel; // 颜色值 this.redChannelTB.Value = (int)m_cameraParamModel.parame.RedChannel; this.greenChannelTB.Value = (int)m_cameraParamModel.parame.GreenChannel; this.blueChannelTB.Value = (int)m_cameraParamModel.parame.BlueChannel; //string colorTemperatureString = m_camera.GetColorTemperatureString(cct); this.redChannelTB.Enabled = false; this.greenChannelTB.Enabled = false; this.blueChannelTB.Enabled = false; if (m_cameraParamModel.parame.WhiteBalance == 1) { button3.Enabled = false; } else { switch (m_cameraParamModel.parame.FMExposure) { case 0: button2.Enabled = false; this.redChannelTB.Enabled = true; this.greenChannelTB.Enabled = true; this.blueChannelTB.Enabled = true; break; case 1: button4.Enabled = false; break; case 2: button5.Enabled = false; break; } } xianshiColourCheckBox.Focus(); } /// /// 是否自动白平衡 修改显示样式 /// /// private void AutoWhiteBalance(Boolean isWhiteBalance) { if (isWhiteBalance) { this.redChannelTB.Enabled = false; this.greenChannelTB.Enabled = false; this.blueChannelTB.Enabled = false; m_cameraParamModel.parame.WhiteBalance = 1; if (m_immediately) { // 自动白平衡 m_camera.SetWhiteBalanceMode(WhiteBalanceMode.AUTO); } } else { m_cameraParamModel.parame.WhiteBalance = 0; if (m_immediately) { // 手动白平衡 m_camera.SetWhiteBalanceMode(WhiteBalanceMode.MANUAL); } } UpdateColorTemperature(); } /// /// 饱和度滑块滑动 /// /// /// private void baoheduTBar_ValueChanged(object sender, System.EventArgs e) { this.m_cameraParamModel.parame.Saturation = this.baoheduTBar.Value; //this.baoheduTBox.Text = ((this.baoheduTBar.Value - 10) / 10.0).ToString("0.0"); this.baoheduTBox.Text = this.baoheduTBar.Value.ToString(); // 设置到相机 if (m_immediately) { m_camera.SetSaturation(this.m_cameraParamModel.parame.Saturation); //usec } } /// /// 显示颜色通道值 /// /// /// private void xianshiColourCheckBox_CheckedChanged(object sender, EventArgs e) { CheckBox cb = (CheckBox)sender; this.yanghongsepictureBox.Visible = cb.Checked; this.yanghongsepictureBox.Visible = cb.Checked; ; this.lansepictureBox.Visible = cb.Checked; this.hongsepictureBox.Visible = cb.Checked; this.lansepictureBox.Visible = cb.Checked; this.huangsepictureBox.Visible = cb.Checked; this.lanlvpictureBox.Visible = cb.Checked; this.lvsepictureBox.Visible = cb.Checked; this.m_cameraParamModel.parame.ShowColorPBoxFlag = cb.Checked ? 1 : 0; } private void txtGain_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Back)) // 非数字键, 放弃该输入 { e.Handled = true; return; } if (e.KeyChar == (char)Keys.Enter) { try { int trbGainVal = Convert.ToInt32(txtGain.Text); if (trbGainVal < trbGain.Minimum) { trbGainVal = trbGain.Minimum; } if (trbGainVal > trbGain.Maximum) { trbGainVal = trbGain.Maximum; } trbGain.Value = trbGainVal; } catch (Exception ex) { MessageBox.Show(ex.Message); } } } private void redChannelTB_ValueChanged(object sender, EventArgs e) { Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).m_use = false; ((ImageCollect.CameraPreviewDialog)form).o_use = false; } m_cameraParamModel.parame.RedChannel = ((TrackBar)sender).Value; // 设置到相机 if (m_immediately) { m_camera.SetRedGain(m_cameraParamModel.parame.RedChannel); //usec } } private void greenChannelTB_Scroll(object sender, EventArgs e) { Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).m_use = false; ((ImageCollect.CameraPreviewDialog)form).o_use = false; } m_cameraParamModel.parame.GreenChannel = ((TrackBar)sender).Value; // 设置到相机 if (m_immediately) { m_camera.SetGreeGain(m_cameraParamModel.parame.GreenChannel); //usec } } private void blueChannelTB_ValueChanged(object sender, EventArgs e) { Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).m_use = false; ((ImageCollect.CameraPreviewDialog)form).o_use = false; } m_cameraParamModel.parame.BlueChannel = ((TrackBar)sender).Value; // 设置到相机 if (m_immediately) { m_camera.SetBlueGain(m_cameraParamModel.parame.BlueChannel); //usec } } private void trbGain_ValueChanged(object sender, EventArgs e) { txtGain.Text = trbGain.Value.ToString(); m_cameraParamModel.parame.GlobalGain = trbGain.Value; // 非白平衡模式有效 if (m_cameraParamModel.parame.WhiteBalance == 0) { // 设置到相机 if (m_immediately) { m_camera.Gain = m_cameraParamModel.parame.GlobalGain; //usec } } } /// /// 白平衡区域选择 /// /// /// private void button7_Click(object sender, EventArgs e) { Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).o_use = false; ((ImageCollect.CameraPreviewDialog)form).m_use = !((ImageCollect.CameraPreviewDialog)form).m_use; } } /// /// 白平衡原图取色 /// /// /// private void button6_Click(object sender, EventArgs e) { Form form = Application.OpenForms["CameraPreviewDialog"]; if (form != null) { ((ImageCollect.CameraPreviewDialog)form).m_use = false; ((ImageCollect.CameraPreviewDialog)form).o_use = !((ImageCollect.CameraPreviewDialog)form).o_use; ((ImageCollect.CameraPreviewDialog)form).m_startP = new System.Drawing.Point(-1, -1); } } } }