调节表格

This commit is contained in:
zqm
2026-03-27 13:31:54 +08:00
parent ef02d13445
commit d37e2fd005
4 changed files with 172 additions and 9 deletions

View File

@@ -23,7 +23,7 @@ namespace Camera
private const string PASSWD = "Yexian.net.168";
// 调试模式
private const Boolean IsDebug = true;
private const Boolean IsDebug = false;
// 配置文件目录
private string _configPath;
@@ -33,6 +33,7 @@ namespace Camera
private ConcurrentDictionary<int, Rectangle> _ledZones = new ConcurrentDictionary<int, Rectangle>();
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
private ConcurrentDictionary<int, string> _ledZoneDetectionResults = new ConcurrentDictionary<int, string>();
private ConcurrentDictionary<int, Tuple<double, double, double>> _ledZoneHsvValues = new ConcurrentDictionary<int, Tuple<double, double, double>>();
private LedDetector _ledDetector = new LedDetector();
public void SetLedDetectorParams(int brightLimit, int satLimit)
@@ -434,6 +435,24 @@ namespace Camera
_ledZoneDetectionResults[index] = result;
}
/// <summary>
/// 获取所有LED区域的HSV值
/// </summary>
public ConcurrentDictionary<int, Tuple<double, double, double>> GetLedZoneHsvValues()
{
return _ledZoneHsvValues;
}
/// <summary>
/// 获取指定索引的LED区域的HSV值
/// </summary>
public Tuple<double, double, double> GetLedZoneHsvValue(int index)
{
if (_ledZoneHsvValues.ContainsKey(index))
return _ledZoneHsvValues[index];
return new Tuple<double, double, double>(0, 0, 0);
}
/// <summary>
/// 保存配置
/// </summary>
@@ -495,7 +514,6 @@ namespace Camera
{
string line;
bool isDetectionZone = true;
bool isReadingLedThresholds = false;
while ((line = reader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line))
@@ -677,6 +695,7 @@ namespace Camera
result = "Off";
}
_ledZoneDetectionResults[index] = result;
_ledZoneHsvValues[index] = new Tuple<double, double, double>(detectResult.Item3, detectResult.Item4, detectResult.Item5);
}
}
}

View File

@@ -24,7 +24,7 @@ public class LedDetector
set { _satLimit = value; }
}
public Tuple<LedState, LedColor> Detect(Image<Bgr, byte> image, Rectangle roi)
public Tuple<LedState, LedColor, double, double, double> Detect(Image<Bgr, byte> image, Rectangle roi)
{
using (Image<Bgr, byte> subImg = image.GetSubRect(roi))
using (Image<Hsv, byte> hsv = subImg.Convert<Hsv, byte>())
@@ -41,12 +41,11 @@ public class LedDetector
S.Dispose();
V.Dispose();
//bool isOn = avgV > _brightLimit;
bool isOn = avgV > _brightLimit && avgS > _satLimit;
if (!isOn)
{
return new Tuple<LedState, LedColor>(LedState.Off, LedColor.Unknown);
return new Tuple<LedState, LedColor, double, double, double>(LedState.Off, LedColor.Unknown, avgH, avgS, avgV);
}
LedColor color = LedColor.Unknown;
@@ -57,7 +56,7 @@ public class LedDetector
else if (avgH >= 90 && avgH < 130)
color = LedColor.Blue;
return new Tuple<LedState, LedColor>(LedState.On, color);
return new Tuple<LedState, LedColor, double, double, double>(LedState.On, color, avgH, avgS, avgV);
}
}
}

View File

@@ -40,7 +40,9 @@ namespace Camera
this.toolStripNumericUpDown3 = new ToolStripNumericUpDown();
this.toolStripLabel4 = new System.Windows.Forms.ToolStripLabel();
this.toolStripNumericUpDown4 = new ToolStripNumericUpDown();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.dataGridView2 = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
@@ -50,7 +52,12 @@ namespace Camera
this.toolStrip2.SuspendLayout();
this.toolStrip3.SuspendLayout();
this.toolStripContainer1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit();
this.SuspendLayout();
//
// splitContainer1
@@ -58,6 +65,7 @@ namespace Camera
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
this.splitContainer1.SplitterWidth = 5;
//
// splitContainer1.Panel1
//
@@ -92,7 +100,6 @@ namespace Camera
this.toolStripContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.toolStripContainer1.Location = new System.Drawing.Point(0, 0);
this.toolStripContainer1.Name = "toolStripContainer1";
this.toolStripContainer1.Size = new System.Drawing.Size(559, 515);
this.toolStripContainer1.TabIndex = 0;
this.toolStripContainer1.Text = "toolStripContainer1";
this.toolStripContainer1.TopToolStripPanel.Dock = System.Windows.Forms.DockStyle.Top;
@@ -103,7 +110,7 @@ namespace Camera
//
// toolStripContainer1.ContentPanel
//
this.toolStripContainer1.ContentPanel.Controls.Add(this.dataGridView1);
this.toolStripContainer1.ContentPanel.Controls.Add(this.splitContainer2);
this.toolStripContainer1.ContentPanel.Dock = System.Windows.Forms.DockStyle.Fill;
//
// toolStrip1
@@ -420,6 +427,7 @@ namespace Camera
this.dataGridView1.AllowUserToResizeRows = false;
this.dataGridView1.ColumnHeadersHeight = 25;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
// this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
@@ -427,10 +435,46 @@ namespace Camera
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.RowTemplate.Height = 25;
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.Size = new System.Drawing.Size(559, 254);
this.dataGridView1.Size = new System.Drawing.Size(600, 254);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.SelectionChanged += new System.EventHandler(this.DataGridView1_SelectionChanged);
//
// splitContainer2
//
this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
this.splitContainer2.SplitterWidth = 5;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.dataGridView1);
this.splitContainer2.Panel1MinSize = 50;
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.dataGridView2);
this.splitContainer2.Panel2MinSize = 50;
this.splitContainer2.TabIndex = 1;
//
// dataGridView2
//
this.dataGridView2.AllowUserToAddRows = false;
this.dataGridView2.AllowUserToDeleteRows = false;
this.dataGridView2.AllowUserToResizeRows = false;
this.dataGridView2.Enabled = false;
this.dataGridView2.ColumnHeadersHeight = 25;
this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
this.dataGridView2.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView2.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
this.dataGridView2.Name = "dataGridView2";
this.dataGridView2.ReadOnly = true;
this.dataGridView2.RowHeadersVisible = false;
this.dataGridView2.RowTemplate.Height = 25;
this.dataGridView2.Size = new System.Drawing.Size(600, 254);
this.dataGridView2.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dataGridView2.TabIndex = 0;
//
// Setting
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
@@ -455,7 +499,9 @@ namespace Camera
this.toolStrip3.PerformLayout();
this.toolStripContainer1.ResumeLayout(false);
this.toolStripContainer1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit();
this.ResumeLayout(false);
}
@@ -486,5 +532,7 @@ namespace Camera
private System.Windows.Forms.ToolStripLabel toolStripLabel6;
private ToolStripNumericUpDown toolStripNumericUpDown6;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.SplitContainer splitContainer2;
private System.Windows.Forms.DataGridView dataGridView2;
}
}

View File

@@ -197,6 +197,19 @@ namespace Camera
dataGridView1.Columns["Color"].Width = 70;
dataGridView1.Columns["Detection"].Width = 80;
}
if (dataGridView2.Columns.Count == 0)
{
dataGridView2.Columns.Add("Index", "索引");
dataGridView2.Columns.Add("H", "H值");
dataGridView2.Columns.Add("S", "S值");
dataGridView2.Columns.Add("V", "V值");
dataGridView2.Columns["Index"].Width = 50;
dataGridView2.Columns["H"].Width = 80;
dataGridView2.Columns["S"].Width = 80;
dataGridView2.Columns["V"].Width = 80;
}
}
private void Setting_Load(object sender, EventArgs e)
@@ -214,6 +227,11 @@ namespace Camera
picBoxCamera.BackColor = Color.Gray;
splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
splitContainer1.SplitterDistance = splitContainer1.Width - splitContainer1.Panel2MinSize;
this.BeginInvoke(new Action(() =>
{
if (splitContainer2.Height > 0)
splitContainer2.SplitterDistance = splitContainer2.Height / 2;
}));
UpdateDataGridView();
UpdateLedZoneButtonsVisibility(0);
}
@@ -354,6 +372,7 @@ namespace Camera
{
Rectangle detectionZone = _camera.GetDetectionZone();
var allLedZones = _camera.GetLedZones();
var hsvValues = _camera.GetLedZoneHsvValues();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
@@ -366,6 +385,21 @@ namespace Camera
dataGridView1.Rows.Add(index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor), detectionResult);
}
}
dataGridView2.Rows.Clear();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
if (hsvValues.ContainsKey(index))
{
var hsv = hsvValues[index];
dataGridView2.Rows.Add(index, hsv.Item1.ToString("F1"), hsv.Item2.ToString("F1"), hsv.Item3.ToString("F1"));
}
else
{
dataGridView2.Rows.Add(index, "0", "0", "0");
}
}
}
picBoxCamera.Invalidate();
_isUpdatingDataGridView = false;
@@ -396,6 +430,8 @@ namespace Camera
}
}
UpdateHsvResults();
if (selectedIndex >= 0)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
@@ -414,6 +450,59 @@ namespace Camera
}
}
private void UpdateHsvResults()
{
if (_camera == null || dataGridView2.Rows.Count == 0) return;
var allLedZones = _camera.GetLedZones();
var hsvValues = _camera.GetLedZoneHsvValues();
if (dataGridView2.Rows.Count != allLedZones.Count)
{
dataGridView2.Rows.Clear();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
if (hsvValues.ContainsKey(index))
{
var hsv = hsvValues[index];
dataGridView2.Rows.Add(index, hsv.Item1.ToString("F1"), hsv.Item2.ToString("F1"), hsv.Item3.ToString("F1"));
}
else
{
dataGridView2.Rows.Add(index, "0", "0", "0");
}
}
}
else
{
foreach (DataGridViewRow row in dataGridView2.Rows)
{
int index = Convert.ToInt32(row.Cells["Index"].Value);
if (hsvValues.ContainsKey(index))
{
var hsv = hsvValues[index];
row.Cells["H"].Value = hsv.Item1.ToString("F1");
row.Cells["S"].Value = hsv.Item2.ToString("F1");
row.Cells["V"].Value = hsv.Item3.ToString("F1");
}
}
}
if (dataGridView1.SelectedRows.Count > 0)
{
int selectedIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (Convert.ToInt32(row.Cells["Index"].Value) == selectedIndex)
{
row.Selected = true;
break;
}
}
}
}
private void PicBoxCamera_Paint(object sender, PaintEventArgs e)
{
Image currentImage = null;
@@ -1199,6 +1288,14 @@ namespace Camera
toolStripNumericUpDown3.Value = zone.Width;
toolStripNumericUpDown4.Value = zone.Height;
}
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (Convert.ToInt32(row.Cells["Index"].Value) == newSelectedZoneIndex)
{
row.Selected = true;
break;
}
}
picBoxCamera.Invalidate();
}
}