【问题标题】:how to make a movie with 30 frames c#, opencvsharp如何制作30帧的电影c#,opencvsharp
【发布时间】:2018-10-04 06:50:48
【问题描述】:

我想制作一部 30 帧的电影。

用相机花一分钟看视频,视频长度只有 42 秒。

作为时间测量的结果,输出为 21 帧。

如何制作 30 帧视频?

下面是运行时间的描述。

....

00:00:01.0339097
00:00:01.0788484
00:00:01.1256316
00:00:01.1741762
00:00:01.2210662
00:00:01.2660053
00:00:01.3138742
00:00:01.3605831
00:00:01.4084542
00:00:01.4543680
00:00:01.5004309
00:00:01.5492773
00:00:01.5951540
00:00:01.6420466
00:00:01.6899155
00:00:01.7348539
00:00:01.7827228
00:00:01.8282291
00:00:01.8760989
00:00:01.9239678
00:00:01.9718930
....

以下是源代码。

    private void StartTimer()
    {
        if (writer == null)
        {
            Utilitys.TStimeD = DateTime.Now;
            TStimeS = Utilitys.TStimeD.ToString("yyyyMMddHHmmss");
            aviPath = TStimeS + ".avi";
            writer = Cv.CreateVideoWriter(aviPath, "XVID", 30, new CvSize(640, 240));
        }
        timer1.Interval = 33;
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

        if (_captureInProgress)
        {
            _image = _capture.QueryFrame();
            try { 

                writer.WriteFrame(_image);
                Console.WriteLine((DateTime.Now - Utilitys.TStimeD).ToString());
                //Cv.ReleaseImage(_image);
            }
            catch(System.AccessViolationException ex)
            {
                Console.WriteLine("System.AccessViolationException");
            }catch(System.ObjectDisposedException ex)
            {
                Console.Write("System.ObjectDisposedException");
            }
        }
    }

【问题讨论】:

    标签: c# opencv video camera opencvsharp


    【解决方案1】:

    我不会为此使用计时器,因为即使绝对精确地每秒调用 30 次,也没有时间用于图像抓取和消费/显示。

    我会自己实施节流。

        async void GrabImages(CancellationToken ct, int FPS)
        {
            var sw = new Stopwatch();
            sw.Start();
    
            var imageSlotInMS = 1000.0 / FPS;
    
            for (ulong grabbed = 0; !ct.IsCancellationRequested;)
            {
                var expectedTimePosInMS = grabbed * imageSlotInMS;
    
                var time2WaitInMS = expectedTimePosInMS - sw.Elapsed.TotalMilliseconds;
                if (time2WaitInMS > 0)
                    await Task.Delay((int)time2WaitInMS);
    
                var image = await GrabImage();
                DisplayImage(image);
                grabbed++;
            }
    
            sw.Stop();
        }
    

    在最坏的情况下,如果系统性能不允许,您至少会以最大可能的 FPS 运行。

    这种方法的另一个优点是您可以在不同的线程上执行图像抓取,这将使主 (UI) 线程免于执行此任务并使其表现更流畅。

    【讨论】:

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