如何用C++&&OpenCV&&Qt同时调用三个摄像头
本人大二,最近在学OpenCV方面的知识,之后将不断更新有关OpenCV的内容。
话不多说,直接上代码:
1. 头文件
#pragma once
#ifndef ImageProcessing_h
#define ImageProcessing_h
#include <QtWidgets/QMainWindow>
#include<QTimer>
#include <QMessageBox>
#include "ui_imageProcessing.h"
#include <core/core.hpp>
#include <highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include "opencv.hpp"
using namespace cv;
using namespace std;
class ImageProcessing : public QMainWindow
{
Q_OBJECT
public:
explicit ImageProcessing(QWidget *parent = Q_NULLPTR);
~ImageProcessing();
public:
void StartCamera();
void CloseCamera();//关闭摄像头
private:
VideoCapture videoCapture_1;//*定义三个VidelCapture对象来抓取摄像头图像*
VideoCapture videoCapture_2;
VideoCapture videoCapture_3;
Mat frame1;//Mat类,用于保存图像
Mat frame2;
Mat frame3;
VideoWriter write;//用于存储图像
bool OPEN;
private slots:
void OpenCamera_clicked();//打开摄像头
void StartREC_clicked();//开始录像
private:
Ui::ImageProcessingClass ui;
};
#endif // ImageProcessing_h
2. ImageProcessing.cpp文件
#include "ImageProcessing.h"
ImageProcessing::ImageProcessing(QWidget *parent) :
QMainWindow(parent)
{
ui.setupUi(this);
timer = new QTimer(this);
OPEN = true;
connect(ui.OpenCamera, SIGNAL(clicked()), this, SLOT(OpenCamera_clicked()));//打开相机按钮
connect(ui.CloseCamera, SIGNAL(clicked()), this, SLOT(close()));//关闭相机按钮
connect(ui.StartREC, SIGNAL(clicked()), this, SLOT(StartREC_clicked()));//保存视频按钮
//connect(timer, SIGNAL(timeout()), this, SLOT(getFrame()));
}
ImageProcessing::~ImageProcessing()
{
}
void ImageProcessing::OpenCamera_clicked()
{
// 设置彩色摄像头的拍摄属性为 分辨率1280x960,帧率25fps
videoCapture_1.set(CV_CAP_PROP_FRAME_HEIGHT, 960);
videoCapture_1.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
videoCapture_2.set(CV_CAP_PROP_FRAME_HEIGHT, 960);
videoCapture_2.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
videoCapture_1.set(CV_CAP_PROP_FPS, 25.0);
videoCapture_2.set(CV_CAP_PROP_FPS, 25.0);
// 设置彩色摄像头的拍摄属性为 分辨率2048x2048,帧率90fps
videoCapture_2.set(CV_CAP_PROP_FRAME_HEIGHT, 2048);
videoCapture_2.set(CV_CAP_PROP_FRAME_WIDTH, 2048);
videoCapture_3.set(CV_CAP_PROP_FPS, 90.0);
StartCamera();//启动外置摄像头,从摄像头中获取视频
}
void ImageProcessing::CloseCamera()
{
timer->stop();
videoCapture_1.release();
videoCapture_2.release();
videoCapture_3.release();
OPEN = false;
write.release();
destroyWindow("摄像头1");
destroyWindow("摄像头2");
destroyWindow("摄像头3");
}
void ImageProcessing::StartCamera()
{
int delay = 30;
videoCapture_1.open(0);//注意,电脑自带摄像头为0,外加则1、2、3以此类推,这里自行配置
videoCapture_2.open(1);
videoCapture_3.open(2);
if (!videoCapture_1.isOpened() || !videoCapture_2.isOpened() || !videoCapture_3.isOpened())
{
QMessageBox::warning(this, "Error", "Camera failed to start!");
exit(0);
}
while (1)
{
videoCapture_1 >> frame1;
namedWindow("摄像头1", WINDOW_NORMAL);
imshow("摄像头1", frame1);
videoCapture_2 >> frame2;
namedWindow("摄像头2", WINDOW_NORMAL);
imshow("摄像头2", frame2);
videoCapture_3 >> frame3;
namedWindow("摄像头3", WINDOW_NORMAL);
imshow("摄像头3", frame3);
if (waitKey(delay) == 27) break;(按esc键退出摄像头)
//else if(waitKey(delay)==32) waitKey(0);(实现按空格实时暂停功能)
else waitKey(30);
}
CloseCamera();
}
void ImageProcessing::StartREC_clicked()//目前只做了一个摄像头的保存,其余摄像头可自行仿制
{
videoCapture_1.open(0);
write.open("F:\\test.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(1280, 960), true);//Size最好对应上面的帧率设置。
while (videoCapture_1.isOpened())
{
videoCapture_1 >> frame1;
//write << frame1;
//设置保存视频的格式为AVI,编码为MJPG
write.write(frame1);
namedWindow("摄像头1", WINDOW_NORMAL);
imshow("摄像头1", frame1);
if (cvWaitKey(20) == 27) break;
}
videoCapture_1.release();
write.release();
destroyWindow("摄像头1");
}
3. main文件
#include "ImageProcessing.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ImageProcessing w;
w.show();
return a.exec();
}
附图: