From ef02d1344586bd290b4a8a99863d257c4235c976 Mon Sep 17 00:00:00 2001 From: zqm Date: Fri, 27 Mar 2026 11:01:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A4=E6=96=AD=E4=BA=AE?= =?UTF-8?q?=E7=81=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CS/Framework4.0/Camera/Camera/Camera.cs | 49 ++++++--- .../Framework4.0/Camera/Camera/LedDetector.cs | 25 +++-- .../Camera/Camera/Setting.Designer.cs | 99 +++++++++++++++++++ .../CS/Framework4.0/Camera/Camera/Setting.cs | 48 ++++++++- .../CS/Framework4.0/Camera/Test/Test.csproj | 2 +- 5 files changed, 200 insertions(+), 23 deletions(-) diff --git a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs index d31c203..2576f29 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs @@ -23,7 +23,7 @@ namespace Camera private const string PASSWD = "Yexian.net.168"; // 调试模式 - private const Boolean IsDebug = false; + private const Boolean IsDebug = true; // 配置文件目录 private string _configPath; @@ -34,6 +34,22 @@ namespace Camera private ConcurrentDictionary _ledZoneColors = new ConcurrentDictionary(); private ConcurrentDictionary _ledZoneDetectionResults = new ConcurrentDictionary(); private LedDetector _ledDetector = new LedDetector(); + + public void SetLedDetectorParams(int brightLimit, int satLimit) + { + _ledDetector.BrightLimit = brightLimit; + _ledDetector.SatLimit = satLimit; + } + + public int GetBrightLimit() + { + return _ledDetector.BrightLimit; + } + + public int GetSatLimit() + { + return _ledDetector.SatLimit; + } private Color _detectionZoneColor = Color.Black; // 检测状态 @@ -53,23 +69,13 @@ namespace Camera public event EventHandler ImageCaptured; /// - /// 根据区域编号获取颜色 + /// 根据区域编号获取检测结果 /// /// 区域编号 - /// 对应区域的颜色 - public Color GetColor(int AreaNum) + /// 检测结果字符串 + public string GetColor(int AreaNum) { - switch (AreaNum) - { - case 1: - return Color.Red; - case 2: - return Color.Green; - case 3: - return Color.Blue; - default: - return Color.Gray; - } + return GetLedZoneDetectionResult(AreaNum); } /// @@ -460,6 +466,9 @@ namespace Camera string colorStr = ColorTranslator.ToHtml(ledColor); writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, colorStr)); } + + writer.WriteLine(); + writer.WriteLine(string.Format("亮灭阈值,{0},{1}", _ledDetector.BrightLimit, _ledDetector.SatLimit)); } } catch { } @@ -486,6 +495,7 @@ namespace Camera { string line; bool isDetectionZone = true; + bool isReadingLedThresholds = false; while ((line = reader.ReadLine()) != null) { if (string.IsNullOrWhiteSpace(line)) @@ -495,6 +505,15 @@ namespace Camera } string[] parts = line.Split(','); + if (parts.Length >= 2 && parts[0] == "亮灭阈值") + { + if (int.TryParse(parts[1], out int brightLimit)) + _ledDetector.BrightLimit = brightLimit; + if (parts.Length >= 3 && int.TryParse(parts[2], out int satLimit)) + _ledDetector.SatLimit = satLimit; + continue; + } + if (parts.Length >= 4) { if (isDetectionZone) diff --git a/Windows/CS/Framework4.0/Camera/Camera/LedDetector.cs b/Windows/CS/Framework4.0/Camera/Camera/LedDetector.cs index 8bb8430..06e19eb 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/LedDetector.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/LedDetector.cs @@ -9,8 +9,20 @@ public enum LedColor { Unknown, Red, Green, Blue } public class LedDetector { - private const int BRIGHT_LIMIT = 110; - private const int SAT_LIMIT = 35; + private int _brightLimit = 110; + private int _satLimit = 35; + + public int BrightLimit + { + get { return _brightLimit; } + set { _brightLimit = value; } + } + + public int SatLimit + { + get { return _satLimit; } + set { _satLimit = value; } + } public Tuple Detect(Image image, Rectangle roi) { @@ -29,7 +41,8 @@ public class LedDetector S.Dispose(); V.Dispose(); - bool isOn = avgV > BRIGHT_LIMIT && avgS < SAT_LIMIT; + //bool isOn = avgV > _brightLimit; + bool isOn = avgV > _brightLimit && avgS > _satLimit; if (!isOn) { @@ -37,11 +50,11 @@ public class LedDetector } LedColor color = LedColor.Unknown; - if ((avgH >= 0 && avgH <= 10) || (avgH >= 170 && avgH <= 180)) + if ((avgH >= 60 && avgH < 75)) color = LedColor.Red; - else if (avgH >= 35 && avgH <= 75) + else if (avgH >= 30 && avgH < 60) color = LedColor.Green; - else if (avgH >= 90 && avgH <= 130) + else if (avgH >= 90 && avgH < 130) color = LedColor.Blue; return new Tuple(LedState.On, color); diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs index 497ade5..f2dccd1 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs @@ -27,6 +27,11 @@ namespace Camera this.toolStripButton4 = new System.Windows.Forms.ToolStripButton(); this.toolStripTextBox1 = new System.Windows.Forms.ToolStripTextBox(); this.toolStrip2 = new System.Windows.Forms.ToolStrip(); + this.toolStrip3 = new System.Windows.Forms.ToolStrip(); + this.toolStripLabel5 = new System.Windows.Forms.ToolStripLabel(); + this.toolStripNumericUpDown5 = new ToolStripNumericUpDown(); + this.toolStripLabel6 = new System.Windows.Forms.ToolStripLabel(); + this.toolStripNumericUpDown6 = new ToolStripNumericUpDown(); this.toolStripLabel1 = new System.Windows.Forms.ToolStripLabel(); this.toolStripNumericUpDown1 = new ToolStripNumericUpDown(); this.toolStripLabel2 = new System.Windows.Forms.ToolStripLabel(); @@ -43,6 +48,7 @@ namespace Camera ((System.ComponentModel.ISupportInitialize)(this.picBoxCamera)).BeginInit(); this.toolStrip1.SuspendLayout(); this.toolStrip2.SuspendLayout(); + this.toolStrip3.SuspendLayout(); this.toolStripContainer1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); @@ -93,6 +99,7 @@ namespace Camera this.toolStripContainer1.TopToolStripPanel.Padding = new System.Windows.Forms.Padding(0); this.toolStripContainer1.TopToolStripPanel.Controls.Add(this.toolStrip1); this.toolStripContainer1.TopToolStripPanel.Controls.Add(this.toolStrip2); + this.toolStripContainer1.TopToolStripPanel.Controls.Add(this.toolStrip3); // // toolStripContainer1.ContentPanel // @@ -181,6 +188,89 @@ namespace Camera this.toolStrip2.Size = new System.Drawing.Size(559, 30); this.toolStrip2.TabIndex = 1; // + // toolStrip3 + // + this.toolStrip3.Dock = System.Windows.Forms.DockStyle.Top; + this.toolStrip3.Location = new System.Drawing.Point(0, 55); + this.toolStrip3.ImageScalingSize = new System.Drawing.Size(32, 32); + this.toolStrip3.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripLabel5, + this.toolStripNumericUpDown5, + this.toolStripLabel6, + this.toolStripNumericUpDown6}); + this.toolStrip3.Name = "toolStrip3"; + this.toolStrip3.Size = new System.Drawing.Size(559, 30); + this.toolStrip3.TabIndex = 2; + this.toolStrip3.Visible = true; + // + // toolStripLabel5 + // + this.toolStripLabel5.Name = "toolStripLabel5"; + this.toolStripLabel5.Size = new System.Drawing.Size(56, 27); + this.toolStripLabel5.Text = "亮度:"; + // + // toolStripNumericUpDown5 + // + this.toolStripNumericUpDown5.AutoSize = false; + this.toolStripNumericUpDown5.DecimalPlaces = 0; + this.toolStripNumericUpDown5.Increment = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown5.Maximum = new decimal(new int[] { + 255, + 0, + 0, + 0}); + this.toolStripNumericUpDown5.Minimum = new decimal(new int[] { + 0, + 0, + 0, + 0}); + this.toolStripNumericUpDown5.Name = "toolStripNumericUpDown5"; + this.toolStripNumericUpDown5.Size = new System.Drawing.Size(60, 27); + this.toolStripNumericUpDown5.ToolTipText = "亮度阈值"; + this.toolStripNumericUpDown5.Value = new decimal(new int[] { + 110, + 0, + 0, + 0}); + // + // toolStripLabel6 + // + this.toolStripLabel6.Name = "toolStripLabel6"; + this.toolStripLabel6.Size = new System.Drawing.Size(44, 27); + this.toolStripLabel6.Text = "饱和度:"; + // + // toolStripNumericUpDown6 + // + this.toolStripNumericUpDown6.AutoSize = false; + this.toolStripNumericUpDown6.DecimalPlaces = 0; + this.toolStripNumericUpDown6.Increment = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.toolStripNumericUpDown6.Maximum = new decimal(new int[] { + 255, + 0, + 0, + 0}); + this.toolStripNumericUpDown6.Minimum = new decimal(new int[] { + 0, + 0, + 0, + 0}); + this.toolStripNumericUpDown6.Name = "toolStripNumericUpDown6"; + this.toolStripNumericUpDown6.Size = new System.Drawing.Size(60, 27); + this.toolStripNumericUpDown6.ToolTipText = "饱和度阈值"; + this.toolStripNumericUpDown6.Value = new decimal(new int[] { + 35, + 0, + 0, + 0}); + // // toolStripLabel1 // this.toolStripLabel1.Name = "toolStripLabel1"; @@ -320,6 +410,8 @@ namespace Camera 0, 0}); this.toolStripNumericUpDown4.ValueChanged += new System.EventHandler(this.ToolStripNumericUpDown4_ValueChanged); + this.toolStripNumericUpDown5.ValueChanged += new System.EventHandler(this.ToolStripNumericUpDown5_ValueChanged); + this.toolStripNumericUpDown6.ValueChanged += new System.EventHandler(this.ToolStripNumericUpDown6_ValueChanged); // // dataGridView1 // @@ -359,6 +451,8 @@ namespace Camera this.toolStrip1.PerformLayout(); this.toolStrip2.ResumeLayout(false); this.toolStrip2.PerformLayout(); + this.toolStrip3.ResumeLayout(false); + this.toolStrip3.PerformLayout(); this.toolStripContainer1.ResumeLayout(false); this.toolStripContainer1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); @@ -386,6 +480,11 @@ namespace Camera private ToolStripNumericUpDown toolStripNumericUpDown3; private System.Windows.Forms.ToolStripLabel toolStripLabel4; private ToolStripNumericUpDown toolStripNumericUpDown4; + private System.Windows.Forms.ToolStrip toolStrip3; + private System.Windows.Forms.ToolStripLabel toolStripLabel5; + private ToolStripNumericUpDown toolStripNumericUpDown5; + private System.Windows.Forms.ToolStripLabel toolStripLabel6; + private ToolStripNumericUpDown toolStripNumericUpDown6; private System.Windows.Forms.DataGridView dataGridView1; } } \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs index 2811e34..1f7bc55 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs @@ -30,6 +30,9 @@ namespace Camera // 设置NumericUpDown的基本属性 _numericUpDown.Size = new Size(60, 22); _numericUpDown.TextAlign = HorizontalAlignment.Right; + + // 转发ValueChanged事件 + _numericUpDown.ValueChanged += (s, e) => ValueChanged?.Invoke(s, e); } [System.ComponentModel.Browsable(false)] @@ -205,6 +208,8 @@ namespace Camera _camera.ImageCaptured += Camera_ImageCaptured; _detectionZoneColor = _camera.GetDetectionZoneColor(); _ledZoneColors = _camera.GetLedZoneColors(); + toolStripNumericUpDown5.Value = _camera.GetBrightLimit(); + toolStripNumericUpDown6.Value = _camera.GetSatLimit(); } picBoxCamera.BackColor = Color.Gray; splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2; @@ -1180,14 +1185,15 @@ namespace Camera if (dataGridView1.SelectedRows.Count > 0) { int newSelectedZoneIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value); + System.Diagnostics.Debug.WriteLine("列表选中索引: " + newSelectedZoneIndex); if (newSelectedZoneIndex != _lastSelectedZoneIndex) { _lastSelectedZoneIndex = newSelectedZoneIndex; _selectedZoneIndex = newSelectedZoneIndex; - // 更新位置和宽高值 if (_selectedZoneIndex >= 0 && _camera != null) { Rectangle zone = _camera.GetLedZone(_selectedZoneIndex); + System.Diagnostics.Debug.WriteLine("GetLedZone(" + _selectedZoneIndex + ") 返回: X=" + zone.X + ", Y=" + zone.Y + ", W=" + zone.Width + ", H=" + zone.Height); toolStripNumericUpDown1.Value = zone.X; toolStripNumericUpDown2.Value = zone.Y; toolStripNumericUpDown3.Value = zone.Width; @@ -1391,6 +1397,7 @@ namespace Camera toolStripButton4.Visible = false; toolStripTextBox1.Visible = false; toolStripTextBox1.Text = ""; + toolStrip3.Visible = true; // 如果有LED区域则显示位置和宽高设置框 if (_camera != null && _camera.GetLedZoneCount() > 0) { @@ -1426,6 +1433,7 @@ namespace Camera toolStripTextBox1.Text = _currentLedIndex.ToString(); // 不显示位置和宽高设置框 toolStrip2.Visible = false; + toolStrip3.Visible = false; break; case 2: // 绘制状态 toolStripButton2.Visible = true; @@ -1434,6 +1442,7 @@ namespace Camera toolStripTextBox1.Visible = false; toolStripTextBox1.Text = ""; toolStrip2.Visible = false; + toolStrip3.Visible = false; break; } } @@ -1475,6 +1484,7 @@ namespace Camera Rectangle zone = _camera.GetLedZone(index); zone.X = x; _camera.SetLedZone(index, zone); + UpdateDataGridViewRow(index, zone); picBoxCamera.Invalidate(); } @@ -1500,6 +1510,7 @@ namespace Camera Rectangle zone = _camera.GetLedZone(index); zone.Y = y; _camera.SetLedZone(index, zone); + UpdateDataGridViewRow(index, zone); picBoxCamera.Invalidate(); } @@ -1525,9 +1536,25 @@ namespace Camera Rectangle zone = _camera.GetLedZone(index); zone.Width = width; _camera.SetLedZone(index, zone); + UpdateDataGridViewRow(index, zone); picBoxCamera.Invalidate(); } + private void UpdateDataGridViewRow(int index, Rectangle zone) + { + foreach (DataGridViewRow row in dataGridView1.Rows) + { + if (Convert.ToInt32(row.Cells["Index"].Value) == index) + { + row.Cells["X"].Value = zone.X; + row.Cells["Y"].Value = zone.Y; + row.Cells["Width"].Value = zone.Width; + row.Cells["Height"].Value = zone.Height; + break; + } + } + } + // 高度相关事件 private void ToolStripNumericUpDown4_ValueChanged(object sender, EventArgs e) { @@ -1550,7 +1577,26 @@ namespace Camera Rectangle zone = _camera.GetLedZone(index); zone.Height = height; _camera.SetLedZone(index, zone); + UpdateDataGridViewRow(index, zone); picBoxCamera.Invalidate(); } + + // 亮度阈值事件 + private void ToolStripNumericUpDown5_ValueChanged(object sender, EventArgs e) + { + if (_camera == null) return; + int brightLimit = (int)toolStripNumericUpDown5.Value; + int satLimit = (int)toolStripNumericUpDown6.Value; + _camera.SetLedDetectorParams(brightLimit, satLimit); + } + + // 饱和度阈值事件 + private void ToolStripNumericUpDown6_ValueChanged(object sender, EventArgs e) + { + if (_camera == null) return; + int brightLimit = (int)toolStripNumericUpDown5.Value; + int satLimit = (int)toolStripNumericUpDown6.Value; + _camera.SetLedDetectorParams(brightLimit, satLimit); + } } } diff --git a/Windows/CS/Framework4.0/Camera/Test/Test.csproj b/Windows/CS/Framework4.0/Camera/Test/Test.csproj index 8335d07..4b45671 100644 --- a/Windows/CS/Framework4.0/Camera/Test/Test.csproj +++ b/Windows/CS/Framework4.0/Camera/Test/Test.csproj @@ -13,7 +13,7 @@ true - x86 + AnyCPU true full false