目录OpenCV读取视频与调用摄像头读取视频播放视频调用摄像头这是读取文件然后进行播放下面是打开摄像头的代码Opencv读取视频以及打开摄像头以及视频读取失败原因1、打开摄像头2、视
1.先实例化再初始化
VideoCapture capture;
Capture.open("1.avi");
2.实例化的同时进行初始化
VideoCapture capture("1.avi");
视频读如到VideoCapture类对象之后,用一个循环将每一帧显示出来
将代码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;
下面是运行结果:
下面看看工程目录的图
#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;
}
运行结果:
#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;
}
// 设置参数
camera.set(CAP_PROP_FRAME_WIDTH, 1000); // 宽度
camera.set(CAP_PROP_FRAME_HEIGHT, 1000); // 高度
camera.set(CAP_PROP_FPS, 30); // 帧率
// 查询参数
double frameWidth = camera.get(CAP_PROP_FRAME_WIDTH);
double frameHeight = camera.get(CAP_PROP_FRAME_HEIGHT);
double fps = camera.get(CAP_PROP_FPS);
// 循环读取视频帧
while (true)
{
Mat frame;
camera >> frame;
imshow(window_name, frame);
if (waitKey(33) == 27) break; // ESC 键退出
}
// 释放
camera.release();
destroyWindow("camera");
return 0;
}
#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;
}
// 设置参数
capture.set(CAP_PROP_FRAME_WIDTH, 1000); // 宽度
capture.set(CAP_PROP_FRAME_HEIGHT, 1000); // 高度
capture.set(CAP_PROP_FPS, 30); // 帧率
// 查询参数
double frameWidth = capture.get(CAP_PROP_FRAME_WIDTH);
double frameHeight = capture.get(CAP_PROP_FRAME_HEIGHT);
//double fps = capture.get(CAP_PROP_FPS);
// 循环读取视频帧
while (true)
{
capture >> frame;
imshow("capture", frame);
if (waitKey(33) == 27) break; // ESC 键退出
}
// 释放
capture.release();
destroyWindow("capture");
return 0;
}
如果是
VideoCapture capture;
capture.open("D:\\OtherFiles\\Video\\1.mp4");
第二部报错:
将VS配置的链接器->附加依赖项中的opencv_worldxxx.lib删除保留opencv_worldxxxd.lib
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: C/C++中的OpenCV读取视频与调用摄像头
本文链接: https://www.lsjlt.com/news/171145.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0