C/C++中的OpenCV讀取視頻與調(diào)用攝像頭
OpenCV讀取視頻與調(diào)用攝像頭
讀取視頻
1.先實例化再初始化
VideoCapture capture; Capture.open("1.avi");
2.實例化的同時進行初始化
VideoCapture capture("1.avi");
播放視頻
視頻讀如到VideoCapture類對象之后,用一個循環(huán)將每一幀顯示出來
while(1) { Mat frame; capture>>frame; imshow("讀取視頻",frame); waitkey(30); }
調(diào)用攝像頭
將代碼VideoCapture capture("1.avi")中的1.avi換成0就可以了
下面來看一段代碼:
#include <opencv2\opencv.hpp> using namespace cv; using namespace std; int main() { //讀取視頻或攝像頭 VideoCapture capture("1.avi"); while (true) { Mat frame; capture >> frame; imshow("讀取視頻", frame); waitKey(30); //延時30 } return 0;
這是讀取文件然后進行播放
下面是運行結(jié)果:
下面看看工程目錄的圖
下面是打開攝像頭的代碼
#include <opencv2\opencv.hpp> using namespace cv; using namespace std; int main() { //讀取視頻或攝像頭 VideoCapture capture(0); while (true) { Mat frame; capture >> frame; imshow("讀取視頻", frame); waitKey(30); //延時30 } return 0; }
運行結(jié)果:
Opencv讀取視頻以及打開攝像頭以及視頻讀取失敗原因
1、打開攝像頭
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; String window_name = "Capture - face detection"; int main() { ?? ?// 實例化 ?? ?VideoCapture camera; ?? ?camera.open(0); ? ?// 打開攝像頭, 默認攝像頭cameraIndex=0 ?? ?if (!camera.isOpened()) ?? ?{ ?? ??? ?cerr << "Couldn't open camera." << endl; ?? ?} ?? ?// 設(shè)置參數(shù) ?? ?camera.set(CAP_PROP_FRAME_WIDTH, 1000); ? ? ?// 寬度 ?? ?camera.set(CAP_PROP_FRAME_HEIGHT, 1000); ? ?// 高度 ?? ?camera.set(CAP_PROP_FPS, 30); ? ? ? ? ? ? ? ? ? ? // 幀率 ?? ?// 查詢參數(shù) ?? ?double frameWidth = camera.get(CAP_PROP_FRAME_WIDTH); ?? ?double frameHeight = camera.get(CAP_PROP_FRAME_HEIGHT); ?? ?double fps = camera.get(CAP_PROP_FPS); ?? ?// 循環(huán)讀取視頻幀 ?? ?while (true) ?? ?{ ?? ??? ?Mat frame; ?? ??? ?camera >> frame; ?? ??? ?imshow(window_name, frame); ?? ??? ?if (waitKey(33) == 27) break; ? // ESC 鍵退出 ?? ?} ?? ?// 釋放 ?? ?camera.release(); ?? ?destroyWindow("camera"); ?? ?return 0; }
2、視頻讀取
#include <opencv2/opencv.hpp> #include "zhelpers.h" #include <iostream> using namespace std; using namespace cv; String window_name = "Capture - face detection"; int main() { ?? ?// 實例化 ?? ?VideoCapture capture; ?? ?Mat frame; ?? ?capture.open("D:\\OtherFiles\\Video\\The.Wandering.Earth.mp4"); ?? ?//capture.open(0); ? ?// 打開攝像頭, 默認攝像頭captureIndex=0 ?? ?void* context = zmq_init(1); ?? ?void* publisher = zmq_socket(context, ZMQ_PUB); ?? ?zmq_bind(publisher, "tcp://*:5556"); ?? ?if (!capture.isOpened()) ?? ?{ ?? ??? ?cout << "Couldn't open capture." << endl; ?? ??? ?return -1; ?? ?} ?? ?// 設(shè)置參數(shù) ?? ?capture.set(CAP_PROP_FRAME_WIDTH, 1000); ? ? ?// 寬度 ?? ?capture.set(CAP_PROP_FRAME_HEIGHT, 1000); ? ?// 高度 ?? ?capture.set(CAP_PROP_FPS, 30); ? ? ? ? ? ? ? ? ? ? // 幀率 ?? ?// 查詢參數(shù) ?? ?double frameWidth = capture.get(CAP_PROP_FRAME_WIDTH); ?? ?double frameHeight = capture.get(CAP_PROP_FRAME_HEIGHT); ?? ?//double fps = capture.get(CAP_PROP_FPS); ?? ?// 循環(huán)讀取視頻幀 ?? ?while (true) ?? ?{ ?? ??? ?capture >> frame; ?? ??? ?imshow("capture", frame); ?? ??? ?if (waitKey(33) == 27) break; ? // ESC 鍵退出 ?? ?} ?? ?// 釋放 ?? ?capture.release(); ?? ?destroyWindow("capture"); ?? ?return 0; }
3、視頻讀取失敗原因
如果是
VideoCapture capture; capture.open("D:\\OtherFiles\\Video\\1.mp4");
第二部報錯:
將VS配置的鏈接器->附加依賴項中的opencv_worldxxx.lib刪除保留opencv_worldxxxd.lib
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
在C++17中實現(xiàn)無鎖數(shù)據(jù)結(jié)構(gòu)的方法詳解
在探索?C++17?中的無鎖數(shù)據(jù)結(jié)構(gòu)之前,我們首先需要理解無鎖編程的基本概念及其在現(xiàn)代軟件開發(fā)中的重要性,在這個章節(jié)中,我們將深入探討無鎖編程的概念,以及它如何滿足人類對于更高效、更可靠軟件的本能需求,文中通過代碼示例介紹的非常詳細,感興趣的朋友可以參考下2023-12-12簡要對比C語言中的setgid()函數(shù)和setregid()函數(shù)
這篇文章主要介紹了C語言中的setgid()函數(shù)和setregid()函數(shù)的簡要對比,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-08-08c語言中十進制轉(zhuǎn)二進制顯示小工具的實現(xiàn)代碼
本篇文章是對c語言中十進制轉(zhuǎn)二進制顯示小工具的實現(xiàn)代碼進行了詳細的分析的介紹,需要的朋友參考下2013-05-05C語言 數(shù)組中重復(fù)的數(shù)字分析及方法
這篇文章主要介紹了C語言 數(shù)組中重復(fù)的數(shù)字分析及方法的相關(guān)資料,需要的朋友可以參考下2017-03-03