【问题标题】:Find Image at specific second in a video在视频中的特定秒查找图像
【发布时间】:2017-07-12 06:22:11
【问题描述】:

我正在尝试在特定秒内从视频中提取图像。例如,如果我使用 3 秒,则图像应在每 3 秒后从视频文件中提取一次。我正在使用 emgu-cv 来实现它,但问题是,它从视频中获取所有帧。我不明白如何设置秒。 这是我的代码:

 private List<Image<Bgr, Byte>> GetVideoFrames(String Filename)
        {
            try
            {
                List<Image<Bgr, Byte>> image_array = new List<Image<Bgr, Byte>>();
               _capture = new Capture(Filename);


                bool Reading = true;
                int frameNumber = 10;
                int count = 0;
                while (Reading)
                {
                    Image<Bgr, Byte> frame = _capture.QueryFrame();
                    if (frame != null)
                    {
                        image_array.Add(frame.Copy());
                        //if(count>=frameNumber && count%frameNumber==0)
                        //{
                            image_array[count].Save(@"D:\SVN\Video Labeling\Images\"+count+".png");
                        //}
                        count++;
                    }
                    else
                    {
                        Reading = false;
                    }

                }

                return image_array;
            }
            catch (Exception ex)
            {

                throw;
            }
        }

【问题讨论】:

    标签: c# image video-processing emgucv


    【解决方案1】:

    如果你想在 EMGUCV 中得到答案,那么这里就是

        private List<Image<Bgr, Byte>> GetVideoFrames(String Filename,int secondsToSkip)
        {
            try
            {
                List<Image<Bgr, Byte>> image_array = new List<Image<Bgr, Byte>>();
                _capture = new Capture(Filename);
    
                double fps = _capture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS);
    
                Image<Bgr, Byte> frame = null;
                bool reading = true;
                double framesToSkip = secondsToSkip * fps;
    
                for (int count=1; reading; count++)
                {
                    if(count%framesToSkip != 0)
                        _capture.QuerySmallFrame(); 
                    else
                    { 
                        frame = _capture.QueryFrame();
                        reading = (frame != null);
                        if (reading)
                        {
                            image_array.Add(frame.Copy());
                            image_array[count].Save(@"D:\SVN\Video Labeling\Images\" + count + ".png");
                        }
                    }
    
                }
    
                return image_array;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    

    【讨论】:

      【解决方案2】:

      FFMpeg 非常适合视频处理和转换。不用emgucv,用ffmpeg就能轻松搞定。

      下面还提供了带有 emgucv 的版本

      你需要的参数是here

      示例代码

      string pathToVideo = @"<enter the video path here>";
      int xSeconds = 10;
      string imageName = @"D:\SVN\Video Labeling\Images\"+count+".png";
      
      Process process = new Process();
      process.StartInfo.RedirectStandardOutput = true;
      process.StartInfo.RedirectStandardError = true;
      process.StartInfo.FileName = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +
                          @"\bin\ffmpeg.exe";
      
      process.StartInfo.Arguments = String.Format("-i {0} -vf fps=1/{1} {2}%04d.bmp",pathToVideo,xSeconds,imageName);
      
      
      process.StartInfo.UseShellExecute = false;
      process.StartInfo.CreateNoWindow = true;
      process.Start();
      

      【讨论】:

        猜你喜欢
        • 2017-05-11
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多