日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

3 、OpenCvSharp 图片转视频

發布時間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 3 、OpenCvSharp 图片转视频 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫作原因:前段時間做圖像識別,一朋友用python將一張圖片合成了一段視頻,用于測試使用,先就c#做一個圖片轉視頻的記錄,使用的是Opencvsharp,如有不對的地方,請多多指教。

一、準備

需要使用到的對象為VideoWriter

?其中重載函數有好幾個,用來對應不同的需求。

?二、參數解釋

參數由很多,筆者用下面這個重載函數對這些參數做講解

/// <summary>/// Creates video writer structure. /// </summary>/// <param name="fileName">Name of the output video file. </param>/// <param name="fourcc">4-character code of codec used to compress the frames. For example, "PIM1" is MPEG-1 codec, "MJPG" is motion-jpeg codec etc. /// Under Win32 it is possible to pass null in order to choose compression method and additional compression parameters from dialog. </param>/// <param name="fps">Frame rate of the created video stream. </param>/// <param name="frameSize">Size of video frames. </param>/// <param name="prms">The `params` parameter allows to specify extra encoder parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .)/// see cv::VideoWriterProperties</param>/// <returns></returns>public VideoWriter(string fileName, FourCC fourcc, double fps, Size frameSize, int[] prms){FileName = fileName ?? throw new ArgumentNullException(nameof(fileName));Fps = fps;FrameSize = frameSize;NativeMethods.HandleException(NativeMethods.videoio_VideoWriter_new4(fileName, (int)fourcc, fps, frameSize, prms, prms.Length, out ptr));if (ptr == IntPtr.Zero)throw new OpenCvSharpException("Failed to create VideoWriter");}

fileName:就是生成的視頻文件的名字,可由使用者自定義。

fourcc:使用的圖片格式,像mjpg。

fps:設置生成的視頻的幀率。

frameSize:生成的視頻每一幀的大小。

prms:其他的一些參數,像編碼格式等,由使用者自定義。

三、代碼

Mat SourceMat = Cv2.ImRead(@"F:\Media\1018_2.png");//方法1Size imgSize = new Size(SourceMat.Width, SourceMat.Height);//構造函數設置必要參數//VideoWriter videoWriter = new VideoWriter(outVideo, VideoWriter.FourCC(@"MPG4"), 20, imgSize, true);VideoWriter videoWriter = new VideoWriter(@"F:\out.avi", VideoWriter.FourCC(@"XVID"), 20, imgSize, true);for (int i = 1; i <= 500; i++){Mat mat = new Mat();string FileString = @"F:\Media\1018_2.png";//FileString = FileString + i.ToString() + ".jpg";mat = Cv2.ImRead(FileString);if (!mat.Empty()){//Cv2.ImShow("Image", mat);videoWriter.Write(mat);Cv2.WaitKey(0);}}

這里面需要注意的是構造函數中的參數一定要對,不然可能生不成視頻或生成后打不開。

?不過這里有個問題,就是這個out.avi有大小也可以正常播放,但是會發現這個視頻的時長是0,用迅雷播放器播放不會有走秒

?用系統自帶的Media Player可以看到視頻在走秒

?不知道是哪里的問題,有知道的大神忘能指導一二。

?

?

總結

以上是生活随笔為你收集整理的3 、OpenCvSharp 图片转视频的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。