判断状态
This commit is contained in:
@@ -8,6 +8,9 @@ using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using Emgu.CV;
|
||||
using Emgu.CV.CvEnum;
|
||||
using Emgu.CV.Structure;
|
||||
|
||||
namespace Camera
|
||||
{
|
||||
@@ -30,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 LedDetector _ledDetector = new LedDetector();
|
||||
private Color _detectionZoneColor = Color.Black;
|
||||
|
||||
// 检测状态
|
||||
@@ -81,11 +85,48 @@ namespace Camera
|
||||
}
|
||||
}
|
||||
|
||||
private static bool _isEmguCvAvailable = false;
|
||||
private static string _emguCvError = "";
|
||||
|
||||
public static bool CheckEmguCv()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var testImage = new Image<Bgr, byte>(1, 1))
|
||||
{
|
||||
testImage.Dispose();
|
||||
}
|
||||
_isEmguCvAvailable = true;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_isEmguCvAvailable = false;
|
||||
_emguCvError = ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsEmguCvAvailable()
|
||||
{
|
||||
return _isEmguCvAvailable;
|
||||
}
|
||||
|
||||
public static string GetEmguCvError()
|
||||
{
|
||||
return _emguCvError;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始获取图像
|
||||
/// </summary>
|
||||
public void Start()
|
||||
{
|
||||
if (!CheckEmguCv())
|
||||
{
|
||||
throw new InvalidOperationException("Emgu CV 初始化失败:" + _emguCvError);
|
||||
}
|
||||
|
||||
Stop();
|
||||
|
||||
_timer = new System.Timers.Timer(200);
|
||||
@@ -523,51 +564,53 @@ namespace Camera
|
||||
private void DetectLedZones(Image image)
|
||||
{
|
||||
if (image == null || _ledZones.Count == 0) return;
|
||||
if (_detectionZone.Width <= 0 || _detectionZone.Height <= 0) return;
|
||||
|
||||
try
|
||||
{
|
||||
using (Bitmap bmp = new Bitmap(image))
|
||||
{
|
||||
foreach (var kvp in _ledZones)
|
||||
if (_detectionZone.X < 0 || _detectionZone.Y < 0 ||
|
||||
_detectionZone.X + _detectionZone.Width > bmp.Width ||
|
||||
_detectionZone.Y + _detectionZone.Height > bmp.Height)
|
||||
return;
|
||||
|
||||
using (var imageBgr = new Image<Bgr, byte>(bmp))
|
||||
{
|
||||
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++)
|
||||
foreach (var kvp in _ledZones)
|
||||
{
|
||||
for (int y = zone.Y; y < zone.Y + zone.Height; y++)
|
||||
int index = kvp.Key;
|
||||
Rectangle zone = kvp.Value;
|
||||
|
||||
if (zone.Width <= 0 || zone.Height <= 0) continue;
|
||||
Rectangle ledRect = new Rectangle(
|
||||
_detectionZone.X + zone.X,
|
||||
_detectionZone.Y + zone.Y,
|
||||
zone.Width,
|
||||
zone.Height);
|
||||
|
||||
if (ledRect.X < 0 || ledRect.Y < 0 ||
|
||||
ledRect.X + ledRect.Width > bmp.Width ||
|
||||
ledRect.Y + ledRect.Height > bmp.Height)
|
||||
continue;
|
||||
|
||||
var detectResult = _ledDetector.Detect(imageBgr, ledRect);
|
||||
string result = "";
|
||||
if (detectResult.Item1 == LedState.On)
|
||||
{
|
||||
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++;
|
||||
if (detectResult.Item2 == LedColor.Red)
|
||||
result = "Red";
|
||||
else if (detectResult.Item2 == LedColor.Green)
|
||||
result = "Green";
|
||||
else if (detectResult.Item2 == LedColor.Blue)
|
||||
result = "Blue";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = "Off";
|
||||
}
|
||||
_ledZoneDetectionResults[index] = result;
|
||||
}
|
||||
|
||||
string result = "";
|
||||
if (totalPixels > 0)
|
||||
{
|
||||
if (redPixelCount * 100 / totalPixels > 30)
|
||||
result = "Red";
|
||||
else if (greenPixelCount * 100 / totalPixels > 30)
|
||||
result = "Green";
|
||||
}
|
||||
|
||||
_ledZoneDetectionResults[index] = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="..\packages\EmguCV.3.1.0.1\build\EmguCV.props" Condition="Exists('..\packages\EmguCV.3.1.0.1\build\EmguCV.props')" />
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
@@ -11,6 +12,8 @@
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
@@ -35,6 +38,21 @@
|
||||
<StartupObject />
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Emgu.CV.UI, Version=3.1.0.2282, Culture=neutral, PublicKeyToken=7281126722ab4438, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\EmguCV.3.1.0.1\lib\net30\Emgu.CV.UI.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Emgu.CV.UI.GL, Version=3.1.0.2282, Culture=neutral, PublicKeyToken=7281126722ab4438, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\EmguCV.3.1.0.1\lib\net30\Emgu.CV.UI.GL.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Emgu.CV.World, Version=3.1.0.2282, Culture=neutral, PublicKeyToken=7281126722ab4438, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\EmguCV.3.1.0.1\lib\net30\Emgu.CV.World.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="OpenTK, Version=1.1.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\OpenTK.1.1.2225.0\lib\net20\OpenTK.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="OpenTK.GLControl, Version=1.1.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\OpenTK.GLControl.1.1.2225.0\lib\net20\OpenTK.GLControl.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
@@ -45,9 +63,13 @@
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="ZedGraph, Version=5.1.5.28844, Culture=neutral, PublicKeyToken=02a83cbd123fcd60, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\ZedGraph.5.1.5\lib\ZedGraph.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Camera.cs" />
|
||||
<Compile Include="LedDetector.cs" />
|
||||
<Compile Include="Setting.cs" />
|
||||
<Compile Include="Setting.Designer.cs">
|
||||
<DependentUpon>Setting.cs</DependentUpon>
|
||||
@@ -66,6 +88,8 @@
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
</Compile>
|
||||
<None Include="OpenTK.dll.config" />
|
||||
<None Include="packages.config" />
|
||||
<None Include="Properties\Settings.settings">
|
||||
<Generator>SettingsSingleFileGenerator</Generator>
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
@@ -76,5 +100,19 @@
|
||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="x64\cvextern.dll" />
|
||||
<Content Include="x64\opencv_ffmpeg310_64.dll" />
|
||||
<Content Include="x86\cvextern.dll" />
|
||||
<Content Include="x86\opencv_ffmpeg310.dll" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\packages\EmguCV.3.1.0.1\build\EmguCV.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EmguCV.3.1.0.1\build\EmguCV.props'))" />
|
||||
<Error Condition="!Exists('..\packages\EmguCV.3.1.0.1\build\EmguCV.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EmguCV.3.1.0.1\build\EmguCV.targets'))" />
|
||||
</Target>
|
||||
<Import Project="..\packages\EmguCV.3.1.0.1\build\EmguCV.targets" Condition="Exists('..\packages\EmguCV.3.1.0.1\build\EmguCV.targets')" />
|
||||
</Project>
|
||||
45
Windows/CS/Framework4.0/Camera/Camera/LedDetector.cs
Normal file
45
Windows/CS/Framework4.0/Camera/Camera/LedDetector.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using Emgu.CV;
|
||||
using Emgu.CV.CvEnum;
|
||||
using Emgu.CV.Structure;
|
||||
|
||||
public enum LedState { Off, On }
|
||||
|
||||
public enum LedColor { Unknown, Red, Green, Blue }
|
||||
|
||||
public class LedDetector
|
||||
{
|
||||
public Tuple<LedState, LedColor> Detect(Image<Bgr, byte> image, Rectangle roi)
|
||||
{
|
||||
using (Image<Bgr, byte> subImg = image.GetSubRect(roi))
|
||||
using (Image<Hsv, byte> hsv = subImg.Convert<Hsv, byte>())
|
||||
{
|
||||
Image<Gray, byte> H = hsv[0];
|
||||
Image<Gray, byte> S = hsv[1];
|
||||
Image<Gray, byte> V = hsv[2];
|
||||
|
||||
double avgH = CvInvoke.Mean(H).V0;
|
||||
double avgS = CvInvoke.Mean(S).V0;
|
||||
double avgV = CvInvoke.Mean(V).V0;
|
||||
|
||||
H.Dispose();
|
||||
S.Dispose();
|
||||
V.Dispose();
|
||||
|
||||
bool isOff = (avgS < 38) || (avgV < 55);
|
||||
if (isOff)
|
||||
return new Tuple<LedState, LedColor>(LedState.Off, LedColor.Unknown);
|
||||
|
||||
LedColor color = LedColor.Unknown;
|
||||
if ((avgH >= 0 && avgH <= 10) || (avgH >= 170 && avgH <= 180))
|
||||
color = LedColor.Red;
|
||||
else if (avgH >= 35 && avgH <= 75)
|
||||
color = LedColor.Green;
|
||||
else if (avgH >= 90 && avgH <= 130)
|
||||
color = LedColor.Blue;
|
||||
|
||||
return new Tuple<LedState, LedColor>(LedState.On, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Windows/CS/Framework4.0/Camera/Camera/OpenTK.dll.config
Normal file
25
Windows/CS/Framework4.0/Camera/Camera/OpenTK.dll.config
Normal file
@@ -0,0 +1,25 @@
|
||||
<configuration>
|
||||
<dllmap os="linux" dll="opengl32.dll" target="libGL.so.1"/>
|
||||
<dllmap os="linux" dll="glu32.dll" target="libGLU.so.1"/>
|
||||
<dllmap os="linux" dll="openal32.dll" target="libopenal.so.1"/>
|
||||
<dllmap os="linux" dll="alut.dll" target="libalut.so.0"/>
|
||||
<dllmap os="linux" dll="opencl.dll" target="libOpenCL.so"/>
|
||||
<dllmap os="linux" dll="libX11" target="libX11.so.6"/>
|
||||
<dllmap os="linux" dll="libXi" target="libXi.so.6"/>
|
||||
<dllmap os="linux" dll="SDL2.dll" target="libSDL2-2.0.so.0"/>
|
||||
<dllmap os="osx" dll="opengl32.dll" target="/System/Library/Frameworks/OpenGL.framework/OpenGL"/>
|
||||
<dllmap os="osx" dll="openal32.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
|
||||
<dllmap os="osx" dll="alut.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
|
||||
<dllmap os="osx" dll="libGLES.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
|
||||
<dllmap os="osx" dll="libGLESv1_CM.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
|
||||
<dllmap os="osx" dll="libGLESv2.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
|
||||
<dllmap os="osx" dll="opencl.dll" target="/System/Library/Frameworks/OpenCL.framework/OpenCL"/>
|
||||
<dllmap os="osx" dll="SDL2.dll" target="libSDL2.dylib"/>
|
||||
<!-- XQuartz compatibility (X11 on Mac) -->
|
||||
<dllmap os="osx" dll="libGL.so.1" target="/usr/X11/lib/libGL.dylib"/>
|
||||
<dllmap os="osx" dll="libX11" target="/usr/X11/lib/libX11.dylib"/>
|
||||
<dllmap os="osx" dll="libXcursor.so.1" target="/usr/X11/lib/libXcursor.dylib"/>
|
||||
<dllmap os="osx" dll="libXi" target="/usr/X11/lib/libXi.dylib"/>
|
||||
<dllmap os="osx" dll="libXinerama" target="/usr/X11/lib/libXinerama.dylib"/>
|
||||
<dllmap os="osx" dll="libXrandr.so.2" target="/usr/X11/lib/libXrandr.dylib"/>
|
||||
</configuration>
|
||||
7
Windows/CS/Framework4.0/Camera/Camera/packages.config
Normal file
7
Windows/CS/Framework4.0/Camera/Camera/packages.config
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="EmguCV" version="3.1.0.1" targetFramework="net40" />
|
||||
<package id="OpenTK" version="1.1.2225.0" targetFramework="net40" />
|
||||
<package id="OpenTK.GLControl" version="1.1.2225.0" targetFramework="net40" />
|
||||
<package id="ZedGraph" version="5.1.5" targetFramework="net40" />
|
||||
</packages>
|
||||
@@ -50,8 +50,15 @@ namespace Test
|
||||
{
|
||||
if (button1.Text == "获取摄像头图像")
|
||||
{
|
||||
button1.Text = "停止获取";
|
||||
_camera.Start();
|
||||
try
|
||||
{
|
||||
_camera.Start();
|
||||
button1.Text = "停止获取";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
|
||||
Reference in New Issue
Block a user