opencv c++ 视频文件摄像头使用(22)

本文详细介绍了OpenCV中的VideoCapture API,包括如何通过路径、图像序列、视频链接或GStreamer管道来打开视频源。同时讲解了如何设置首选的捕获API后端,并展示了如何使用.get()方法获取视频的帧率、宽度、高度和帧数。通过示例代码,演示了视频播放、帧处理和显示的基本操作,以及如何调整和释放资源。此外,还提到可以通过获取网站视频的实际链接来捕获在线视频流。
摘要由CSDN通过智能技术生成

API:

VideoCapture  name(const string filename, int apiPreference = CAP_ANY)

参数说明:

filename

1、视频路径,name of video file (eg. video.avi)

2、一系列连续图像or image sequence (eg. img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...)

3、视频链接or URL of video stream (eg. protocol://host:port/script_name?script_params|auth)

4、or GStreamer pipeline string in gst-launch tool format in case if GStreamer is used as backend Note that each video stream or IP camera feed has its own URL scheme. Please refer to the documentation of source stream to know the right URL.

apiPreferencepreferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_FFMPEG or cv::CAP_IMAGES or cv::CAP_DSHOW.

当使用 VideoCapture  capture(0)时,能实现电脑摄像头调用。

.get(param):

输入不同的param可以获取各种视频的信息,参考页面:OpenCV: Additional flags for video I/O API backends

代码:

void QuickDemo::video_demo()
{
	VideoCapture capture(0);
	Mat frame;
    //获取帧率等信息
    int fps = capture.get(CAP_PROP_FPS);
	int width = capture.get(CAP_PROP_FRAME_WIDTH);
	int height = capture.get(CAP_PROP_FRAME_HEIGHT);
	int num = capture.get(CAP_PROP_FRAME_COUNT);
	cout << "fps" << fps << endl;
	cout << "width" << width << endl;
	cout << "height" << height << endl;
	cout << "num" << num << endl;
	while (true) {
		int c = waitKey(10);
		if (c == 27) {//退出
			break;
		}
        else if(frame.empty()){
            break;
        }
		capture.read(frame);
		//翻转输出图片
		flip(frame, frame, 1);
		// TODO,框图等操作,颜色变换
		imshow("frame", frame);
	}
}

 如果要打开视频文件的话,将上述代码capture(0),改为capture(videopath)即可。

注:完成后需要释放空间,一般API会完成自动释放,不过最好自定义释放位置。

capture.release();

 此外:

要获取在线视频,可以在网站的开发人员界面获取实际视频链接,然后在origin中输入对应字符串型的网址即可。

VideoCapture capture(origin);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值