【问题标题】:How to view and record video simultaneously using EmguCV?如何使用 EmguCV 同时查看和录制视频?
【发布时间】:2019-02-04 22:20:48
【问题描述】:

我正在开发类似于监控系统的 Windows Form App,并尝试同时捕获和保存视频。我可以从相机查看视频,但尝试保存视频时。生成一个空的视频文件。

由于我是 emguCV 的新手,所以我被卡住了,无法解决。

这是我尝试过的

 VideoCapture videoCapture;
        double totalFrames;
        double fps;
        VideoWriter videoWriter;
        DateTime dateTime;
        int i;

        public Body()
        {
            InitializeComponent();
            i = 0;


                if (videoCapture == null)
                {
                    dateTime = new DateTime();
                    dateTime = DateTime.Now;
                    videoCapture = new Emgu.CV.VideoCapture(0);
                }
                videoCapture.ImageGrabbed += VideoCapture_ImageGrabbed;
                videoCapture.Start();



        }

        private void VideoCapture_ImageGrabbed(object sender, EventArgs e)
        {

            Mat m = new Mat();
            videoCapture.Retrieve(m);
            pictureBox1.Image = m.ToImage<Bgr, byte>().Bitmap;

            totalFrames = videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameCount);
            fps = videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps);

            if (videoCapture == null)
            {
                return;
            }
            else
            {

                int fourcc = Convert.ToInt32(videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FourCC));
                int frameHeight = Convert.ToInt32(videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight));
                int frameWidth = Convert.ToInt32(videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth));
                string destination = "C:\\Users\\ITNOA\\Desktop\\savedVideoDHS\\" + i+".mp4";
                videoWriter = new VideoWriter(destination, fourcc, fps, new Size(frameWidth, frameHeight), true);
                videoWriter.Write(m);
            }


        }

private void videoSavingtimer_Tick(object sender, EventArgs e)
        {
            videoCapture = null;
            i++;
            if (videoWriter.IsOpened)
            {
                videoWriter.Dispose();

            }
        } 

有什么帮助吗?

谢谢。

【问题讨论】:

    标签: c# video save video-capture emgucv


    【解决方案1】:

    这是使用 emgu cv 同时查看和保存视频的方法。

    public Body()
            {
                InitializeComponent();
    
                    if (videoCapture == null)
                    {
    
                        videoCapture = new Emgu.CV.VideoCapture(0);
                    }
                    videoCapture.ImageGrabbed += VideoCapture_ImageGrabbed;
                    videoCapture.Start();
    
    
    
            }
    
            private void VideoCapture_ImageGrabbed(object sender, EventArgs e)
            {
                if (fileChanged)
                {
                    totalFrames = videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameCount);
                    fps = videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps);
                    int fourcc = Convert.ToInt32(videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FourCC));
                    int frameHeight = Convert.ToInt32(videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight));
                    int frameWidth = Convert.ToInt32(videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth));
                    string destination = "C:\\Users\\ITNOA\\Desktop\\savedVideoDHS\\" + i + ".avi";
                    videoWriter = new VideoWriter(destination, VideoWriter.Fourcc('I', 'Y', 'U', 'V'), fps, new Size(frameWidth, frameHeight), true);
                    fileChanged = false;
                }
    
    
                Mat m = new Mat();
                videoCapture.Retrieve(m);
                pictureBox1.Image = m.ToImage<Bgr, byte>().Bitmap;
                videoWriter.Write(m);
            }
    

    在上面的代码中,由于我需要将视频保存到多个文件中,我正在重新初始化 video-writer。对于单个文件,它应该初始化一次。

    我尝试了多个压缩代码,但以下对我有用

    压缩码 VideoWriter.Fourcc('I', 'Y', 'U', 'V')

    videoWriter = new VideoWriter(destination, VideoWriter.Fourcc('I', 'Y', 'U', 'V'), fps, new Size(frameWidth, frameHeight), true);

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      相关资源
      最近更新 更多