C/C++中的OpenCV讀取視頻與調(diào)用攝像頭
OpenCV讀取視頻與調(diào)用攝像頭
讀取視頻
1.先實(shí)例化再初始化
VideoCapture capture;
Capture.open("1.avi");2.實(shí)例化的同時(shí)進(jìn)行初始化
VideoCapture capture("1.avi");播放視頻
視頻讀如到VideoCapture類對(duì)象之后,用一個(gè)循環(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); //延時(shí)30
}
return 0;這是讀取文件然后進(jìn)行播放
下面是運(yùn)行結(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); //延時(shí)30
}
return 0;
}運(yùn)行結(jié)果:

Opencv讀取視頻以及打開攝像頭以及視頻讀取失敗原因
1、打開攝像頭
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
String window_name = "Capture - face detection";
int main() {
?? ?// 實(shí)例化
?? ?VideoCapture camera;
?? ?camera.open(0); ? ?// 打開攝像頭, 默認(rèn)攝像頭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() {
?? ?// 實(shí)例化
?? ?VideoCapture capture;
?? ?Mat frame;
?? ?capture.open("D:\\OtherFiles\\Video\\The.Wandering.Earth.mp4");
?? ?//capture.open(0); ? ?// 打開攝像頭, 默認(rèn)攝像頭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");第二部報(bào)錯(cuò):
將VS配置的鏈接器->附加依賴項(xiàng)中的opencv_worldxxx.lib刪除保留opencv_worldxxxd.lib
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++ 數(shù)據(jù)結(jié)構(gòu)完全二叉樹的判斷
這篇文章主要介紹了C++ 數(shù)據(jù)結(jié)構(gòu)完全二叉樹的判斷的相關(guān)資料,需要的朋友可以參考下2017-06-06
基于Qt+OpenCV實(shí)現(xiàn)圖像灰度化像素
在圖像處理領(lǐng)域,OpenCV是一款強(qiáng)大而廣泛應(yīng)用的開源庫,能夠提供豐富的圖像處理和計(jì)算機(jī)視覺功能,本文將介紹如何利用Qt?編輯器調(diào)用OpenCV庫對(duì)照片進(jìn)行換底色處理,實(shí)現(xiàn)更加獨(dú)特和吸引人的效果2023-11-11
淺析C++調(diào)用Java的Jar包(帶參數(shù))問題
這篇文章主要介紹了C++調(diào)用Java的Jar包(帶參數(shù))問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
C語言 while for do while循環(huán)體詳解用法
在不少實(shí)際問題中有許多具有規(guī)律性的重復(fù)操作,因此在程序中就需要重復(fù)執(zhí)行某些語句。一組被重復(fù)執(zhí)行的語句稱之為循環(huán)體,能否繼續(xù)重復(fù),決定循環(huán)的終止條件2021-10-10

