增加结果显示列
This commit is contained in:
@@ -29,8 +29,13 @@ namespace Camera
|
|||||||
private Rectangle _detectionZone;
|
private Rectangle _detectionZone;
|
||||||
private ConcurrentDictionary<int, Rectangle> _ledZones = new ConcurrentDictionary<int, Rectangle>();
|
private ConcurrentDictionary<int, Rectangle> _ledZones = new ConcurrentDictionary<int, Rectangle>();
|
||||||
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
|
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
|
||||||
|
private ConcurrentDictionary<int, string> _ledZoneDetectionResults = new ConcurrentDictionary<int, string>();
|
||||||
private Color _detectionZoneColor = Color.Black;
|
private Color _detectionZoneColor = Color.Black;
|
||||||
|
|
||||||
|
// 检测状态
|
||||||
|
private bool _isDetecting = false;
|
||||||
|
private readonly object _detectionLock = new object();
|
||||||
|
|
||||||
// 定时器
|
// 定时器
|
||||||
private System.Timers.Timer _timer;
|
private System.Timers.Timer _timer;
|
||||||
|
|
||||||
@@ -256,8 +261,10 @@ namespace Camera
|
|||||||
{
|
{
|
||||||
Rectangle r;
|
Rectangle r;
|
||||||
Color c;
|
Color c;
|
||||||
|
string s;
|
||||||
_ledZones.TryRemove(index, out r);
|
_ledZones.TryRemove(index, out r);
|
||||||
_ledZoneColors.TryRemove(index, out c);
|
_ledZoneColors.TryRemove(index, out c);
|
||||||
|
_ledZoneDetectionResults.TryRemove(index, out s);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -304,6 +311,32 @@ namespace Camera
|
|||||||
return Color.Lime;
|
return Color.Lime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取LED区域检测结果
|
||||||
|
/// </summary>
|
||||||
|
public ConcurrentDictionary<int, string> GetLedZoneDetectionResults()
|
||||||
|
{
|
||||||
|
return _ledZoneDetectionResults;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取指定索引的LED区域检测结果
|
||||||
|
/// </summary>
|
||||||
|
public string GetLedZoneDetectionResult(int index)
|
||||||
|
{
|
||||||
|
if (_ledZoneDetectionResults.ContainsKey(index))
|
||||||
|
return _ledZoneDetectionResults[index];
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 设置LED区域检测结果
|
||||||
|
/// </summary>
|
||||||
|
public void SetLedZoneDetectionResult(int index, string result)
|
||||||
|
{
|
||||||
|
_ledZoneDetectionResults[index] = result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 保存配置
|
/// 保存配置
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -427,10 +460,28 @@ namespace Camera
|
|||||||
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Timer_Elapsed 开始");
|
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Timer_Elapsed 开始");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
lock (_detectionLock)
|
||||||
|
{
|
||||||
|
if (_isDetecting)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("Camera: 正在检测,跳过本次");
|
||||||
|
if (_timer != null)
|
||||||
|
{
|
||||||
|
_timer.Stop();
|
||||||
|
_timer.Start();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_isDetecting = true;
|
||||||
|
}
|
||||||
|
|
||||||
Image image = GetCameraImage();
|
Image image = GetCameraImage();
|
||||||
if (image != null)
|
if (image != null)
|
||||||
{
|
{
|
||||||
System.Diagnostics.Debug.WriteLine("Camera: 捕获图像 Size=" + image.Width + "x" + image.Height);
|
System.Diagnostics.Debug.WriteLine("Camera: 捕获图像 Size=" + image.Width + "x" + image.Height);
|
||||||
|
|
||||||
|
DetectLedZones(image);
|
||||||
|
|
||||||
Image oldImage = null;
|
Image oldImage = null;
|
||||||
lock (_imageLock)
|
lock (_imageLock)
|
||||||
{
|
{
|
||||||
@@ -451,6 +502,10 @@ namespace Camera
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
lock (_detectionLock)
|
||||||
|
{
|
||||||
|
_isDetecting = false;
|
||||||
|
}
|
||||||
if (_timer != null)
|
if (_timer != null)
|
||||||
{
|
{
|
||||||
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 重新启动定时器");
|
System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 重新启动定时器");
|
||||||
@@ -460,6 +515,66 @@ namespace Camera
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检测LED区域颜色
|
||||||
|
/// </summary>
|
||||||
|
private void DetectLedZones(Image image)
|
||||||
|
{
|
||||||
|
if (image == null || _ledZones.Count == 0) return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (Bitmap bmp = new Bitmap(image))
|
||||||
|
{
|
||||||
|
foreach (var kvp in _ledZones)
|
||||||
|
{
|
||||||
|
int index = kvp.Key;
|
||||||
|
Rectangle zone = kvp.Value;
|
||||||
|
|
||||||
|
if (zone.Width <= 0 || zone.Height <= 0) continue;
|
||||||
|
if (zone.X < 0 || zone.Y < 0 || zone.X + zone.Width > bmp.Width || zone.Y + zone.Height > bmp.Height)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int redPixelCount = 0;
|
||||||
|
int greenPixelCount = 0;
|
||||||
|
int totalPixels = 0;
|
||||||
|
|
||||||
|
for (int x = zone.X; x < zone.X + zone.Width; x++)
|
||||||
|
{
|
||||||
|
for (int y = zone.Y; y < zone.Y + zone.Height; y++)
|
||||||
|
{
|
||||||
|
Color pixel = bmp.GetPixel(x, y);
|
||||||
|
if (pixel.R > 150 && pixel.G < 100 && pixel.B < 100)
|
||||||
|
{
|
||||||
|
redPixelCount++;
|
||||||
|
}
|
||||||
|
else if (pixel.G > 150 && pixel.R < 100 && pixel.B < 100)
|
||||||
|
{
|
||||||
|
greenPixelCount++;
|
||||||
|
}
|
||||||
|
totalPixels++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string result = "";
|
||||||
|
if (totalPixels > 0)
|
||||||
|
{
|
||||||
|
if (redPixelCount * 100 / totalPixels > 30)
|
||||||
|
result = "Red";
|
||||||
|
else if (greenPixelCount * 100 / totalPixels > 30)
|
||||||
|
result = "Green";
|
||||||
|
}
|
||||||
|
|
||||||
|
_ledZoneDetectionResults[index] = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine("Camera: 检测LED区域失败: " + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取摄像头图像
|
/// 获取摄像头图像
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ namespace Camera
|
|||||||
// splitContainer1.Panel2
|
// splitContainer1.Panel2
|
||||||
//
|
//
|
||||||
this.splitContainer1.Panel2.Controls.Add(this.toolStripContainer1);
|
this.splitContainer1.Panel2.Controls.Add(this.toolStripContainer1);
|
||||||
this.splitContainer1.Panel2MinSize = 457;
|
this.splitContainer1.Panel2MinSize = 460;
|
||||||
this.splitContainer1.Size = new System.Drawing.Size(1263, 515);
|
this.splitContainer1.Size = new System.Drawing.Size(1263, 515);
|
||||||
this.splitContainer1.SplitterDistance = 700;
|
this.splitContainer1.SplitterDistance = 700;
|
||||||
this.splitContainer1.TabIndex = 0;
|
this.splitContainer1.TabIndex = 0;
|
||||||
|
|||||||
@@ -131,6 +131,7 @@ namespace Camera
|
|||||||
private int _ledZoneState = 0;
|
private int _ledZoneState = 0;
|
||||||
private Color _detectionZoneColor = Color.Black;
|
private Color _detectionZoneColor = Color.Black;
|
||||||
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
|
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
|
||||||
|
private ConcurrentDictionary<int, string> _ledZoneDetectionResults = new ConcurrentDictionary<int, string>();
|
||||||
private Point _resizeStartPoint;
|
private Point _resizeStartPoint;
|
||||||
private Rectangle _originalResizeRect;
|
private Rectangle _originalResizeRect;
|
||||||
private int _hoveredHandle = -1;
|
private int _hoveredHandle = -1;
|
||||||
@@ -183,6 +184,7 @@ namespace Camera
|
|||||||
dataGridView1.Columns.Add("Width", "宽度");
|
dataGridView1.Columns.Add("Width", "宽度");
|
||||||
dataGridView1.Columns.Add("Height", "高度");
|
dataGridView1.Columns.Add("Height", "高度");
|
||||||
dataGridView1.Columns.Add("Color", "颜色");
|
dataGridView1.Columns.Add("Color", "颜色");
|
||||||
|
dataGridView1.Columns.Add("Detection", "检测结果");
|
||||||
|
|
||||||
dataGridView1.Columns["Index"].Width = 50;
|
dataGridView1.Columns["Index"].Width = 50;
|
||||||
dataGridView1.Columns["X"].Width = 60;
|
dataGridView1.Columns["X"].Width = 60;
|
||||||
@@ -190,6 +192,7 @@ namespace Camera
|
|||||||
dataGridView1.Columns["Width"].Width = 60;
|
dataGridView1.Columns["Width"].Width = 60;
|
||||||
dataGridView1.Columns["Height"].Width = 60;
|
dataGridView1.Columns["Height"].Width = 60;
|
||||||
dataGridView1.Columns["Color"].Width = 70;
|
dataGridView1.Columns["Color"].Width = 70;
|
||||||
|
dataGridView1.Columns["Detection"].Width = 80;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,8 +208,7 @@ namespace Camera
|
|||||||
}
|
}
|
||||||
picBoxCamera.BackColor = Color.Gray;
|
picBoxCamera.BackColor = Color.Gray;
|
||||||
splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
|
splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
|
||||||
splitContainer1.Panel2MinSize = 380;
|
splitContainer1.SplitterDistance = splitContainer1.Width - splitContainer1.Panel2MinSize;
|
||||||
splitContainer1.SplitterDistance = splitContainer1.Width - 380;
|
|
||||||
UpdateDataGridView();
|
UpdateDataGridView();
|
||||||
UpdateLedZoneButtonsVisibility(0);
|
UpdateLedZoneButtonsVisibility(0);
|
||||||
}
|
}
|
||||||
@@ -313,10 +315,12 @@ namespace Camera
|
|||||||
if (picBoxCamera.InvokeRequired)
|
if (picBoxCamera.InvokeRequired)
|
||||||
{
|
{
|
||||||
picBoxCamera.Invoke(new Action<Image>(UpdateImage), e.Image);
|
picBoxCamera.Invoke(new Action<Image>(UpdateImage), e.Image);
|
||||||
|
picBoxCamera.Invoke(new Action(UpdateDataGridView));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
UpdateImage(e.Image);
|
UpdateImage(e.Image);
|
||||||
|
UpdateDataGridView();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -350,12 +354,13 @@ namespace Camera
|
|||||||
int index = kvp.Key;
|
int index = kvp.Key;
|
||||||
Rectangle ledZone = kvp.Value;
|
Rectangle ledZone = kvp.Value;
|
||||||
Color ledColor = _camera.GetLedZoneColor(index);
|
Color ledColor = _camera.GetLedZoneColor(index);
|
||||||
|
string detectionResult = _camera.GetLedZoneDetectionResult(index);
|
||||||
|
|
||||||
if (ledZone.Width > 0 && ledZone.Height > 0)
|
if (ledZone.Width > 0 && ledZone.Height > 0)
|
||||||
{
|
{
|
||||||
int relativeX = ledZone.X - detectionZone.X;
|
int relativeX = ledZone.X - detectionZone.X;
|
||||||
int relativeY = ledZone.Y - detectionZone.Y;
|
int relativeY = ledZone.Y - detectionZone.Y;
|
||||||
dataGridView1.Rows.Add(index, relativeX, relativeY, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor));
|
dataGridView1.Rows.Add(index, relativeX, relativeY, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor), detectionResult);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user