在视频中的特定秒处查找图像

时间:2017-07-12 06:22:11

标签: c# image video-processing emgucv

我试图在特定的秒钟从视频中提取图像。例如,如果我使用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;
            }
        }

2 个答案:

答案 0 :(得分: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;
        }
    }

答案 1 :(得分:0)

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();