Răsfoiți Sursa

20250621 取图流程简化

向羽 孟 1 lună în urmă
părinte
comite
84d1e04b01

+ 20 - 213
TestWork.DLL/CameraClass.cs

@@ -27,8 +27,6 @@ namespace CCDCount.DLL
 
         private List<IDeviceInfo> CamList;      // 相机列表
         private IDevice device = null;          // 相机实例
-        public bool IsGrabbing { get { return _isGrabbing; }}
-        private bool _isGrabbing = false;        // 是否正在取图
         private Thread receiveThread = null;    // 接收图像线程
         private ConcurrentQueue<IFrameOut> FrameOuts = new ConcurrentQueue<IFrameOut>();
         private ConcurrentQueue<byte[]> ImageBytes = new ConcurrentQueue<byte[]>(); //图像接受队列
@@ -224,17 +222,7 @@ namespace CCDCount.DLL
         /// </summary>
         public void StartCamera()
         {
-            //StartEventGetImage();
             StartReceiveFuntion();
-            //StartImageRowsDataFuntion();
-            // 开始采集 
-            if (!_isGrabbing)
-            {
-                _isGrabbing = false;
-                receiveThread.Join();
-                LOG.log(string.Format("{0}:采集线程开启失败", "CameraClass-StartCamera"));
-                return;
-            }
         }
 
         /// <summary>
@@ -242,7 +230,7 @@ namespace CCDCount.DLL
         /// </summary>
         public void StopCamera()
         {
-            StopReceiveAndImageRowsDataFuntion();
+            StopReceiveFuntion();
             //StopEventGetImage();
             if(device!=null)
             {
@@ -256,36 +244,28 @@ namespace CCDCount.DLL
             }
 
         }
-
-        /// <summary>
-        /// 获取第一张缓存中的图像,并把这个图像从缓存中清楚
-        /// </summary>
-        /// <param name="ImageData"></param>
-        public bool GetOnceImage(out byte[] ImageData)
+        public bool GetOnceImage(out IFrameOut IFrameData)
         {
             bool result = false;
-            //初始化图像数据
-            ImageData = null;
-            //判断是否可以给图像数据赋值
-            if(ImageBytes.Count()!=0)
+            int BRet = device.StreamGrabber.GetImageBuffer(1000, out IFrameData);
+            if (BRet == 0 && IFrameData != null) result = true;
+            else return result;
+            if (lastframeNum == -1)
             {
-                ImageData = new byte[ImageBytes.Last().Count()];
-                ImageBytes.TryDequeue(out ImageData);
-                result = true;
+                lastframeNum = IFrameData.FrameNum;
             }
-            return result;
-        }
-
-        public bool GetOnceImage(out IFrameOut IFrameData)
-        {
-            bool result = false;
-            //初始化图像数据
-            IFrameData = null;
-            //判断是否可以给图像数据赋值
-            if (FrameOuts.Count() != 0)
+            else if (lastframeNum == IFrameData.FrameNum - 1)
             {
-                FrameOuts.TryDequeue(out IFrameData);
-                result = true;
+                lastframeNum = IFrameData.FrameNum;
+            }
+            else
+            {
+                //丢帧记录
+                LOG.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
+                    IFrameData.Image.Width, IFrameData.Image.Height, IFrameData.FrameNum - 1, lastframeNum), 6);
+                Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
+                    IFrameData.Image.Width, IFrameData.Image.Height, IFrameData.FrameNum - 1, lastframeNum);
+                lastframeNum = IFrameData.FrameNum;
             }
             return result;
         }
@@ -363,63 +343,6 @@ namespace CCDCount.DLL
 
         #region 私有方法
 
-        /// <summary>
-        /// 图像分割线程
-        /// </summary>
-        private void ImageRowsDataProcess()
-        {
-            IFrameOut frameOut;
-            Queue<byte[]> frameDatas = null;
-            byte[] data = null;
-            while (_isGrabbing)
-            {
-                if(FrameOuts.Count == 0) Thread.Sleep(5);
-                bool BRet = FrameOuts.TryDequeue(out frameOut);
-                if (BRet)
-                {
-                    frameDatas = GetImageRowsData(frameOut);
-                    try
-                    {
-                        while (frameDatas!= null && frameDatas.Count > 0)
-                        {
-                            data = frameDatas.Dequeue();
-                            ImageBytes.Enqueue(data.Clone() as byte[]);
-                            if (ImageBytes.Count > 500)
-                                Console.WriteLine("ImageBytes.Count:" + ImageBytes.Count);
-                            data = null;
-                        }
-                    }
-                    catch (Exception e)
-                    {
-                        LOG.error("CameraClass_ImageRowsDataProcess " + e.Message);
-                        return;
-                    }
-                    device.StreamGrabber.FreeImageBuffer(frameOut);
-                }
-            }
-        }
-
-        /// <summary>
-        /// 开启图像分割
-        /// </summary>
-        private void StartImageRowsDataFuntion()
-        {
-            try
-            {
-                // 开启线程
-                if(_isGrabbing)
-                {
-                    receiveThread = new Thread(ImageRowsDataProcess);
-                    receiveThread.Start();
-                }
-            }
-            catch (Exception ex)
-            {
-                LOG.error("Start thread failed!, " + ex.Message);
-                throw;
-            }
-        }
-
         /// <summary>
         /// 开启采集
         /// </summary>
@@ -427,17 +350,8 @@ namespace CCDCount.DLL
         {
             try
             {
-                // 标志位置位true
-                _isGrabbing = true;
                 device.StreamGrabber.SetImageNodeNum(100);
                 device.StreamGrabber.StartGrabbing();
-
-                // 开启线程
-                receiveThread = new Thread(ReceiveThreadProcess)
-                {
-                    Priority = ThreadPriority.Highest
-                };
-                receiveThread.Start();
             }
             catch (Exception ex)
             {
@@ -447,14 +361,12 @@ namespace CCDCount.DLL
         }
 
         /// <summary>
-        /// 关闭采集线程
+        /// 关闭采集
         /// </summary>
-        private void StopReceiveAndImageRowsDataFuntion()
+        private void StopReceiveFuntion()
         {
             try
             {
-                // 标志位设为false
-                _isGrabbing = false;
                 if (device == null) return;
                 int ret = device.StreamGrabber.StopGrabbing();
                 if (ret != MvError.MV_OK)
@@ -462,8 +374,6 @@ namespace CCDCount.DLL
                     LOG.error(("Stop grabbing failed:{0:x8}", ret));
                     return;
                 }
-                if (receiveThread != null&&receiveThread.IsAlive)
-                    receiveThread.Join();
             }
             catch (Exception ex)
             {
@@ -472,40 +382,6 @@ namespace CCDCount.DLL
             }
         }
 
-        /// <summary>
-        /// 采集线程
-        /// </summary>
-        private void ReceiveThreadProcess()
-        {
-            while (_isGrabbing)
-            {
-                IFrameOut frameOut;
-                int BRet = device.StreamGrabber.GetImageBuffer(100, out frameOut);
-                if (BRet == MvError.MV_OK)
-                {
-                    FrameOuts.Enqueue(frameOut.Clone() as IFrameOut);
-                    if (lastframeNum == -1)
-                    {
-                        lastframeNum = frameOut.FrameNum;
-                    }
-                    else if (lastframeNum == frameOut.FrameNum - 1)
-                    {
-                        lastframeNum = frameOut.FrameNum;
-                    }
-                    else
-                    {
-                        //丢帧记录
-                        LOG.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
-                            frameOut.Image.Width, frameOut.Image.Height, frameOut.FrameNum - 1, lastframeNum), 5);
-                        Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
-                            frameOut.Image.Width, frameOut.Image.Height, frameOut.FrameNum - 1, lastframeNum);
-                        lastframeNum = frameOut.FrameNum;
-                    }
-                    device.StreamGrabber.FreeImageBuffer(frameOut);
-                }
-            }
-        }
-
         /// <summary>
         /// 读取图像像素信息(单行)
         /// </summary>
@@ -539,75 +415,6 @@ namespace CCDCount.DLL
             }
             return PixelDatas;
         }
-
-        /// <summary>
-        /// 帧获取回调事件
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private void FrameGrabedEventHandler(object sender, FrameGrabbedEventArgs e)
-        {
-            if (e.FrameOut.Image.PixelData.Count() == 0)
-            {
-                LOG.log("FrameGrabedEventHandler: PixelData is null", 0);
-                return;
-            }
-            if (!_isGrabbing) return;
-            FrameOuts.Enqueue(e.FrameOut.Clone() as IFrameOut);
-            if (lastframeNum == -1)
-            {
-                lastframeNum = e.FrameOut.FrameNum;
-            }
-            else if (lastframeNum == e.FrameOut.FrameNum - 1)
-            {
-                lastframeNum = e.FrameOut.FrameNum;
-            }
-            else
-            {
-                //丢帧记录
-                LOG.log(string.Format("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
-                    e.FrameOut.Image.Width, e.FrameOut.Image.Height, e.FrameOut.FrameNum - 1, lastframeNum), 5);
-                lastframeNum = e.FrameOut.FrameNum;
-            }
-        }
-
-        /// <summary>
-        /// 开启事件获取图像
-        /// </summary>
-        private void StartEventGetImage()
-        {
-            //ch: 设置合适的缓存节点数量 | en: Setting the appropriate number of image nodes
-            device.StreamGrabber.SetImageNodeNum(50);
-
-            // ch:注册回调函数 | en:Register image callback
-            device.StreamGrabber.FrameGrabedEvent += FrameGrabedEventHandler;
-
-            // ch:开启抓图 || en: start grab image
-            int ret = device.StreamGrabber.StartGrabbing();
-            if (ret != MvError.MV_OK)
-            {
-                LOG.error(string.Format("Start grabbing failed:{0}", ret));
-                return;
-            }
-            Console.ReadLine();
-
-        }
-
-        /// <summary>
-        /// 停止事件获取图像
-        /// </summary>
-        private void StopEventGetImage()
-        {
-            if (device == null) return;
-            int ret = device.StreamGrabber.StopGrabbing();
-            if (ret != MvError.MV_OK)
-            {
-                Console.WriteLine("Stop grabbing failed:{0:x8}", ret);
-                return;
-            }
-            // ch:注册回调函数 | en:Register image callback
-            device.StreamGrabber.FrameGrabedEvent -= FrameGrabedEventHandler;
-        }
         #endregion
     }
 }

+ 3 - 5
TestWork.DLL/MainThreadClass.cs

@@ -18,7 +18,7 @@ namespace CCDCount.DLL
     {
         #region 变量与实例
         Thread SwitchIdentifyImageThread = null;
-        bool IsSwitch = false;
+        private bool IsSwitch = false;
         public ShuLiClass shuLiClass = null;
         CameraClass cameraClass = new CameraClass();
         public string ThisCameraDevice  = string.Empty;
@@ -31,7 +31,7 @@ namespace CCDCount.DLL
         public bool CameraStatic { get { return _CameraStatic; } }
         private bool _CameraStatic = false;
 
-        public bool CameraRunStatic { get { return cameraClass.IsGrabbing; } }
+        public bool CameraRunStatic { get { return IsSwitch; } }
 
         public bool IsOpenLoadThread { get  { return _IsOpenLoadThread; }}
         private bool _IsOpenLoadThread = false;
@@ -423,8 +423,6 @@ namespace CCDCount.DLL
             {
                 if (stopwatch.ElapsedMilliseconds > 1000)
                 {
-                    Console.WriteLine("交换线程-相机获取实例中的图像缓存队列长度:{0}", cameraClass.ImageNum);
-
                     Console.WriteLine("交换线程-图像处理实例中的待识别图像缓存队列长度{0}", shuLiClass.ImageNum);
                     GC.Collect();
                     stopwatch.Restart();
@@ -435,7 +433,7 @@ namespace CCDCount.DLL
                 {
                     if (IFramedata == null)
                         continue;
-                    shuLiClass.SetOnceIdentifyImageData(IFramedata);
+                    shuLiClass.SetOnceIdentifyImageData(IFramedata.Image);
                 }
                 else
                     continue;

+ 7 - 7
TestWork.DLL/ShuLiClass.cs

@@ -22,7 +22,7 @@ namespace CCDCount.DLL
         private List<ActiveObjectClass> activeObjects = new List<ActiveObjectClass>(); // 当前跟踪中的物体
         private List<ActiveObjectClass> historyActiveObjects = new List<ActiveObjectClass>(); // 历史物体
         private ConcurrentQueue<byte[]> ImageBytes = new ConcurrentQueue<byte[]>(); //图像数据队列
-        private ConcurrentQueue<IFrameOut> IFrameDatas = new ConcurrentQueue<IFrameOut>(); //图像数据队列
+        private ConcurrentQueue<IImage> IFrameDatas = new ConcurrentQueue<IImage>(); //图像数据队列
         private Thread IdentifyImageProcessThread = null; // 识别线程
         private bool IsIdentify = false; //线程是否开始识别的标志
         private long currentLine = 0; //行数记录
@@ -90,12 +90,12 @@ namespace CCDCount.DLL
         /// <param name="ImageWidth">图像宽</param>
         /// <param name="currentLine">当前行数</param>
         /// <returns>检测到的物体总数</returns>
-        public bool ProcessImageSequence(IFrameOut image)
+        public bool ProcessImageSequence(IImage image)
         {
             bool result = false;
-            for (int i = 0;i<image.Image.Height;i++)
+            for (int i = 0;i<image.Height;i++)
             {
-                result = ProcessLine(image.Image,i);
+                result = ProcessLine(image,i);
                 currentLine += 1;
             }
             return result;
@@ -219,9 +219,9 @@ namespace CCDCount.DLL
         /// 向识别队列添加一个数据
         /// </summary>
         /// <param name="items"></param>
-        public void SetOnceIdentifyImageData(IFrameOut items)
+        public void SetOnceIdentifyImageData(IImage items)
         {
-            IFrameDatas.Enqueue(items.Clone() as IFrameOut);
+            IFrameDatas.Enqueue(items.Clone() as IImage);
         }
 
         /// <summary>
@@ -855,7 +855,7 @@ namespace CCDCount.DLL
         private void IdentifyImageProcess()
         {
             //Stopwatch stopwatch = Stopwatch.StartNew();
-            IFrameOut IframeData = null;
+            IImage IframeData = null;
             while (IsIdentify)
             {
                 //判断队列中是否有数据

+ 2 - 17
TestWork/Forms/MainForm.cs

@@ -20,21 +20,6 @@ namespace CCDCount.Forms
         bool meunStatic = false;
         //当前显示窗体
         Form showFrom = null;
-        //List<(CameraConfig CameraConfigValue, ShuLiConfigClass ShuLiConfigValue)> Configs =
-        //    new List<(CameraConfig CameraConfigValue, ShuLiConfigClass ShuLiConfigValue)>()
-        //    {
-        //        (new CameraConfig()
-        //        {
-        //            CameraSNNum = "L39879764",
-        //            CameraName = "MV-CL042-91GC",
-        //            IsOpenLoad = true,
-        //            CamerNo = 0
-        //        },new ShuLiConfigClass()
-        //        {
-        //            Channel = 8,
-        //            CameraSN = "L39879764",
-        //        }),
-        //    };
         List<(CameraConfig CameraConfigValue, ShuLiConfigClass ShuLiConfigValue)> Configs =
             new List<(CameraConfig CameraConfigValue, ShuLiConfigClass ShuLiConfigValue)>();
         #endregion
@@ -227,8 +212,8 @@ namespace CCDCount.Forms
                 //启动单相机实例的全部线程
                 if(!LsMainThread[i].StartMianThread(Configs.Select(o => o.CameraConfigValue).ToList().Where(o=>o.CameraSNNum == LsMainThread[i].ThisCameraSN).First()))
                 {
-                    LOG.error(LsMainThread[i].ThisCamerName + "启动失败");
-                    MessageBox.Show(LsMainThread[i].ThisCamerName + "启动失败");
+                    LOG.error(LsMainThread[i].ThisCameraDevice+ "_"+ LsMainThread[i].ThisCameraSN + "启动失败");
+                    MessageBox.Show(LsMainThread[i].ThisCameraDevice + "_" + LsMainThread[i].ThisCameraSN + "启动失败");
                     return;
                 }
                 LsMainThread[i].WorkerToFrom +=  Worker_MianThreadToFrom;