Opencv (C++) series learning --- read video files and open the camera

The two contents learned today are relatively simple, so without further ado, let’s go directly to the code!

Table of contents

【1】Read video file

【2】Camera reading video


【1】Read video file

#include<iostream>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc,char** argv)
{
	//定义视频读取器

	//若是读取视频,则需要在cap()内提供读取的地址
	VideoCapture cap("E:\\乔大花进度\\11-21\\读取摄像头和视频\\tree.avi");

	//对视频读取是否成功进行判断
	if (!cap.isOpened())
	{
		cout << "视频读取失败,请检查地址!!!" << endl;
		return -1;
	}

	Mat frame;//frame 用来接收视频每一帧的图片
	namedWindow("test",WINDOW_AUTOSIZE);
	while (true)
	{
		//将视频中每帧的图片传入frame
		cap >> frame;
		
		//第二种方法
	//	bool ok = cap.read(frame); ok是对视频是否成功读取每帧图片进行判断,抓取的每帧图像存在frame中

	//判断载入的图片是否为空,同时也可以用frame.data 代替
		if (frame.empty())
		{
			//重新从开始读取
			cout << "视频读取完成,重新开始读取" << endl;

			//set函数可以设置cap捕捉器中参数,也可以获取捕捉视频的一些信息
			cap.set(CAP_PROP_POS_FRAMES,0);//将帧设置在0处
			continue;
		}
		
		imshow("test",frame);
		
		//定义循环速度为50ms
		int key = waitKey(50);
		if (key==27||key=='q'||key=='Q')
		{
			break;
		}
	}
	destroyAllWindows();
	return 0;

}

operation result:

b5cf318a8af246e28fb8f371372356db.png

 This code is relatively simple, interested students can also challenge it, add a progress bar on the video reading, you can drag the progress bar to the specified position with the mouse, like the video player we usually use, mainly createTrackbar Subcall callback function implementation.

The reference code is as follows:

#include<iostream>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;

//定义全局变量
int g_slider_position = 0, frames_all = 0;
VideoCapture cap;
//定义全局函数
void onTrackerslide(int, void*);

void onTrackerslide(int, void*)
{
	//将视频移动到指定位置帧
	cap.set(CAP_PROP_POS_FRAMES, g_slider_position);
}

int main(int argc, char** argv)
{
	//定义视频读取器

	//若是读取视频,则需要在cap()内提供读取的地址
	/*VideoCapture cap("E:\\乔大花进度\\11-21\\读取摄像头和视频\\tree.avi");*/
	cap.open("E:\\乔大花进度\\11-21\\读取摄像头和视频\\1.mp4");

	//对视频读取是否成功进行判断
	if (!cap.isOpened())
	{
		cout << "视频读取失败,请检查地址!!!" << endl;
		return -1;
	}

	Mat frame;//frame 用来接收视频每一帧的图片
	namedWindow("test", WINDOW_AUTOSIZE);

	//获取图像的总帧数
	frames_all = cap.get(CAP_PROP_FRAME_COUNT);
	cout << "总的帧数为:" << frames_all << endl;
	//创建进度条
	createTrackbar("进度", "test", &g_slider_position, frames_all, onTrackerslide);

	//回调函数初始化
//	onTrackerslide(0,0);
	int i = 0;
	while (true)
	{
			//将视频中每帧的图片传入frame
			cap >> frame;

		//第二种方法
	//	bool ok = cap.read(frame); ok是对视频是否成功读取每帧图片进行判断,抓取的每帧图像存在frame中


	//判断载入的图片是否为空,同时也可以用frame.data 代替
		if (frame.empty())
		{
			//重新从开始读取
			cout << "视频读取完成,重新开始读取" << endl;

			//set函数可以设置cap捕捉器中参数,get可以获取捕捉视频的一些信息
			cap.set(CAP_PROP_POS_FRAMES, 0);//将帧设置在0处
			continue;
		}

		//设置进度条的位置,使进度条可以可以和视频一致运动
		int now_frame = cap.get(CAP_PROP_POS_FRAMES);
		setTrackbarPos("进度", "test", now_frame);

		//定义循环速度为50ms
		int key = waitKey(20);

		imshow("test", frame);

		if (key == 27 || key == 'q' || key == 'Q')
		{
			break;

		}

	}
	cv::destroyAllWindows();
	return 0;

}

The result of the operation is:

803dadf7002d4e17873f2b144a2c71c1.png

【2】Camera reading video

#include<opencv2\opencv.hpp>
#include<iostream>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
	//argc为设备上摄像头的数量,如果只有一个1,则初始ID为0,默认为-1
	cout << argc << endl;
	//打开初始摄像头
	VideoCapture cap(0);
	
	namedWindow("test",WINDOW_AUTOSIZE);
	Mat frame;
	while (true)
	{
		cap >> frame;
		if (!frame.data)
		{
			cout << "检查摄像头" << endl;
			break;
		}

		imshow("test", frame);
		int key = waitKey(20);
		if (key==27||key=='Q'||key=='q')
		{
			break;
		}

	}
	
	system("pause");
	return 0;
}

operation result:

68118b52409e48388a3a9b901550d327.png

Guess you like

Origin blog.csdn.net/qiaodahua/article/details/127979095