Ver Fonte

20260617001 部分类结构优化,分类优化,相机增加帧数软限制。

向羽 孟 há 4 dias atrás
pai
commit
8bc964f108

+ 2 - 1
MvvmScaffoldFrame48.DLL/ImageAlgorithm/ProcessingAlgorithm_CCDShuLi.cs → MvvmScaffoldFrame48.DLL/AlgorithmProcess/ProcessingAlgorithm_CCDShuLi.cs

@@ -13,8 +13,9 @@ using System.Linq;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
+using MvvmScaffoldFrame48.DLL.ImageAlgorithm;
 
-namespace MvvmScaffoldFrame48.DLL.ImageAlgorithm
+namespace MvvmScaffoldFrame48.DLL.AlgorithmProcess
 {
     public class ProcessingAlgorithm_CCDShuLi : IImageProcessingAlgorithmHikVision
     {

+ 4 - 3
MvvmScaffoldFrame48.DLL/ImageAlgorithm/ProcessingAlgorithm.cs → MvvmScaffoldFrame48.DLL/AlgorithmProcess/ProcessingAlgorithm_Default.cs

@@ -1,6 +1,7 @@
 //实现线程算法示例
 using MvCameraControl;
 using MvvmScaffoldFrame48.DLL.ConfigTools;
+using MvvmScaffoldFrame48.DLL.ImageAlgorithm;
 using MvvmScaffoldFrame48.Model.ResultModel;
 using MvvmScaffoldFrame48.Model.StorageModel.Configs;
 using System;
@@ -10,9 +11,9 @@ using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
 
-namespace MvvmScaffoldFrame48.DLL.ImageAlgorithm
+namespace MvvmScaffoldFrame48.DLL.AlgorithmProcess
 {
-    public class ProcessingAlgorithm : IImageProcessingAlgorithmHikVision
+    public class ProcessingAlgorithm_Default : IImageProcessingAlgorithmHikVision
     {
         #region 变量与实例
         //参数:阈值
@@ -24,7 +25,7 @@ namespace MvvmScaffoldFrame48.DLL.ImageAlgorithm
         //参数:滤波类型
         private string _filterType = "Gaussian";
         //算法名称
-        public string AlgorithmName => "ProcessingAlgorithm";
+        public string AlgorithmName => "ProcessingAlgorithm_Default";
         #endregion
 
         #region 私有方法

+ 80 - 18
MvvmScaffoldFrame48.DLL/CameraTools/HikCamera.cs

@@ -7,6 +7,7 @@ using MvvmScaffoldFrame48.Model.StorageModel.Configs;
 using MvvmScaffoldFrame48.Model.StorageModel.HikVisionCamera;
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -35,7 +36,7 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
         /// 设备事件委托
         /// </summary>
         public event EventHandler<FrameGrabbedEventArgs> FrameGrabbedEvent;
-
+        private DateTime _lastFrame = DateTime.MinValue;
         #endregion
 
         #region 变量
@@ -43,10 +44,48 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
         /// 最后一次帧号记录
         /// </summary>
         private long lastframeNum = -1;
+
+        /// <summary>
+        /// 帧率限制下最小帧间隔(单位ms)
+        /// </summary>
+        private double minIntervalMs;
+
+        /// <summary>
+        /// 是否进行取图帧率限制
+        /// </summary>
+        private bool isFrameRateLimit = false;
+        public bool IsFrameRateLimit
+        {
+            get 
+            {
+                return isFrameRateLimit;
+            }
+            set
+            {
+                isFrameRateLimit = value;
+            }
+        }
+
+        /// <summary>
+        /// 取图帧率限制
+        /// </summary>
+        private int frameRateLimit;
+        public int FrameRateLimit
+        {
+            get
+            {
+                return frameRateLimit;
+            }
+            set
+            {
+                frameRateLimit = value;
+                minIntervalMs = 1000.0 / frameRateLimit;
+            }
+        }
         #endregion
 
         #region 构造函数
-        
+
         /// <summary>
         /// 相机构造函数 不加载相机
         /// </summary>
@@ -183,7 +222,12 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
             return Blresult;
         }
 
-        public int LoadCameraConfig(LinescanCameraParameterModel cameraParameter)
+        /// <summary>
+        /// 加载线阵相机参数
+        /// </summary>
+        /// <param name="cameraParameter"></param>
+        /// <returns></returns>
+        public int LoadLineCameraConfig(LinescanCameraParameterModel cameraParameter)
         {
             int result = 0;
             _device.Parameters.SetFloatValue("ExposureTime", cameraParameter.ExposureTimeValue);
@@ -194,7 +238,12 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
             return result;
         }
 
-        public int LoadCameraConfig(string cameraParameter)
+        /// <summary>
+        /// 加载线阵相机参数(从序列化字符串读取)
+        /// </summary>
+        /// <param name="cameraParameter"></param>
+        /// <returns></returns>
+        public int LoadLineCameraConfig(string cameraParameter)
         {
             int result = 0;
             LinescanCameraParameterModel cameraParameterModel = XMLReadWrite.DeserializeFromString<LinescanCameraParameterModel>(cameraParameter);
@@ -298,24 +347,37 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
         /// </summary>
         private void FrameGrabbedEventHandler(object sender,FrameGrabbedEventArgs e)
         {
-            // 触发外部注册的事件
-            FrameGrabbedEvent?.Invoke(this, e);
-            if (lastframeNum == -1)
-            {
-                lastframeNum = e.FrameOut.FrameNum;
-            }
-            else if (lastframeNum == e.FrameOut.FrameNum - 1)
+            if(isFrameRateLimit && frameRateLimit>0)
             {
-                lastframeNum = e.FrameOut.FrameNum;
+                TimeSpan elapsed = DateTime.Now - _lastFrame;
+                if (elapsed.TotalSeconds > minIntervalMs)
+                {
+                    // 触发外部注册的事件
+                    FrameGrabbedEvent?.Invoke(this, e);
+                    _lastFrame = DateTime.Now;
+                }
             }
             else
             {
-                //丢帧记录
-                TxtLog.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
-                    e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum, lastframeNum), 6);
-                Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
-                    e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum, lastframeNum);
-                lastframeNum = e.FrameOut.FrameNum;
+                // 触发外部注册的事件
+                FrameGrabbedEvent?.Invoke(this, e);
+                if (lastframeNum == -1)
+                {
+                    lastframeNum = e.FrameOut.FrameNum;
+                }
+                else if (lastframeNum == e.FrameOut.FrameNum - 1)
+                {
+                    lastframeNum = e.FrameOut.FrameNum;
+                }
+                else
+                {
+                    //丢帧记录
+                    TxtLog.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
+                        e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum, lastframeNum), 6);
+                    Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
+                        e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum, lastframeNum);
+                    lastframeNum = e.FrameOut.FrameNum;
+                }
             }
             e.FrameOut.Dispose();
         }

+ 4 - 4
MvvmScaffoldFrame48.DLL/ConfigTools/ConfigService.cs

@@ -85,7 +85,7 @@ namespace MvvmScaffoldFrame48.DLL.ConfigTools
                 //测试用代码,此处应为创建空配置,后续由图形界面控制新建配置
                 _camerasConfig = new List<CameraProcessConfigModel>()
                 {
-                    CreateCameraConfiguration(0, "ProcessingAlgorithm_CCDShuLi"),
+                    CreateCameraConfiguration(0, "CCDShuLi"),
                 };
             }
         }
@@ -113,8 +113,8 @@ namespace MvvmScaffoldFrame48.DLL.ConfigTools
 
             switch (ProcessingAlgorithm)
             {
-                case "ProcessingAlgorithm":
-                    config.ProcessingAlgorithmName = "ProcessingAlgorithm";
+                case "Default":
+                    config.ProcessingAlgorithmName = "ProcessingAlgorithm_Default";
                     config.AlgorithmParameters = XMLReadWrite.SerializeToString(new ProcessingAlgorithmConfigModel()
                     {
                         Threshold = 0.5,
@@ -123,7 +123,7 @@ namespace MvvmScaffoldFrame48.DLL.ConfigTools
                         FilterType = "FilterType"
                     });
                     break;
-                case "ProcessingAlgorithm_CCDShuLi":
+                case "CCDShuLi":
                     config.ProcessingAlgorithmName = "ProcessingAlgorithm_CCDShuLi";
                     config.AlgorithmParameters = XMLReadWrite.SerializeToString(new ShuLiConfigClassModel());
                     break;

+ 2 - 1
MvvmScaffoldFrame48.DLL/ImageAlgorithm/ImageProcessingAlgorithmHikVisionFactory.cs

@@ -1,4 +1,5 @@
 // ImageProcessingAlgorithmFactory.cs 识别算法工厂,用于注册算法实现
+using MvvmScaffoldFrame48.DLL.AlgorithmProcess;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -21,7 +22,7 @@ namespace MvvmScaffoldFrame48.DLL.ImageAlgorithm
         static ImageProcessingAlgorithmHikVisionFactory()
         {
             // 注册所有可用的算法实现
-            RegisterAlgorithm<ProcessingAlgorithm>("ProcessingAlgorithm");
+            RegisterAlgorithm<ProcessingAlgorithm_Default>("ProcessingAlgorithm_Default");
             RegisterAlgorithm<ProcessingAlgorithm_CCDShuLi>("ProcessingAlgorithm_CCDShuLi");
             // 可以继续注册更多算法...
         }

+ 2 - 2
MvvmScaffoldFrame48.DLL/MvvmScaffoldFrame48.Dll.csproj

@@ -90,7 +90,7 @@
     <Compile Include="ImageAlgorithm\BoundRectangleClass.cs" />
     <Compile Include="ImageAlgorithm\ImageAlgorithmTools.cs" />
     <Compile Include="ImageAlgorithm\OpenCvImageAlgorihm.cs" />
-    <Compile Include="ImageAlgorithm\ProcessingAlgorithm_CCDShuLi.cs" />
+    <Compile Include="AlgorithmProcess\ProcessingAlgorithm_CCDShuLi.cs" />
     <Compile Include="LogTools\TxtLog.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="SystemTools\AutoStartHelper.cs" />
@@ -101,7 +101,7 @@
     <Compile Include="ThreadManager\CommunicationThread.cs" />
     <Compile Include="ImageAlgorithm\IImageProcessingAlgorithmHikVision.cs" />
     <Compile Include="ImageAlgorithm\ImageProcessingAlgorithmHikVisionFactory.cs" />
-    <Compile Include="ImageAlgorithm\ProcessingAlgorithm.cs" />
+    <Compile Include="AlgorithmProcess\ProcessingAlgorithm_Default.cs" />
     <Compile Include="ThreadManager\ThreadManager.cs" />
     <Compile Include="UserManager.cs" />
     <Compile Include="WindowsTools\OnScreenKeyboardTools.cs" />

+ 2 - 1
MvvmScaffoldFrame48.DLL/ThreadManager/CameraGroup.cs

@@ -116,6 +116,7 @@ namespace MvvmScaffoldFrame48.DLL.ThreadManager
 
             IsRunning = false;
             Camera.StopReceiveFuntionSetFrameGrabedEvent();
+            Camera.FrameGrabbedEvent -= CameraCaptureLoop;
             Console.WriteLine($"相机组 {CameraId} 已停止");
         }
 
@@ -196,7 +197,7 @@ namespace MvvmScaffoldFrame48.DLL.ThreadManager
         { 
             if(Configuration != null&& !string.IsNullOrEmpty(Configuration.CameraParameters))
             {
-                Camera.LoadCameraConfig(Configuration.CameraParameters);
+                Camera.LoadLineCameraConfig(Configuration.CameraParameters);
             }
         }
 

+ 10 - 6
MvvmScaffoldFrame48.DLL/ThreadManager/ThreadManager.cs

@@ -18,16 +18,21 @@ namespace MvvmScaffoldFrame48.DLL.ThreadManager
 {
     public class ThreadManager
     {
-        #region 变量与实例
+        #region 实例
+        //线程管理中心单例实现
         public static ThreadManager Instance => _instance;
-
+        private static ThreadManager _instance = new ThreadManager();
+        //配置管理单例获取
+        private ConfigService configService = ConfigService.Instance;
         //相机组
         private readonly CameraGroup[] _cameraGroups = new CameraGroup[1];
         // 通信线程
         private readonly CommunicationThread _communicationThread;
 
-        private static ThreadManager _instance = new ThreadManager();
-        private ConfigService configService = ConfigService.Instance;
+        #endregion
+
+        # region 变量
+        
         #endregion
 
         #region 构造函数
@@ -38,12 +43,11 @@ namespace MvvmScaffoldFrame48.DLL.ThreadManager
         {
             // 加载相机配置
             configService.LoadAsync();
-            configService.SaveAsync();
 
             // 获取相机列表
             HikVision.GetCameraList(out List<IDeviceInfo> cameraInfoList);
 
-            // 初始化四个相机组
+            // 初始化相机组
             for (int i = 0; i < _cameraGroups.Count(); i++)
             {
                 // 加载对应相机位的配置参数