c++~ Opencv读取视频以及打开摄像头以及视频读取失败原因

1、打开摄像头

#include 
#include 
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;
}

2、视频读取

#include 
#include "zhelpers.h"
#include 
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;
}

3、视频读取失败原因
如果是

VideoCapture capture;
capture.open("D:\\OtherFiles\\Video\\1.mp4");

第二部报错:
将VS配置的链接器->附加依赖项中的opencv_worldxxx.lib删除保留opencv_worldxxxd.lib

你可能感兴趣的:(opencv,c++)