網站首頁 編程語言 正文
本文實例為大家分享了C#+EmguCV使用攝像頭讀取、保存視頻的具體代碼,供大家參考,具體內容如下
在Emgucv中調用攝像頭需要用到VideoCapture類
這個類有四種重載的方式
1、不寫參數,那么初始化的時候就會調用默認的攝像頭
2、一個int類型的數字,這個數字的含義是攝像頭的索引。會按照索引來初始化攝像頭
3、一個字符串,這個字符串的含義是文件名或視頻流名。初始化會讀取視頻文件或視頻流
4、CaptureType 的枚舉
這個枚舉的內容很多,這里不再贅述
新建一個控制臺應用程序來實現視頻的讀取和保存,代碼如下
class Program ? ? { ? ? ? ? static void Main(string[] args) ? ? ? ? { ? ? ? ? ? ? show(); ? ? ? ? } ? ? ? ? ? private static void show() ? ? ? ? { ? ? ? ? ? ? //VideoCapture cap = new VideoCapture("AVSEQ02.avi"); ? ? ? ? ? ? //VideoCapture cap = new VideoCapture(CaptureType.Any); ? ? ? ? ? ? VideoCapture cap = new VideoCapture(); ? ? ? ? ? ? VideoWriter writer = new VideoWriter("save.avi", 20, new Size(640, 480), true); ? ? ? ? ? ? if (!cap.IsOpened) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("打開失敗"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? Mat mat = new Mat(); ? ? ? ? ? ? while (true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? cap.Read(mat); ? ? ? ? ? ? ? ? if (mat.IsEmpty) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Console.WriteLine("讀取完畢"); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? CvInvoke.Imshow("video", mat); ? ? ? ? ? ? ? ? writer.Write(mat); ? ? ? ? ? ? ? ? CvInvoke.WaitKey(30); ? ? ? ? ? ? } ? ? ? ? ? ? CvInvoke.WaitKey(0); ? ? ? ? ? } ? ? }
這里用VideoCapture 的Read()方法來讀取視頻的每一幀。保存在Mat這個對象中。并 CvInvoke.Imshow()出來。然后用VideoWriter的Write方法,把每一幀的圖像保存成視頻。
這里需要注意的地方是:在循環中需要停留30毫秒,否則視頻顯示不出來。 CvInvoke.WaitKey(30);
運行程序后,可以看到,攝像頭讀取的視頻已經保存了下來
原文鏈接:https://blog.csdn.net/qq_34090937/article/details/84448820
相關推薦
- 2022-09-30 python語言中pandas字符串分割str.split()函數_python
- 2022-11-09 PostgreSQL索引掃描時為什么index?only?scan不返回ctid_PostgreSQ
- 2022-11-10 C++實現拷貝構造函數的方法詳解_C 語言
- 2022-06-28 C++實現String與UF8互轉_C 語言
- 2022-10-13 C++智能指針詳解_C 語言
- 2022-06-19 C#文件非占用讀取與幫助類FileHelper_C#教程
- 2022-09-12 Windows系統下安裝tensorflow的配置步驟_python
- 2023-01-31 MongoDB?聚合查詢詳解_MongoDB
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支