EmguCV學習——視頻與圖片互轉

其實視頻轉圖片在上篇文章中已經有些眉目了,其實就是按幀讀取視頻,而後把幀保存就ok。而後本身再加個進度條美化一下。。。這代碼簡單易懂,仍是直接上代碼吧。ide

視頻轉圖片spa

 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。code

 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!視頻

種一棵樹最好的時間是十年前,其次是如今。blog

相關文章
相關標籤/搜索