EmguCV学习——视频与图片互转

其实视频转图片在上篇文章中已经有些眉目了,其实就是按帧读取视频,然后把帧保存就ok。然后自己再加个进度条美化一下。。。这代码简单易懂,还是直接上代码吧。

视频转图片

 1         /// <summary>
 2         /// 视频转换为图片
 3         /// </summary>
 4         /// <param name="path"></param>
 5         public void Video2Image(object path)
 6         {
 7             try
 8             {
 9                 //判断文件夹是否存在
10                 if (!Directory.Exists(filepath + filename + "\"))
11                 {
12                     try
13                     {
14                         //不存在 创建文件夹
15                         Directory.CreateDirectory(filepath + filename + "\");
16                     }
17                     catch { }
18                 }
19 
20                 IntPtr CatchFrame = CvInvoke.cvCreateFileCapture(path.ToString());
21                 // 得到总帧数
22                 var count = CvInvoke.cvGetCaptureProperty(CatchFrame, Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_COUNT);
23                 // 视频宽度
24                 int wd = (int)CvInvoke.cvGetCaptureProperty(CatchFrame, Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH);
25                 // 视频高度
26                 int hg = (int)CvInvoke.cvGetCaptureProperty(CatchFrame, Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT);
27                 //// 当前帧位置
28                 //CvInvoke.cvGetCaptureProperty(CatchFrame, Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES);
29                 //// 帧频
30                 //CvInvoke.cvGetCaptureProperty(CatchFrame, Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FPS);
31 
32                 IntPtr FrameImg;
33                 int i = 0;
34                 
35                 IntPtr grayImg = CvInvoke.cvCreateImage(new Size(wd, hg), Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_8U, 1);
36                 while ((FrameImg = CvInvoke.cvQueryFrame(CatchFrame)) != IntPtr.Zero)
37                 {
38                     //此处我是转为灰度图,保存灰度图,小伙伴可以直接保存FrameImg
39                     CvInvoke.cvCvtColor(FrameImg, grayImg, Emgu.CV.CvEnum.COLOR_CONVERSION.BGR2GRAY);
40                    
41                     string picname = filepath + filename + "\image" + (++i) + ".jpg";
42                     
43                     CvInvoke.cvSaveImage(picname, grayImg, IntPtr.Zero);
44 
45                 }
46                 
47             }
48             catch (Exception ex)
49             {
50                 MessageBox.Show(ex.ToString());
51             }
52 
53         }    

好吧,重要的是图片转视频,这里涉及到解码器。我需要转换的是mp4格式的视频,所以我选择的解码器为Xvid。

 1         /// <summary>
 2         /// 图片转换为视频
 3         /// </summary>
 4         public void Image2Video()
 5         {
 6             try
 7             {
 8                 var files = Directory.GetFiles(filepath, "*.jpg");
 9                 int count = files.Count();
10                 int isColor = 1;
11                 //帧频
12                 int fps = 5;
13                 int i = 0;
14                 string picname = files[0];
15                 Bitmap map = new Bitmap(picname);
16                 int frameW = map.Width;
17                 int frameH = map.Height;
18                 string videoname = filepath + "\out.mp4";
19                 var writer = CvInvoke.cvCreateVideoWriter(videoname, CvInvoke.CV_FOURCC('X', 'V', 'I', 'D'), fps, new System.Drawing.Size(frameW, frameH), isColor);
20                 map.Dispose();
21                 CvInvoke.cvNamedWindow("mainWin");
22                 while (i < count)
23                 {
24                     picname = files[i];
25                     var img = CvInvoke.cvLoadImage(picname, Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);
26                     if (img == null)
27                     {
28                         CvInvoke.cvReleaseImage(ref img);
29                         continue;
30                     }
31                     CvInvoke.cvShowImage("mainWin", img);
32                     bool flag = CvInvoke.cvWriteFrame(writer, img);
33                     if (!flag)
34                     {
35                         CvInvoke.cvReleaseImage(ref img);
36                         continue;
37                     }
38                     CvInvoke.cvWaitKey(20);
39                     CvInvoke.cvReleaseImage(ref img);
40                     i++;
41                 }
42                 CvInvoke.cvReleaseVideoWriter(ref writer);
43                 CvInvoke.cvDestroyWindow("mainWin");
44             }
45             catch (Exception e)
46             {
47                 MessageBox.Show(e.ToString());
48             }
49         }            

That's all!

种一棵树最好的时间是十年前,其次是现在。

原文地址:https://www.cnblogs.com/jixin/p/4727592.html