C/C++中的OpenCV讀取視頻與調(diào)用攝像頭
更新時間:2022年11月10日 08:32:59 作者:IT1995
這篇文章主要介紹了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++ 數(shù)據(jù)結(jié)構(gòu)完全二叉樹的判斷
這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)完全二叉樹的判斷的相關(guān)資料,需要的朋友可以參考下2017-06-06
淺析C++調(diào)用Java的Jar包(帶參數(shù))問題
這篇文章主要介紹了C++調(diào)用Java的Jar包(帶參數(shù))問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11
C語言 while for do while循環(huán)體詳解用法
在不少實際問題中有許多具有規(guī)律性的重復操作,因此在程序中就需要重復執(zhí)行某些語句。一組被重復執(zhí)行的語句稱之為循環(huán)體,能否繼續(xù)重復,決定循環(huán)的終止條件2021-10-10

