大华摄像头应用opencvsharp进行实时监控、监控录像保存和播放

1.配置大华摄像机参数
 
检查RTSP端口号

 

修改分辨率,在这里我们主要设置【辅码流】

大华RTSP协议解释
访问主码流
rtsp://admin:tld123456@192.168.1.10:554/cam/realmonitor?channel=1&subtype=0
访问辅码流
rtsp://admin:tld123456@192.168.1.10:554/cam/realmonitor?channel=1&subtype=1
 
2.opencvsharp引用:

 

3.opencvsharp代码示例:

 

获取视频流:
        private void savestream()
        {
                VideoCapture cap = new VideoCapture();
                cap.Open("rtsp://admin:tld123456@192.168.1.10:554/cam/realmonitor?channel=1&subtype=1");
                Mat Camera_GImg = new Mat();
                string path = System.Environment.CurrentDirectory+"\\video\\666.avi";
                OpenCvSharp.Size sz = new OpenCvSharp.Size(640,480);
                VideoWriter vw = new VideoWriter(path, FourCCCalcurator.Run('X', 'V', 'I', 'D'), cap.Fps, sz, true);
            while (true)
            {
                cap.Read(Camera_GImg);
                if (Camera_GImg.Empty())
                { break; }
                else
                { vw.Write(Camera_GImg); }

            }          
        }
播放监控录像:
   private void readvideo()
        {
            byte[] image_byte = new Byte[2048];
            VideoCapture cap = new VideoCapture();
            string path = System.Environment.CurrentDirectory + "\\video\\666.avi";
            cap.Open(path);
            while (true)
            {
                Mat Camera_GImg = new Mat();
                cap.Read(Camera_GImg);
                if (Camera_GImg.Empty())
                {
                    isopen = !isopen;
                    break;
                }
                else
                {
                    image_byte = Camera_GImg.ToBytes();
                    #region 图片嵌入picturebox
                    MemoryStream ms = new MemoryStream(image_byte);
                    ms.Position = 0;
                    img = System.Drawing.Image.FromStream(ms);
                    this.Invoke((EventHandler)delegate//将分线程对主线程的调用委托给主线程
                    {
                        pictureBox1.BackgroundImage = img;
                        pictureBox1.Refresh();
                    });
                    #endregion
                    Camera_GImg.Release();//释放,别等到gc来回收,太占内存
                }
            }
        }

 

posted @ 2023-12-16 16:12  四月柳巷  阅读(584)  评论(0编辑  收藏  举报